Skip to content
File

Blob: kv/internal/lease/token.go

go22 lines
1package lease
2 
3import (
4 "math"
5 "time"
6 
7 "go.miragespace.co/specter/spec/chord"
8)
9 
10// Next returns a positive UnixNano expiry that advances the previous token.
11// Providers pass zero for acquisition and retain their atomic ownership checks.
12func Next(now time.Time, ttl time.Duration, previous uint64) (uint64, error) {
13 nanos := now.UnixNano()
14 if nanos < 0 || ttl < time.Second || int64(ttl) > math.MaxInt64-nanos {
15 return 0, chord.ErrKVLeaseInvalidTTL
16 }
17 if previous >= math.MaxInt64 {
18 return 0, chord.ErrKVLeaseExpired
19 }
20 return max(uint64(nanos+int64(ttl)), previous+1), nil
21}