1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00

更新ID生成器

This commit is contained in:
2023-10-26 14:42:23 +08:00
parent 6647f96978
commit 0d124c0b79
17 changed files with 1079 additions and 150 deletions

View File

@ -0,0 +1,60 @@
package store
import (
"sync"
"github.com/charlienet/go-mixed/mathx"
)
type memStore struct {
mu sync.Mutex
machine int64
current int64
}
func NewMemStore(machineCode int64) *memStore {
return &memStore{machine: machineCode}
}
func (s *memStore) UpdateMachineCode(max int64) (int64, error) {
return s.machine, nil
}
func (s *memStore) MachineCode() int64 {
return s.machine
}
func (s *memStore) Assign(min, max, step int64) (*Segment, error) {
s.mu.Lock()
defer s.mu.Unlock()
step = mathx.Min(step, max)
start := mathx.Max(s.current, min)
end := start + step
reback := false
if start >= max {
start = min
end = step
s.current = end
reback = true
}
if end > max {
end = max
s.current = end
}
s.current = end
return &Segment{
start: start,
current: start,
end: end,
reback: reback,
}, nil
}
func (s *memStore) Close() {
}