1package spent
2
3import (
4 "sync"
5 "time"
6)
7
8type Set struct {
9 mu sync.Mutex
10 m map[string]time.Time
11}
12
13func New() *Set {
14 return &Set{m: map[string]time.Time{}}
15}
16
17func (s *Set) Spend(key string, until, now time.Time) bool {
18 s.mu.Lock()
19 defer s.mu.Unlock()
20 for k, exp := range s.m {
21 if now.After(exp) {
22 delete(s.m, k)
23 }
24 }
25 if _, used := s.m[key]; used {
26 return false
27 }
28 s.m[key] = until
29 return true
30}
31
32func (s *Set) Len() int {
33 s.mu.Lock()
34 defer s.mu.Unlock()
35 return len(s.m)
36}