❄️ unique id generator docs.rs/snof/latest/snof/
0

Configure Feed

Select the types of activity you want to include in your feed.

Rust 82.0%
Other 18.0%
3 1 0

Clone this repository

https://tangled.org/filtercoffee.cc/snof https://tangled.org/did:plc:3en7iz647ebfuswh6sprbgpr
git@tangled.org:filtercoffee.cc/snof git@tangled.org:did:plc:3en7iz647ebfuswh6sprbgpr

For self-hosted knots, clone URLs may differ based on your setup.



readme.md

❄️ snof#

snof is a unique ID generator. Loosely based on snowflake ID-s, snof generates 64 bit long identifiers consisting of a 32 bit millisecond-based timestamp, and 22 bits of sequence distinguishing identifiers generated within the same millisecond.

The generator uses atomic operations for tracking state, thus it provides a thread-safe, lock-free way of generating unique ID-s. In case of the sequence being exhausted, or the clock moving backwards, the generator spins until validity is restored.

Usage#

use snof::SnowflakeGenerator;

fn main() {
    let generator = Arc::new(SnowflakeGenerator::new());

    let threads: Vec<_> = (0..4).map(|_| {
        let other_generator = Arc::clone(&generator);
        thread::spawn(move || {
            let id = other_generator.generate();
            println!("thread id: {}", id.0);
        })
    }).collect();

    for t in threads { t.join().unwrap(); }
}