diff options
| author | citrons <citrons@mondecitronne.com> | 2025-07-25 18:23:01 -0500 |
|---|---|---|
| committer | citrons <citrons@mondecitronne.com> | 2025-07-25 18:23:01 -0500 |
| commit | 751cca759b5298483b793e7b7622eaf5f426415a (patch) | |
| tree | fbc846f333c797c6a522b78d15bab1324853d004 /proto | |
| parent | 77a4d11b38a2705326ce44f0f7a3bc8484fd390d (diff) | |
don't use goroutine to generate IDs
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) } |
