/
strategy.go
61 lines (47 loc) · 1.06 KB
/
strategy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package data
import (
"sync"
"time"
"hawx.me/code/relme-auth/internal/random"
)
type StrategyStore struct {
expiry int64
mu sync.Mutex
inProgress map[string]*expiringItem
}
type expiringItem struct {
createdAt int64
value interface{}
}
func Strategy(name string) (*StrategyStore, error) {
return &StrategyStore{
inProgress: map[string]*expiringItem{},
expiry: 60,
}, nil
}
func (s *StrategyStore) Insert(value interface{}) (state string, err error) {
state, err = random.String(64)
if err != nil {
return
}
return state, s.Set(state, value)
}
func (s *StrategyStore) Set(key string, value interface{}) error {
s.mu.Lock()
s.inProgress[key] = &expiringItem{createdAt: time.Now().Unix(), value: value}
s.mu.Unlock()
return nil
}
func (s *StrategyStore) Claim(key string) (value interface{}, ok bool) {
s.mu.Lock()
defer s.mu.Unlock()
item, ok := s.inProgress[key]
if !ok {
return "", false
}
delete(s.inProgress, key)
if time.Now().Unix()-item.createdAt > s.expiry {
return "", false
}
return item.value, true
}