Expand description



Customizable and thread-safe distributed id generator, like twitter’s snowflake.

Simple usage for single generator

use frostflake::{Generator, GeneratorOptions};

let mut generator = Generator::new(GeneratorOptions::default());

let id1 = generator.generate();
let id2 = generator.generate();
let id3 = generator.generate();

Async generator works with tokio

This requires tokio feature.

use frostflake::{GeneratorAsync, GeneratorOptions};

async fn main() {
    let generator = GeneratorAsync::spawn(GeneratorOptions::default());

    let id1 = generator.generate().await.unwrap();

GeneratorAsync is Send so that you can pass it to other threads or tasks safely.

use frostflake::{GeneratorAsync, GeneratorOptions};

async fn main() {
    let g = GeneratorAsync::spawn(GeneratorOptions::default());
    for _ in 0..10 {
        let g = g.clone();
        tokio::spawn(async move {
            let id = g.generate().await.unwrap();

Multi-threads generator by GeneratorPool

This requires std-thread feature.

use frostflake::{GeneratorPool, GeneratorPoolOptions};
use std::thread;

// create 10 generators
let pool = GeneratorPool::new(10, GeneratorPoolOptions::default());

    // pool also can be shared with threads by std::sync::Arc
    let pool = pool.clone();
    thread::spawn(move || {
        let id = pool.generate();


frostflake is highly configurable.

use frostflake::{Generator, GeneratorOptions};

let opts = GeneratorOptions::default()
    .bits(42, 10, 12)           // 42bit timestamp, 10bit node, 12bit sequence
    .base_ts(1483228800000)     // base time 2017-01-01T00:00:00Z as milliseonds
    .node(3);                   // node number

let generator = Generator::new(opts);

Also, time function is can be set. If you want to use plain seconds unit instead of millisedond, you can do by this:

use frostflake::{Generator, GeneratorOptions};
use std::time::{SystemTime, UNIX_EPOCH};

fn my_time() -> u64 {
    let t = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();

// use smaller time bits (because this is not milliseconds)
// use larger sequence bits
let opts = GeneratorOptions::default()
    .base_ts(0) // need this for avoid exceeding time value on smaller bit size
    .bits(36, 10, 18)
    .base_ts(1483228800) // base time should be second too
    .time_fn(my_time); // set my time function

let generator = Generator::new(opts);

Default configurations

OptionsDefault value
bits42=timestamp, 10=node, 12=sequence
base_ts1483228800000 (2017-01-01T00:00:00Z as milliseonds)
time_fnreturn current milliseonds

Almost same as Generator, but GeneratorPool uses pool_id bit for distinguish each pools.

So default bit widths is:

OptionsDefault value
bits42=timestamp, 4=pool_id, 6=node, 12=sequence

All other options are same with Generator.


  • Support redis based automatic node_id generation like katsubushi
  • Support other async runtimes?

Patches or pull-requests are always welcome.# frostflake