You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+18Lines changed: 18 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -27,6 +27,24 @@ This assumes you already have a working Go environment, if not please see
27
27
go get github.com/bwmarrin/snowflake
28
28
```
29
29
30
+
### ID Format
31
+
By default, the ID format follows the original Twitter snowflake format.
32
+
* The ID as a whole is a 63 bit integer stored in an int64
33
+
* 41 bits are used to store a timestamp with millisecond precision, using a custom epoch.
34
+
* 10 bits are used to store a node id - a range from 0 through 1023.
35
+
* 12 bits are used to store a sequence number - a range from 0 through 4095.
36
+
37
+
### Custom Format
38
+
Coming soon.. ish. I plan to find the best way to modify the existing package to allow altering the number of bits used for Node ID's and Sequence Numbers. The goal is to implement this without breaking the API (1/31/18)
39
+
40
+
### How it Works.
41
+
Each time you generate an ID, it works, like this.
42
+
* A timestamp with millisecond precision is stored in the first 41 bits of the ID.
43
+
* Then the NodeID is added in subsequent bits.
44
+
* Then the Sequence Number is added, starting at 0 and incrementing for each ID generated in the same millisecond. If you generate enough IDs in the same millisecond that the sequence would roll over or overfill then the generate function will pause until the next millisecond.
45
+
46
+
Using the default settings, this allows for 4096 unique IDs to be generated every millisecond, per Node ID.
47
+
30
48
### Usage
31
49
32
50
Import the package into your project then construct a new snowflake Node using a
0 commit comments