diff options
Diffstat (limited to 'proto')
| -rw-r--r-- | proto/id.go | 15 |
1 files changed, 4 insertions, 11 deletions
diff --git a/proto/id.go b/proto/id.go index 22bb395..ae74718 100644 --- a/proto/id.go +++ b/proto/id.go @@ -3,23 +3,16 @@ package proto import ( "time" "strconv" + "sync/atomic" ) -var counter = make(chan uint8) -func init() { - go func() { - var i uint8 - for i = 0; true; i++ { - counter <- i - } - }() -} - +var counter atomic.Uint32 var epoch = time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) func GenId() string { t := time.Now().UnixMilli() - epoch.UnixMilli() - id := uint64(t << 6) | uint64(<-counter & 63) + id := uint64(t << 6) | uint64(counter.Load() & 63) + counter.Add(1) return strconv.FormatUint(id, 36) } |
