a329d4906b
- 方案文档: AVCC 体系建设、IPTV TCS 需求(0-req)/PRD(1-prd)/任务(2-task)/二三四期任务 - tcs-iptv: Go 后端(哈希SDK/MA码生成/可信数据空间mock/业务编排/HTTP API+HMAC鉴权) - web-console: React+AntD 监管大屏(角色工作台/全流程演示/监管片库) - 一剧一码+集级哈希, 集级下架/恢复, 全栈测试通过
37 lines
936 B
Go
37 lines
936 B
Go
package macode
|
|
|
|
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)
|