#unique-id #snowflake-id #id #generator #distributed-id #snowflake

idgenerator

A powerful unique id generator. Shorter ID and faster generation with a new snowflake drift algorithm. The core is to shorten the ID length, but also can have a very high instantaneous concurrent processing capacity, and powerful configuration capacity.

5 stable releases

2.0.0 Feb 8, 2022
1.2.1 Dec 13, 2021
1.2.0 Oct 22, 2021
1.1.0 Oct 22, 2021
1.0.0 Oct 19, 2021

#1192 in Algorithms

Download history 375/week @ 2024-07-20 340/week @ 2024-07-27 299/week @ 2024-08-03 251/week @ 2024-08-10 490/week @ 2024-08-17 499/week @ 2024-08-24 797/week @ 2024-08-31 762/week @ 2024-09-07 607/week @ 2024-09-14 491/week @ 2024-09-21 827/week @ 2024-09-28 512/week @ 2024-10-05 221/week @ 2024-10-12 399/week @ 2024-10-19 330/week @ 2024-10-26 508/week @ 2024-11-02

1,485 downloads per month

MIT license

43KB
859 lines

IdGenerator

github-repo LICENSE Apache-2.0

docs.rs crates.io codecov

A powerful unique id generator.

ATTENTION: Now this crate has upgraded to v2, meaning most of the interfaces in v1 is deprecated. If you still want to use v1, please see branch v1. You can also see deprecated.md for how to use v1.

Shorter ID and faster generation with a new snowflake drift algorithm. The core is to shorten the ID length, but also can have a very high instantaneous concurrent processing capacity (about 50W/0.1s), and powerful configuration capacity.

If you want to have such a high throughput, please set a higher seq_bit_len (e.g. 10 or 12).

Usage

Out-of-the-box Instances implemented by this lib

This lib has implemented two out-of-the-box instances:

You can also see how lib test works in src/lib.rs or how benchmark works in bench/id_bench.rs.

The steps mainly can be described as:

  1. Setup the options(i.e. configure the instance).
  2. Initialize the instance or set its options.
  3. Call the next_id method to generate unique id.

What you can configure about the instances is demonstrated as the struct IdGeneratorOptions:

  • method: 1 means snowflake with shift.
  • base_time: base time of the snowflake algorithm, in milliseconds, can not exceed the current system time.
  • worker_id: should be decided externally, smaller than 2^worker_id_bit_len-1.
  • worker_id_bit_len: the bit length of worker_id, default to 8, in range [1, 15]. worker_id_bit_len + seq_bit_len should be less than 22.
  • seq_bit_len: the bit length of sequence, default to 8, in range [3, 21].
  • max_seq_num: set the range of [min_seq_num, 2^seq_bit_len-1], default to 0 meaning 2^seq_bit_len-1.
  • min_seq_num: default to 5, range [5, max_seq_num], reserved for manually value and time turned back.
  • top_over_cost_count: max shift count(included), default to 2000, recommended range is [500, 20000] (associated with computing ability).

A very simple example:

use idgenerator::*;

fn main() {
    // Setup the option for the id generator instance.
    let options = IdGeneratorOptions::new().worker_id(1).worker_id_bit_len(6);
    // Initialize the id generator instance with the option.
    // Other options not set will be given the default value.
    let _ = IdInstance::init(options)?;
    // Call `next_id` to generate a new unique id.
    let id = IdInstance::next_id();
    println!("id is {}", id);
}

For more complex usage, see directory examples.

Self-implement Instances

This lib wraps the snowflake algorithm inside the CoreIdGenerator struct. You can wrap this struct inside your own instance of unique id generator.

For more details, please refer to Documentation.

Credits

Dependencies

~1.6–6.5MB
~46K SLoC