frostflake
Customizable and thread-safe distributed id generator, like twitter's snowflake.
Simple usage for single generator
use ;
let mut generator = new;
let id1 = generator.generate;
let id2 = generator.generate;
let id3 = generator.generate;
Async generator works with tokio
This requires tokio
feature.
use ;
async
GeneratorAsync
is Send
so that you can pass it to other threads or tasks safely.
use ;
async
Multi-threads generator by GeneratorPool
This requires std-thread
feature.
use ;
use thread;
// create 10 generators
let pool = new;
Configurations
frostflake is highly configurable.
use ;
let opts = default
.bits // 42bit timestamp, 10bit node, 12bit sequence
.base_ts // base time 2017-01-01T00:00:00Z as milliseonds
.node; // node number
let generator = new;
Also, time function is can be set. If you want to use plain seconds unit instead of millisedond, you can do by this:
use ;
use ;
// use smaller time bits (because this is not milliseconds)
// use larger sequence bits
let opts = default
.base_ts // need this for avoid exceeding time value on smaller bit size
.bits
.base_ts // base time should be second too
.time_fn; // set my time function
let generator = new;
Default configurations
Generator
Options | Default value |
---|---|
bits | 42=timestamp, 10=node, 12=sequence |
base_ts | 1483228800000 (2017-01-01T00:00:00Z as milliseonds) |
node | 0 |
time_fn | return current milliseonds |
GeneratorPool
Almost same as Generator, but GeneratorPool uses pool_id
bit for distinguish each pools.
So default bit widths is:
Options | Default value |
---|---|
bits | 42=timestamp, 4=pool_id, 6=node, 12=sequence |
All other options are same with Generator.
TODO
- Support redis based automatic node_id generation like katsubushi
- Support other async runtimes?
Patches or pull-requests are always welcome.