2 releases

0.1.2 Apr 25, 2019
0.1.1 Apr 25, 2019

#26 in #64-bit

MIT license

8KB
118 lines

Unique ID Generator

Unique 64 bit ID generator inspired by twitter snowflake

By default, settings are:

  • Machine ID - 8 bit (up to 255 machines)
  • Timestamp subset - 41 bits ~69 years since Unix Epoch
  • Remaining bits (15) - sequence number

If sequence number is overflowing, generator will wait in chunks of 100 microseconds until next millisecond.

Can be configured:

  • Machine ID - up to 8 bit
  • Timestamp subset - from 41 to 43 bits (up to 278 years since Unix Epoch)

Usage

Add into your Cargo.toml dependencies:

idgen = "0.1.2"
use idgen::IDGen;

fn main() {
    let idgen = IDGen::new(128);
    println!("{}", idgen.new_id());
}

Alternatively, it can be configured to have more bits for sequence number with less bits for machine ID:

use idgen::IDGen;

fn main() {
    let idgen = IDGen::new_with_config(1, 1, 41);
    println!("{}", idgen.new_id());
}

Notes

  • Performance is "ok" - on MacBook Air 2019 it generates ~3M unique ids per second in single-threaded mode (Overhead for interior mutability/thread safety at least halves the performance)
  • It is thread-safe
  • Strictly speaking, it can be used with less than 41 bits for timestamp (as only last meaningful bits are taken into account)

No runtime deps