Files
MAcode/tcs-iptv/internal/cccode/store_memory.go
T

37 lines
936 B
Go

package cccode
import "sync"
// MemoryStore 是 AllocationStore 的内存实现(MVP / 测试)。
// 生产环境应替换为 PostgreSQL 行锁或 Redis INCR,保证多实例下原子且持久。
type MemoryStore struct {
mu sync.Mutex
cursors map[string]uint64 // segmentKey -> 已分配的最大序列
}
// NewMemoryStore 创建内存分配存储。
func NewMemoryStore() *MemoryStore {
return &MemoryStore{cursors: make(map[string]uint64)}
}
// Next 原子返回下一个序列:首次取 start,之后递增;超过 end 返回耗尽错误。
func (s *MemoryStore) Next(segmentKey string, start, end uint64) (uint64, error) {
s.mu.Lock()
defer s.mu.Unlock()
cur, ok := s.cursors[segmentKey]
var next uint64
if !ok {
next = start
} else {
next = cur + 1
}
if next > end {
return 0, ErrSegmentExhausted
}
s.cursors[segmentKey] = next
return next, nil
}
var _ AllocationStore = (*MemoryStore)(nil)