| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package snowflake |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"time" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
const ( |
| 17 |
TimestampLength = 41 |
| 18 |
MachineIDLength = 6 |
| 19 |
SequenceLength = 6 |
| 20 |
|
| 21 |
MaxTimestamp = 1<<TimestampLength - 1 |
| 22 |
MaxMachineID = 1<<MachineIDLength - 1 |
| 23 |
MaxSequence = 1<<SequenceLength - 1 |
| 24 |
|
| 25 |
machineIDMoveLength = SequenceLength |
| 26 |
timestampMoveLength = MachineIDLength + SequenceLength |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
type SequenceResolver func(ms int64) (uint16, error) |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
var ( |
| 41 |
resolver SequenceResolver |
| 42 |
machineID = 0 |
| 43 |
startTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
func ID() uint64 { |
| 49 |
id, _ := NextID() |
| 50 |
return id |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
func NextID() (uint64, error) { |
| 56 |
c := currentMillis() |
| 57 |
seqResolver := callSequenceResolver() |
| 58 |
seq, err := seqResolver(c) |
| 59 |
|
| 60 |
if err != nil { |
| 61 |
return 0, err |
| 62 |
} |
| 63 |
|
| 64 |
for seq >= MaxSequence { |
| 65 |
c = waitForNextMillis(c) |
| 66 |
seq, err = seqResolver(c) |
| 67 |
if err != nil { |
| 68 |
return 0, err |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
df := int(elapsedTime(c, startTime)) |
| •73 |
if df < 0 || uint64(df) > uint64(MaxTimestamp) { |
| 74 |
return 0, errors.New("The maximum life cycle of the snowflake algorithm is 2^41(millis), please check starttime") |
| 75 |
} |
| 76 |
|
| 77 |
id := uint64((df << timestampMoveLength) | (machineID << machineIDMoveLength) | int(seq)) |
| 78 |
return id, nil |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
func SetStartTime(s time.Time) { |
| 91 |
s = s.UTC() |
| 92 |
|
| 93 |
if s.IsZero() { |
| 94 |
panic("The start time cannot be a zero value") |
| 95 |
} |
| 96 |
|
| 97 |
if s.After(time.Now().UTC()) { |
| 98 |
panic("The s cannot be greater than the current millisecond") |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
df := elapsedTime(currentMillis(), s) |
| 103 |
if df > MaxTimestamp { |
| 104 |
panic("The maximum life cycle of the snowflake algorithm is 69 years") |
| 105 |
} |
| 106 |
|
| 107 |
startTime = s |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
func SetMachineID(m int) { |
| 113 |
if m > MaxMachineID { |
| 114 |
panic(fmt.Sprintf("The machineid cannot be greater than %d", MaxMachineID)) |
| 115 |
} |
| 116 |
machineID = m |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
func SetSequenceResolver(seq SequenceResolver) { |
| 122 |
if seq != nil { |
| 123 |
resolver = seq |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
type SID struct { |
| 129 |
Sequence uint64 |
| 130 |
MachineID uint64 |
| 131 |
Timestamp uint64 |
| 132 |
ID uint64 |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
func (id *SID) GenerateTime() time.Time { |
| 137 |
ms := startTime.UTC().UnixNano()/1e6 + int64(id.Timestamp) |
| 138 |
|
| 139 |
return time.Unix(0, ms*int64(time.Millisecond)).UTC() |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
func ParseID(id uint64) SID { |
| 144 |
time := id >> (SequenceLength + MachineIDLength) |
| 145 |
sequence := id & MaxSequence |
| 146 |
machineID := (id & (MaxMachineID << SequenceLength)) >> SequenceLength |
| 147 |
|
| 148 |
return SID{ |
| 149 |
ID: id, |
| 150 |
Sequence: sequence, |
| 151 |
MachineID: machineID, |
| 152 |
Timestamp: time, |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
func waitForNextMillis(last int64) int64 { |
| 161 |
now := currentMillis() |
| 162 |
for now == last { |
| 163 |
now = currentMillis() |
| 164 |
} |
| 165 |
return now |
| 166 |
} |
| 167 |
|
| 168 |
func callSequenceResolver() SequenceResolver { |
| 169 |
if resolver == nil { |
| 170 |
return AtomicResolver |
| 171 |
} |
| 172 |
|
| 173 |
return resolver |
| 174 |
} |
| 175 |
|
| 176 |
func elapsedTime(nowms int64, s time.Time) int64 { |
| 177 |
return nowms - s.UTC().UnixNano()/1e6 |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
func currentMillis() int64 { |
| 182 |
return time.Now().UTC().UnixNano() / 1e6 |
| 183 |
} |