-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocking.go
156 lines (126 loc) · 3.01 KB
/
blocking.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
package gopq
import (
"context"
"database/sql"
"errors"
"strings"
"time"
)
type tryDequeuer interface {
TryDequeueCtx(ctx context.Context) (Msg, error)
}
type ErrNoItemsWaiting struct{}
func (e *ErrNoItemsWaiting) Error() string {
return "no items waiting"
}
type ErrDBLocked struct{}
func (e *ErrDBLocked) Error() string {
return "database table is locked"
}
// A helper function to handle common dequeue errors.
func handleDequeueResult(id int64, item []byte, err error) (Msg, error) {
if err == sql.ErrNoRows {
return Msg{}, &ErrNoItemsWaiting{}
}
// On error on cancelled context
if err != nil {
return Msg{}, err
}
return Msg{
ID: id,
Item: item,
}, nil
}
// dequeueBlocking blocks until an item is available to dequeue, or the context is cancelled.
func dequeueBlocking(ctx context.Context, dequeuer tryDequeuer, pollInterval time.Duration, notifyChan chan struct{}) (Msg, error) {
for {
item, err := dequeuer.TryDequeueCtx(ctx)
if err == nil {
return item, nil
}
_, ok := err.(*ErrNoItemsWaiting)
if !ok {
return Msg{}, err
}
select {
case <-ctx.Done():
return Msg{}, context.Canceled
case <-time.After(pollInterval): // Continue
case <-notifyChan: // Continue
}
}
}
type tryEnqueuer interface {
TryEnqueueCtx(ctx context.Context, item []byte) error
}
func handleEnqueueResult(err error) error {
if err == sql.ErrNoRows {
return &ErrNoItemsWaiting{}
}
if strings.Contains(err.Error(), "database table is locked") {
return &ErrDBLocked{}
}
if err != nil {
return err
}
return nil
}
func enqueueBlocking(ctx context.Context, enqueuer tryEnqueuer, item []byte, pollInterval time.Duration) error {
for {
err := enqueuer.TryEnqueueCtx(ctx, item)
if err == nil {
return nil
}
if !errors.Is(err, &ErrDBLocked{}) {
return err
}
select {
case <-ctx.Done():
return context.Canceled
case <-time.After(pollInterval):
// Continue to next attempt
}
}
}
type tryAcker interface {
TryAckCtx(ctx context.Context, id int64) error
TryNackCtx(ctx context.Context, id int64) error
}
func ackBlocking(ctx context.Context, acker tryAcker, id int64, pollInterval time.Duration) error {
for {
err := acker.TryAckCtx(ctx, id)
if err == nil {
return nil
}
// We return on all errors except a locked DB
// which we just wait on by trying again.
if !strings.Contains(err.Error(), "database table is locked") {
return err
}
select {
case <-ctx.Done():
return context.Canceled
case <-time.After(pollInterval):
// Continue to next attempt
}
}
}
func nackBlocking(ctx context.Context, acker tryAcker, id int64, pollInterval time.Duration) error {
for {
err := acker.TryNackCtx(ctx, id)
if err == nil {
return nil
}
// We return on all errors except a locked DB
// which we just wait on by trying again.
if !strings.Contains(err.Error(), "database table is locked") {
return err
}
select {
case <-ctx.Done():
return context.Canceled
case <-time.After(pollInterval):
// Continue to next attempt
}
}
}