package spent import ( "sync" "time" ) type Set struct { mu sync.Mutex m map[string]time.Time } func New() *Set { return &Set{m: map[string]time.Time{}} } func (s *Set) Spend(key string, until, now time.Time) bool { s.mu.Lock() defer s.mu.Unlock() for k, exp := range s.m { if now.After(exp) { delete(s.m, k) } } if _, used := s.m[key]; used { return false } s.m[key] = until return true } func (s *Set) Len() int { s.mu.Lock() defer s.mu.Unlock() return len(s.m) }