| 1 | <div> |
| 2 | <p align="center"> |
| 3 | <image src="https://www.pngkey.com/png/full/105-1052235_snowflake-png-transparent-background-snowflake-with-clear-background.png" width="250" height="250"> |
| 4 | </p> |
| 5 | <p align="center">An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).</p> |
| 6 | <p align="center"> |
| 7 | <a href="https://github.com/godruoyi/go-snowflake/actions?workflow=run%20tests"> |
| 8 | <image src="https://github.com/godruoyi/go-snowflake/workflows/run%20tests/badge.svg" alt="test"> |
| 9 | </a> |
| 10 | <a href="https://goreportcard.com/report/github.com/godruoyi/go-snowflake"> |
| 11 | <image src="https://goreportcard.com/badge/github.com/godruoyi/go-snowflake" alt="Go report"> |
| 12 | </a> |
| 13 | <a href="https://coveralls.io/repos/github/godruoyi/go-snowflake/badge.svg?branch=master"> |
| 14 | <image src="https://coveralls.io/repos/github/godruoyi/go-snowflake/badge.svg?branch=master" alt="Coverage status"> |
| 15 | </a> |
| 16 | </p> |
| 17 | </div> |
| 18 | |
| 19 | ## Description |
| 20 | |
| 21 | An **Lock Free** ID Generator for Golang implementation. |
| 22 | |
| 23 |  |
| 24 | |
| 25 | Snowflake is a network service for generating unique ID numbers at high scale with some simple guarantees. |
| 26 | |
| 27 | * The first bit is unused sign bit. |
| 28 | * The second part consists of a 41-bit timestamp (milliseconds) whose value is the offset of the current time relative |
| 29 | to a certain time. |
| 30 | * The 6 bits machineID, max num is 2^4 = 32. |
| 31 | * The last part consists of 6 bits, its means the length of the serial number generated per millisecond per working |
| 32 | node, a maximum of 2^6 = 64 IDs can be generated in the same millisecond. |
| 33 | * The binary length of 41 bits is at most 2^41 millisecond = 69 years. So the snowflake algorithm can be used for up |
| 34 | to 69 years, In order to maximize the use of the algorithm, you should specify a start time for it. |
| 35 | |
| 36 | The ID generated by the snowflake algorithm is not guaranteed to be unique. For example, when two different requests |
| 37 | enter the same machine at the same time, and the sequence generated by the node is the same, the generated ID will be |
| 38 | duplicated. |
| 39 | |
| 40 | So if you want use the snowflake algorithm to generate unique ID, You must ensure: The sequence-number generated in the |
| 41 | same millisecond of the same node is unique. |
| 42 | |
| 43 | Based on this, we created this package and integrated multiple sequence-number providers into it. |
| 44 | |
| 45 | * AtomicResolver (base sync/atomic) |
| 46 | |
| 47 | > Each provider only needs to ensure that the serial number generated in the same millisecond is different. You can get |
| 48 | > a unique ID. |
| 49 | |
| 50 | ## Feature |
| 51 | |
| 52 | - ✅ Lock Free |
| 53 | - 🎈 Zero configuration, out of the box |
| 54 | - 🚀 Concurrency safety |
| 55 | - 🌵 Support private ip to machineid |
| 56 | - 🐡 Support custom sequence resolver |
| 57 | |
| 58 | ## Installation |
| 59 | |
| 60 | ```shell |
| 61 | $ go get github.com/kankanreno/go-snowflake |
| 62 | ``` |
| 63 | |
| 64 | ## Usage |
| 65 | |
| 66 | 1. simple to use. |
| 67 | |
| 68 | ```go |
| 69 | package main |
| 70 | |
| 71 | import ( |
| 72 | "fmt" |
| 73 | |
| 74 | "github.com/kankanreno/go-snowflake" |
| 75 | ) |
| 76 | |
| 77 | func main() { |
| 78 | id := snowflake.ID() |
| 79 | fmt.Println(id) |
| 80 | // 1537200202186752 |
| 81 | } |
| 82 | ``` |
| 83 | |
| 84 | 2. Specify the MachineID. |
| 85 | |
| 86 | ```go |
| 87 | package main |
| 88 | |
| 89 | import ( |
| 90 | "fmt" |
| 91 | |
| 92 | "github.com/kankanreno/go-snowflake" |
| 93 | ) |
| 94 | |
| 95 | func main() { |
| 96 | snowflake.SetMachineID(1) |
| 97 | |
| 98 | // Or set private ip to machineid, testing... |
| 99 | // snowflake.SetMachineID(snowflake.PrivateIPToMachineID()) |
| 100 | |
| 101 | id := snowflake.ID() |
| 102 | fmt.Println(id) |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | 3. Specify start time. |
| 107 | |
| 108 | ```go |
| 109 | package main |
| 110 | |
| 111 | import ( |
| 112 | "fmt" |
| 113 | "time" |
| 114 | |
| 115 | "github.com/kankanreno/go-snowflake" |
| 116 | ) |
| 117 | |
| 118 | func main() { |
| 119 | snowflake.SetStartTime(time.Date(2014, 9, 1, 0, 0, 0, 0, time.UTC)) |
| 120 | id := snowflake.ID() |
| 121 | fmt.Println(id) |
| 122 | } |
| 123 | ``` |
| 124 | |
| 125 | 4. Parse ID. |
| 126 | |
| 127 | ```go |
| 128 | package main |
| 129 | |
| 130 | import ( |
| 131 | "fmt" |
| 132 | "time" |
| 133 | |
| 134 | "github.com/kankanreno/go-snowflake" |
| 135 | ) |
| 136 | |
| 137 | func main() { |
| 138 | id := snowflake.ID() |
| 139 | sid := snowflake.ParseID(id) |
| 140 | |
| 141 | fmt.Println(sid.ID) // 132271570944000000 |
| 142 | fmt.Println(sid.MachineID) // 0 |
| 143 | fmt.Println(sid.Sequence) // 0 |
| 144 | fmt.Println(sid.Timestamp) // 31536000000 |
| 145 | fmt.Println(sid.GenerateTime()) // 2009-11-10 23:00:00 +0000 UTC |
| 146 | } |
| 147 | ``` |
| 148 | |
| 149 | ## Best practices |
| 150 | |
| 151 | > ⚠️⚠️ All SetXXX method is thread-unsafe, recommended you call him in the main function. |
| 152 | |
| 153 | ```go |
| 154 | package main |
| 155 | |
| 156 | import ( |
| 157 | "fmt" |
| 158 | "net/http" |
| 159 | "time" |
| 160 | |
| 161 | "github.com/kankanreno/go-snowflake" |
| 162 | ) |
| 163 | |
| 164 | func main() { |
| 165 | snowflake.SetMachineID(1) // change to your machineID |
| 166 | snowflake.SetStartTime(time.Date(2014, 9, 1, 0, 0, 0, 0, time.UTC)) |
| 167 | |
| 168 | http.HandleFunc("/order", submitOrder) |
| 169 | http.ListenAndServe(":8090", nil) |
| 170 | } |
| 171 | |
| 172 | func submitOrder(w http.ResponseWriter, req *http.Request) { |
| 173 | orderId := snowflake.ID() |
| 174 | // save order |
| 175 | } |
| 176 | ``` |
| 177 | |
| 178 | ## Advanced |
| 179 | |
| 180 | Custom sequence resolver. you can customize the sequence-number resolver by following way: |
| 181 | |
| 182 | ```go |
| 183 | package main |
| 184 | |
| 185 | import ( |
| 186 | "fmt" |
| 187 | "time" |
| 188 | |
| 189 | "github.com/kankanreno/go-snowflake" |
| 190 | ) |
| 191 | |
| 192 | func yourSequenceNumber(ms int64) (uint16, error) { |
| 193 | |
| 194 | } |
| 195 | |
| 196 | // usage |
| 197 | |
| 198 | snowflake.SetSequenceResolver(yourSequenceNumber) |
| 199 | snowflake.ID() |
| 200 | ``` |
| 201 | |
| 202 | ## License |
| 203 | |
| 204 | MIT |