1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//! # frostflake
//!
//! [![Crates.io](https://img.shields.io/crates/v/frostflake.svg)](https://crates.io/crates/frostflake)
//!
//! Customizable and thread-safe distributed id generator, like twitter's [snowflake](https://github.com/twitter/snowflake).
//!
//! ## Simple usage for single generator
//!
//! ```rust
//! 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.
//!
//! ```ignore
//! use frostflake::{GeneratorAsync, GeneratorOptions};
//!
//! #[tokio::main]
//! 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.
//!
//! ```ignore
//! use frostflake::{GeneratorAsync, GeneratorOptions};
//!
//! #[tokio::main]
//! 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.
//!
//! ```ignore
//! 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();
//!     }).join();
//! }
//! ```
//!
//! ## Configurations
//!
//! frostflake is highly configurable.
//!
//! ```rust
//! 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:
//!
//! ```rust
//! use frostflake::{Generator, GeneratorOptions};
//! use std::time::{SystemTime, UNIX_EPOCH};
//!
//! fn my_time() -> u64 {
//!     let t = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
//!     t.as_secs()
//! }
//!
//! // 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
//!
//! #### 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](https://github.com/kayac/go-katsubushi)
//! - Support other async runtimes?
//!
//! Patches or pull-requests are always welcome.# frostflake

use std::time::{SystemTime, UNIX_EPOCH};
use std::u64::MAX;

#[cfg(feature = "tokio")]
pub mod tokio;

#[cfg(feature = "std-thread")]
pub mod pool;

#[cfg(feature = "tokio")]
pub use crate::tokio::GeneratorAsync;
#[cfg(feature = "std-thread")]
pub use pool::{GeneratorPool, GeneratorPoolOptions};

#[derive(Clone)]
pub struct GeneratorOptions {
    bits: (u8, u8, u8),
    base_ts: u64,
    node: u64,
    time_fn: fn() -> u64,
}

pub struct Generator {
    opts: GeneratorOptions,
    last_ts: u64,
    seq: u64,
}

fn default_time_fn() -> u64 {
    let t = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
    t.as_secs() * 1000 + (t.subsec_nanos() as u64) / 1000000
}

impl Default for GeneratorOptions {
    fn default() -> Self {
        GeneratorOptions {
            bits: (42, 10, 12),
            base_ts: 1483228800000, // 2017-01-01T00:00:00Z as milliseconds
            node: 0,
            time_fn: default_time_fn,
        }
    }
}

impl GeneratorOptions {
    pub fn time_fn(mut self, time_fn: fn() -> u64) -> Self {
        self.time_fn = time_fn;
        self
    }

    pub fn bits(mut self, ts_bits: u8, node_bits: u8, seq_bits: u8) -> Self {
        assert!(
            64 == ts_bits + node_bits + seq_bits,
            "bits set should be total 64bit"
        );
        assert!(
            self.base_ts <= max(ts_bits),
            "base_ts exceeds ts_bits limit, set base_ts first"
        );
        assert!(
            self.node <= max(node_bits),
            "node number exceeeds node_bits limit, set node number first"
        );

        self.bits = (ts_bits, node_bits, seq_bits);
        self
    }

    pub fn node(mut self, node: u64) -> Self {
        assert!(
            node <= max(self.bits.1),
            "node number exceeds node_bits limit, set bit width first"
        );

        self.node = node;
        self
    }

    pub fn base_ts(mut self, base_ts: u64) -> Self {
        assert!(
            base_ts <= max(self.bits.0),
            "base_ts exceeds ts_bits limit, set bit width first"
        );

        self.base_ts = base_ts;
        self
    }
}

impl Generator {
    pub fn new(opts: GeneratorOptions) -> Generator {
        Generator {
            opts,
            last_ts: 0,
            seq: 0,
        }
    }

    pub fn generate(&mut self) -> u64 {
        let now = (self.opts.time_fn)();
        assert!(
            now > self.opts.base_ts,
            "time_fn returned the time before base_ts"
        );
        assert!(
            now >= self.last_ts,
            "clock moved backwards. check your NTP setup"
        );

        let elapsed = now - self.opts.base_ts;

        let seq = if now == self.last_ts { self.seq + 1 } else { 0 };

        let (_, node_bits, seq_bits) = self.opts.bits;

        assert!(seq <= max(seq_bits), "seq number exceeds seq_bits!");

        let ts_mask = bitmask(node_bits + seq_bits);
        let node_mask = bitmask(seq_bits) ^ ts_mask;

        self.last_ts = now;
        self.seq = seq;

        ((elapsed << (node_bits + seq_bits)) & ts_mask)
            | ((self.opts.node << seq_bits) & node_mask)
            | seq & max(seq_bits)
    }

    pub fn extract(&self, id: u64) -> (u64, u64, u64) {
        let (ts_bits, node_bits, seq_bits) = self.opts.bits;

        let ts = (id >> (node_bits + seq_bits)) & max(ts_bits);
        let node = (id >> seq_bits) & max(node_bits);
        let seq = id & max(seq_bits);

        (ts, node, seq)
    }
}

fn bitmask(shift: u8) -> u64 {
    MAX << shift
}

fn max(shift: u8) -> u64 {
    !bitmask(shift)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::sync::{Arc, Mutex};
    use std::thread;

    #[test]
    fn test_basic() {
        fn my_time_fn() -> u64 {
            1483228800000 + 123
        }

        let opts = GeneratorOptions::default().time_fn(my_time_fn);

        let mut g = Generator::new(opts);
        assert_eq!(g.generate(), (123 << 22));
        assert_eq!(g.generate(), (123 << 22) + 1);
        assert_eq!(g.generate(), (123 << 22) + 2);
    }

    #[test]
    fn test_extract() {
        fn my_time_fn() -> u64 {
            1483228800000 + 123
        }

        let opts = GeneratorOptions::default().time_fn(my_time_fn).node(3);

        let mut g = Generator::new(opts);

        let id = g.generate();
        let (ts, node, seq) = g.extract(id);
        assert_eq!(ts, 123);
        assert_eq!(node, 3);
        assert_eq!(seq, 0);

        let id = g.generate();
        let (ts, node, seq) = g.extract(id);
        assert_eq!(ts, 123);
        assert_eq!(node, 3);
        assert_eq!(seq, 1);
    }

    #[test]
    fn test_bitmask() {
        assert_eq!(bitmask(1), 0xFFFFFFFFFFFFFFFE);
        assert_eq!(bitmask(4), 0xFFFFFFFFFFFFFFF0);
        assert_eq!(bitmask(8), 0xFFFFFFFFFFFFFF00);
    }

    #[test]
    fn test_max() {
        assert_eq!(max(1), 1);
        assert_eq!(max(2), 3);
        assert_eq!(max(8), 255);
    }

    #[test]
    fn test_threaded() {
        let g = Arc::new(Mutex::new(Generator::new(GeneratorOptions::default())));
        let mut h = vec![];
        let results = Arc::new(Mutex::new(vec![]));

        for _ in 1..10 {
            let g = g.clone();
            let r = results.clone();

            let handle = thread::spawn(move || {
                for _ in 1..100 {
                    let mut g = g.lock().unwrap();
                    let mut r = r.lock().unwrap();
                    r.push(g.generate());
                }
            });
            h.push(handle);
        }

        for handle in h {
            let _ = handle.join();
        }

        let mut hash: HashMap<u64, u64> = HashMap::new();
        for r in results.lock().unwrap().iter() {
            // check uniqueness
            assert_eq!(hash.contains_key(r), false);
            hash.insert(*r, 1);
            assert_eq!(hash.contains_key(r), true);
        }
    }
}