-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
172 lines (137 loc) · 3.36 KB
/
store.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package memory
import (
"context"
"sync"
"time"
"github.com/code-payments/code-server/pkg/pointer"
"github.com/code-payments/code-server/pkg/code/data/webhook"
)
type store struct {
mu sync.Mutex
last uint64
records []*webhook.Record
}
// New returns a new in memory webhook.Store
func New() webhook.Store {
return &store{}
}
// Put implements webhook.Store.Put
func (s *store) Put(_ context.Context, data *webhook.Record) error {
if err := data.Validate(); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
s.last++
if item := s.find(data); item != nil {
return webhook.ErrAlreadyExists
} else {
if data.Id == 0 {
data.Id = s.last
}
data.CreatedAt = time.Now()
cloned := data.Clone()
s.records = append(s.records, &cloned)
}
return nil
}
// Update implements webhook.Store.Update
func (s *store) Update(_ context.Context, data *webhook.Record) error {
if err := data.Validate(); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
s.last++
if item := s.find(data); item != nil {
item.Attempts = data.Attempts
item.State = data.State
item.NextAttemptAt = pointer.TimeCopy(data.NextAttemptAt)
item.CopyTo(data)
} else {
return webhook.ErrNotFound
}
return nil
}
// Get implements webhook.Store.Get
func (s *store) Get(_ context.Context, webhookId string) (*webhook.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
item := s.findByWebhookId(webhookId)
if item == nil {
return nil, webhook.ErrNotFound
}
cloned := item.Clone()
return &cloned, nil
}
// CountByState implements webhook.Store.CountByState
func (s *store) CountByState(_ context.Context, state webhook.State) (uint64, error) {
s.mu.Lock()
defer s.mu.Unlock()
items := s.findByState(state)
return uint64(len(items)), nil
}
// GetAllPendingReadyToSend implements webhook.Store.GetAllPendingReadyToSend
func (s *store) GetAllPendingReadyToSend(_ context.Context, limit uint64) ([]*webhook.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
items := s.findByState(webhook.StatePending)
items = s.filterNextAttemptBeforeOrAt(items, time.Now())
if len(items) == 0 {
return nil, webhook.ErrNotFound
} else if uint64(len(items)) > limit {
items = items[:limit]
}
return cloneSlice(items), nil
}
func (s *store) find(data *webhook.Record) *webhook.Record {
for _, item := range s.records {
if item.Id == data.Id {
return item
}
if item.WebhookId == data.WebhookId {
return item
}
}
return nil
}
func (s *store) findByWebhookId(webhookId string) *webhook.Record {
for _, item := range s.records {
if item.WebhookId == webhookId {
return item
}
}
return nil
}
func (s *store) findByState(state webhook.State) []*webhook.Record {
var res []*webhook.Record
for _, item := range s.records {
if item.State == state {
res = append(res, item)
}
}
return res
}
func (s *store) filterNextAttemptBeforeOrAt(items []*webhook.Record, ts time.Time) []*webhook.Record {
var res []*webhook.Record
for _, item := range items {
if item.NextAttemptAt != nil && item.NextAttemptAt.Compare(ts) <= 0 {
res = append(res, item)
}
}
return res
}
func (s *store) reset() {
s.mu.Lock()
defer s.mu.Unlock()
s.last = 0
s.records = nil
}
func cloneSlice(items []*webhook.Record) []*webhook.Record {
var res []*webhook.Record
for _, item := range items {
cloned := item.Clone()
res = append(res, &cloned)
}
return res
}