-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_process_driver.go
210 lines (191 loc) · 5.12 KB
/
in_process_driver.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package queue
import (
"container/heap"
"context"
"fmt"
"sync"
"time"
)
// An item is something we manage in a priority queue.
type item struct {
Job *PersistedJob // The value JobFrom the item; arbitrary.
priority time.Time // The priority JobFrom the item in the queue.
// The index is needed by update and is maintained by the heap.Interface methods.
index int // The index JobFrom the item in the heap.
}
// A priorityQueue implements heap.Interface and holds *PersistedJob.
type priorityQueue []*item
func (pq priorityQueue) Len() int { return len(pq) }
func (pq priorityQueue) Less(i, j int) bool {
// We want Pop to give us the highest, not lowest, priority so we use greater than here.
return pq[i].priority.Before(pq[j].priority)
}
func (pq priorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
func (pq *priorityQueue) Push(x interface{}) {
n := len(*pq)
item := x.(*item)
item.index = n
*pq = append(*pq, item)
}
func (pq *priorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
old[n-1] = nil // avoid memory leak
item.index = -1 // for safety
*pq = old[0 : n-1]
return item
}
// InProcessDriver is a test replacement for redis driver. It doesn't persist your Job in any way,
// so not suitable for production use.
type InProcessDriver struct {
popInterval time.Duration
mutex sync.Mutex
delayed *priorityQueue
waiting chan *PersistedJob
reserved map[*PersistedJob]time.Time
failed map[*PersistedJob]struct{}
timeout map[*PersistedJob]struct{}
}
// NewInProcessDriverWithPopInterval creates an *InProcessDriver for testing
func NewInProcessDriver() *InProcessDriver {
delayed := make(priorityQueue, 0, 10)
return &InProcessDriver{
popInterval: time.Second,
delayed: &delayed,
reserved: make(map[*PersistedJob]time.Time),
waiting: make(chan *PersistedJob, 1000),
failed: make(map[*PersistedJob]struct{}),
timeout: make(map[*PersistedJob]struct{}),
}
}
// NewInProcessDriverWithPopInterval creates an *InProcessDriver with an pop interval.
func NewInProcessDriverWithPopInterval(duration time.Duration) *InProcessDriver {
delayed := make(priorityQueue, 0, 10)
return &InProcessDriver{
popInterval: duration,
delayed: &delayed,
reserved: make(map[*PersistedJob]time.Time),
waiting: make(chan *PersistedJob, 1000),
failed: make(map[*PersistedJob]struct{}),
timeout: make(map[*PersistedJob]struct{}),
}
}
func (i *InProcessDriver) Push(ctx context.Context, message *PersistedJob, delay time.Duration) error {
if delay > 0 {
i.mutex.Lock()
heap.Push(i.delayed, &item{
Job: message,
priority: time.Now().Add(delay),
})
i.mutex.Unlock()
return nil
}
select {
case i.waiting <- message:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (i *InProcessDriver) Pop(ctx context.Context) (*PersistedJob, error) {
i.mutex.Lock()
for {
if len(*i.delayed) == 0 {
break
}
top := heap.Pop(i.delayed).(*item)
if top.priority.After(time.Now()) {
heap.Push(i.delayed, top)
break
}
i.waiting <- top.Job
}
for k := range i.reserved {
if i.reserved[k].Before(time.Now()) {
i.timeout[k] = struct{}{}
delete(i.reserved, k)
}
}
i.mutex.Unlock()
select {
case message := <-i.waiting:
i.reserved[message] = time.Now().Add(message.HandleTimeout)
return message, nil
case <-time.After(i.popInterval):
return nil, ErrEmpty
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (i *InProcessDriver) Ack(ctx context.Context, message *PersistedJob) error {
i.mutex.Lock()
defer i.mutex.Unlock()
delete(i.reserved, message)
return nil
}
func (i *InProcessDriver) Fail(ctx context.Context, message *PersistedJob) error {
i.mutex.Lock()
defer i.mutex.Unlock()
delete(i.reserved, message)
i.failed[message] = struct{}{}
return nil
}
func (i *InProcessDriver) Reload(ctx context.Context, channel string) (int64, error) {
i.mutex.Lock()
defer i.mutex.Unlock()
var j int64
if channel == "failed" {
for k := range i.failed {
delete(i.failed, k)
i.waiting <- k
j++
}
return j, nil
}
if channel == "timeout" {
for k := range i.timeout {
delete(i.timeout, k)
i.waiting <- k
j++
}
return j, nil
}
return 0, fmt.Errorf("unsupported channel %s", channel)
}
func (i *InProcessDriver) Flush(ctx context.Context, channel string) error {
i.mutex.Lock()
defer i.mutex.Unlock()
if channel == "failed" {
i.failed = make(map[*PersistedJob]struct{})
}
if channel == "timeout" {
i.timeout = make(map[*PersistedJob]struct{})
}
return nil
}
func (i *InProcessDriver) Info(ctx context.Context) (QueueInfo, error) {
i.mutex.Lock()
defer i.mutex.Unlock()
return QueueInfo{
Waiting: int64(len(i.waiting)),
Delayed: int64(len(*i.delayed)),
Timeout: int64(len(i.timeout)),
Failed: int64(len(i.failed)),
}, nil
}
func (i *InProcessDriver) Retry(ctx context.Context, message *PersistedJob) error {
i.mutex.Lock()
defer i.mutex.Unlock()
delete(i.reserved, message)
newBackOff := getRetryDuration(message.Backoff)
heap.Push(i.delayed, &item{
Job: message,
priority: time.Now().Add(newBackOff),
})
return nil
}