diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e2201d1..d8162e2 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -12,13 +12,13 @@ jobs: runs-on: ubuntu-latest steps: - name: Install Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: - go-version: 1.15.x + go-version: 1.17 - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Run linters - uses: golangci/golangci-lint-action@v2 + uses: golangci/golangci-lint-action@v3 with: version: v1.29 diff --git a/atomic_resolver.go b/atomic_resolver.go index ff337c2..35d385a 100644 --- a/atomic_resolver.go +++ b/atomic_resolver.go @@ -18,7 +18,7 @@ func AtomicResolver(ms int64) (uint16, error) { } if last == ms { - seq = MaxSequence & (localSeq + 1) + seq = uint32(MaxSequence) & (localSeq + 1) if seq == 0 { return MaxSequence, nil } diff --git a/snowflake.go b/snowflake.go index 9b5eb19..0153806 100644 --- a/snowflake.go +++ b/snowflake.go @@ -7,12 +7,12 @@ import ( // These constants are the bit lengths of snowflake ID parts. const ( - TimestampLength = 41 - MachineIDLength = 10 - SequenceLength = 12 - MaxSequence = 1< MaxTimestamp { - return 0, errors.New("The maximum life cycle of the snowflake algorithm is 2^41-1(millis), please check starttime") + df := elapsedTime(c, startTime) + if df < 0 || uint64(df) > MaxTimestamp { + return 0, errors.New("the maximum life cycle of the snowflake algorithm is 2^41-1(millis), please check start-time") } - id := uint64((df << timestampMoveLength) | (machineID << machineIDMoveLength) | int(seq)) + id := (uint64(df) << uint64(timestampMoveLength)) | (machineID << uint64(machineIDMoveLength)) | uint64(seq) return id, nil } @@ -74,7 +74,7 @@ func NextID() (uint64, error) { // // It will panic when: // s IsZero -// s > current millisecond +// s > current millisecond, // current millisecond - s > 2^41(69 years). // This function is thread-unsafe, recommended you call him in the main function. func SetStartTime(s time.Time) { @@ -88,25 +88,25 @@ func SetStartTime(s time.Time) { panic("The s cannot be greater than the current millisecond") } - // Because s must after now, so the `df` not < 0. + // since we check the current millisecond is greater than s, so we don't need to check the overflow. df := elapsedTime(currentMillis(), s) - if df > MaxTimestamp { + if uint64(df) > MaxTimestamp { panic("The maximum life cycle of the snowflake algorithm is 69 years") } startTime = s } -// SetMachineID specify the machine ID. It will panic when machineid > max limit for 2^10-1. +// SetMachineID specify the machine ID. It will panic when machined > max limit for 2^10-1. // This function is thread-unsafe, recommended you call him in the main function. func SetMachineID(m uint16) { if m > MaxMachineID { - panic("The machineid cannot be greater than 1023") + panic("The machineID cannot be greater than 1023") } - machineID = int(m) + machineID = uint64(m) } -// SetSequenceResolver set an custom sequence resolver. +// SetSequenceResolver set a custom sequence resolver. // This function is thread-unsafe, recommended you call him in the main function. func SetSequenceResolver(seq SequenceResolver) { if seq != nil { @@ -126,20 +126,20 @@ type SID struct { func (id *SID) GenerateTime() time.Time { ms := startTime.UTC().UnixNano()/1e6 + int64(id.Timestamp) - return time.Unix(0, (ms * int64(time.Millisecond))).UTC() + return time.Unix(0, ms*int64(time.Millisecond)).UTC() } // ParseID parse snowflake it to SID struct. func ParseID(id uint64) SID { - time := id >> (SequenceLength + MachineIDLength) - sequence := id & MaxSequence - machineID := (id & (MaxMachineID << SequenceLength)) >> SequenceLength + t := id >> uint64(SequenceLength+MachineIDLength) + sequence := id & uint64(MaxSequence) + mID := (id & (uint64(MaxMachineID) << SequenceLength)) >> SequenceLength return SID{ ID: id, Sequence: sequence, - MachineID: machineID, - Timestamp: time, + MachineID: mID, + Timestamp: t, } } @@ -163,8 +163,8 @@ func callSequenceResolver() SequenceResolver { return resolver } -func elapsedTime(nowms int64, s time.Time) int64 { - return nowms - s.UTC().UnixNano()/1e6 +func elapsedTime(noms int64, s time.Time) int64 { + return noms - s.UTC().UnixNano()/1e6 } // currentMillis get current millisecond. diff --git a/snowflake_test.go b/snowflake_test.go index 691632b..8be7592 100644 --- a/snowflake_test.go +++ b/snowflake_test.go @@ -156,8 +156,8 @@ func TestSetMachineID(t *testing.T) { defer func() { if err := recover(); err == nil { tt.Error("Should throw a error") - } else if err.(string) != "The machineid cannot be greater than 1023" { - tt.Error("The error message should be eq 「The machineid cannot be greater than 1023」") + } else if err.(string) != "The machineID cannot be greater than 1023" { + tt.Error("The error message should be eq 「The machineID cannot be greater than 1023」") } }()