MCPcopy
hub / github.com/bwmarrin/snowflake / Generate

Method Generate

snowflake.go:135–162  ·  view source on GitHub ↗

Generate creates and returns a unique snowflake ID To help guarantee uniqueness - Make sure your system is keeping accurate system time - Make sure you never have multiple nodes running with the same node ID

()

Source from the content-addressed store, hash-verified

133// - Make sure your system is keeping accurate system time
134// - Make sure you never have multiple nodes running with the same node ID
135func (n *Node) Generate() ID {
136
137 n.mu.Lock()
138
139 now := time.Since(n.epoch).Nanoseconds() / 1000000
140
141 if now == n.time {
142 n.step = (n.step + 1) & n.stepMask
143
144 if n.step == 0 {
145 for now <= n.time {
146 now = time.Since(n.epoch).Nanoseconds() / 1000000
147 }
148 }
149 } else {
150 n.step = 0
151 }
152
153 n.time = now
154
155 r := ID((now)<<n.timeShift |
156 (n.node << n.nodeShift) |
157 (n.step),
158 )
159
160 n.mu.Unlock()
161 return r
162}
163
164// Int64 returns an int64 of the snowflake ID
165func (f ID) Int64() int64 {

Callers 15

TestGenerateDuplicateIDFunction · 0.95
TestRaceFunction · 0.95
TestPrintAllFunction · 0.95
TestInt64Function · 0.95
TestStringFunction · 0.95
TestBase2Function · 0.95
TestBase32Function · 0.95
TestBase36Function · 0.95
TestBase58Function · 0.95
TestBase64Function · 0.95
TestBytesFunction · 0.95
TestIntBytesFunction · 0.95

Calls 1

IDTypeAlias · 0.85

Tested by 15

TestGenerateDuplicateIDFunction · 0.76
TestRaceFunction · 0.76
TestPrintAllFunction · 0.76
TestInt64Function · 0.76
TestStringFunction · 0.76
TestBase2Function · 0.76
TestBase32Function · 0.76
TestBase36Function · 0.76
TestBase58Function · 0.76
TestBase64Function · 0.76
TestBytesFunction · 0.76
TestIntBytesFunction · 0.76