forked from vmihailenco/taskq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
173 lines (143 loc) · 4.01 KB
/
options.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
package msgqueue
import (
"runtime"
"time"
"github.com/go-redis/redis"
"github.com/go-redis/redis_rate"
"golang.org/x/time/rate"
)
type Redis interface {
Del(keys ...string) *redis.IntCmd
SetNX(key string, value interface{}, expiration time.Duration) *redis.BoolCmd
SAdd(key string, members ...interface{}) *redis.IntCmd
SMembers(key string) *redis.StringSliceCmd
Pipelined(func(pipe redis.Pipeliner) error) ([]redis.Cmder, error)
Eval(script string, keys []string, args ...interface{}) *redis.Cmd
EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd
ScriptExists(scripts ...string) *redis.BoolSliceCmd
ScriptLoad(script string) *redis.StringCmd
Publish(channel string, message interface{}) *redis.IntCmd
}
type Storage interface {
Exists(key string) bool
}
type redisStorage struct {
Redis
}
var _ Storage = (*redisStorage)(nil)
func (s redisStorage) Exists(key string) bool {
val, err := s.SetNX(key, "", 24*time.Hour).Result()
if err != nil {
return true
}
return !val
}
type RateLimiter interface {
AllowRate(name string, limit rate.Limit) (delay time.Duration, allow bool)
}
type Options struct {
// Queue name.
Name string
// Queue group name.
GroupName string
// Function called to process a message.
Handler interface{}
// Function called to process failed message.
FallbackHandler interface{}
// Maximum number of goroutines processing messages.
// Default is 32 * number of CPUs.
MaxWorkers int
// Global limit of concurrently running workers across all servers.
// Overrides MaxWorkers.
WorkerLimit int
// Maximum number of goroutines fetching messages.
// Default is 4 * number of CPUs.
MaxFetchers int
// Size of the buffer where reserved messages are stored.
// Default is 10 messages.
BufferSize int
// Number of messages reserved by a fetcher in the queue in one request.
// Default is 10 messages.
ReservationSize int
// Time after which the reserved message is returned to the queue.
ReservationTimeout time.Duration
// Time that a long polling receive call waits for a message to become
// available before returning an empty response.
// Default is 10 seconds.
WaitTimeout time.Duration
// Number of tries/releases after which the message fails permanently
// and is deleted.
RetryLimit int
// Minimum backoff time between retries.
MinBackoff time.Duration
// Maximum backoff time between retries.
MaxBackoff time.Duration
// Number of consecutive failures after which queue processing is paused.
// Default is 100 failures.
PauseErrorsThreshold int
// Processing rate limit.
RateLimit rate.Limit
// Redis client that is used for storing metadata.
Redis Redis
// Optional storage interface. The default is to use Redis.
Storage Storage
// Optional rate limiter interface. The default is to use Redis.
RateLimiter RateLimiter
inited bool
}
func (opt *Options) Init() {
if opt.inited {
return
}
opt.inited = true
if opt.GroupName == "" {
opt.GroupName = opt.Name
}
if opt.WorkerLimit > 0 {
opt.MaxWorkers = opt.WorkerLimit
}
if opt.MaxWorkers == 0 {
opt.MaxWorkers = 32 * runtime.NumCPU()
}
if opt.MaxFetchers == 0 {
opt.MaxFetchers = 4 * runtime.NumCPU()
}
if opt.BufferSize == 0 {
opt.BufferSize = 10
}
switch opt.PauseErrorsThreshold {
case -1:
opt.PauseErrorsThreshold = 0
case 0:
opt.PauseErrorsThreshold = 100
}
if opt.RateLimit == 0 {
opt.RateLimit = rate.Inf
}
if opt.ReservationSize == 0 {
opt.ReservationSize = 10
}
if opt.ReservationTimeout == 0 {
opt.ReservationTimeout = 300 * time.Second
}
if opt.WaitTimeout == 0 {
opt.WaitTimeout = 10 * time.Second
}
if opt.RetryLimit == 0 {
opt.RetryLimit = 10
}
if opt.MinBackoff == 0 {
opt.MinBackoff = 30 * time.Second
}
if opt.MaxBackoff == 0 {
opt.MaxBackoff = 12 * time.Hour
}
if opt.Storage == nil {
opt.Storage = redisStorage{opt.Redis}
}
if opt.RateLimit != rate.Inf && opt.RateLimiter == nil && opt.Redis != nil {
limiter := redis_rate.NewLimiter(opt.Redis)
limiter.Fallback = rate.NewLimiter(opt.RateLimit, 1)
opt.RateLimiter = limiter
}
}