mirror of
https://github.com/godruoyi/go-snowflake.git
synced 2026-01-03 10:55:15 +00:00
Compare commits
8 Commits
v0.0.1-alp
...
v0.0.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0500e4b57b | ||
|
|
937cb414e9 | ||
|
|
495d927cce | ||
|
|
03bb06070a | ||
|
|
20079db81e | ||
|
|
056fce86b8 | ||
|
|
ad2188ad52 | ||
|
|
96659ed81e |
2
.github/FUNDING.yml
vendored
Normal file
2
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
# These are supported funding model platforms
|
||||
custom: ["https://images.godruoyi.com/wechat.png"]
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Godruoyi
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -2,13 +2,18 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/godruoyi/go-snowflake"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// set starttime and machineID for the first time if you wan't to use the default value
|
||||
snowflake.SetStartTime(time.Date(2021, 9, 1, 0, 0, 0, 0, time.UTC))
|
||||
snowflake.SetMachineID(snowflake.PrivateIPToMachineID()) // testing, not to be used in production
|
||||
|
||||
id := snowflake.ID()
|
||||
fmt.Println(id)
|
||||
fmt.Println(id) // 1537200202186752
|
||||
|
||||
sid := snowflake.ParseID(id)
|
||||
// SID {
|
||||
|
||||
@@ -36,7 +36,7 @@ So if you want use the snowflake algorithm to generate unique ID, You must ensur
|
||||
|
||||
Based on this, we created this package and integrated multiple sequence-number providers into it.
|
||||
|
||||
* AtomicResolver (base sync/atmoic)
|
||||
* AtomicResolver (base sync/atomic)
|
||||
|
||||
> Each provider only needs to ensure that the serial number generated in the same millisecond is different. You can get a unique ID.
|
||||
|
||||
@@ -54,7 +54,7 @@ Based on this, we created this package and integrated multiple sequence-number p
|
||||
$ go get github.com/godruoyi/go-snowflake
|
||||
```
|
||||
|
||||
## Useage
|
||||
## Usage
|
||||
|
||||
1. simple to use.
|
||||
|
||||
|
||||
12
snowflake.go
12
snowflake.go
@@ -61,12 +61,12 @@ func NextID() (uint64, error) {
|
||||
}
|
||||
}
|
||||
|
||||
df := int(elapsedTime(startTime))
|
||||
df := int(elapsedTime(c, startTime))
|
||||
if df < 0 || df > MaxTimestamp {
|
||||
return 0, errors.New("The maximum life cycle of the snowflake algorithm is 2^41-1(millis), please check starttime")
|
||||
}
|
||||
|
||||
id := uint64(df<<timestampMoveLength | machineID<<machineIDMoveLength | int(seq))
|
||||
id := uint64((df << timestampMoveLength) | (machineID << machineIDMoveLength) | int(seq))
|
||||
return id, nil
|
||||
}
|
||||
|
||||
@@ -84,12 +84,12 @@ func SetStartTime(s time.Time) {
|
||||
panic("The start time cannot be a zero value")
|
||||
}
|
||||
|
||||
if s.After(time.Now()) {
|
||||
if s.After(time.Now().UTC()) {
|
||||
panic("The s cannot be greater than the current millisecond")
|
||||
}
|
||||
|
||||
// Because s must after now, so the `df` not < 0.
|
||||
df := elapsedTime(s)
|
||||
df := elapsedTime(currentMillis(), s)
|
||||
if df > MaxTimestamp {
|
||||
panic("The maximum life cycle of the snowflake algorithm is 69 years")
|
||||
}
|
||||
@@ -163,8 +163,8 @@ func callSequenceResolver() SequenceResolver {
|
||||
return resolver
|
||||
}
|
||||
|
||||
func elapsedTime(s time.Time) int64 {
|
||||
return currentMillis() - s.UTC().UnixNano()/1e6
|
||||
func elapsedTime(nowms int64, s time.Time) int64 {
|
||||
return nowms - s.UTC().UnixNano()/1e6
|
||||
}
|
||||
|
||||
// currentMillis get current millisecond.
|
||||
|
||||
@@ -2,6 +2,7 @@ package snowflake_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -14,6 +15,48 @@ func TestID(t *testing.T) {
|
||||
if id <= 0 {
|
||||
t.Error("The snowflake should't < 0.")
|
||||
}
|
||||
|
||||
mp := make(map[uint64]bool)
|
||||
for i := 0; i < 100000; i++ {
|
||||
id, e := snowflake.NextID()
|
||||
if e != nil {
|
||||
t.Error(e)
|
||||
continue
|
||||
}
|
||||
if _, ok := mp[id]; ok {
|
||||
t.Error("ID should't repeat", id)
|
||||
break
|
||||
}
|
||||
mp[id] = true
|
||||
}
|
||||
}
|
||||
|
||||
func TestID_bitch(t *testing.T) {
|
||||
le := 100000
|
||||
ch := make(chan uint64, le)
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < le; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
id := snowflake.ID()
|
||||
ch <- id
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
close(ch)
|
||||
|
||||
mp := make(map[uint64]bool)
|
||||
for id := range ch {
|
||||
if _, ok := mp[id]; ok {
|
||||
t.Error("It should not be repeated")
|
||||
break
|
||||
}
|
||||
mp[id] = true
|
||||
}
|
||||
if len(mp) != le {
|
||||
t.Error("map length should be equal", le)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetStartTime(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user