forked from vmihailenco/taskq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
139 lines (116 loc) · 3.17 KB
/
queue.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
package taskq
import (
"context"
"fmt"
"runtime"
"time"
"github.com/go-redis/redis_rate/v9"
)
type QueueOptions struct {
// Queue name.
Name string
// Minimum number of goroutines processing messages.
// Default is 1.
MinNumWorker int32
// Maximum number of goroutines processing messages.
// Default is 32 * number of CPUs.
MaxNumWorker int32
// Global limit of concurrently running workers across all servers.
// Overrides MaxNumWorker.
WorkerLimit int32
// Maximum number of goroutines fetching messages.
// Default is 16 * number of CPUs.
MaxNumFetcher int32
// 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.
// Default is 5 minutes.
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
// Size of the buffer where reserved messages are stored.
// Default is the same as ReservationSize.
BufferSize int
// Number of consecutive failures after which queue processing is paused.
// Default is 100 failures.
PauseErrorsThreshold int
// Processing rate limit.
RateLimit redis_rate.Limit
// Optional rate limiter. The default is to use Redis.
RateLimiter *redis_rate.Limiter
// Redis client that is used for storing metadata.
Redis Redis
// Optional storage interface. The default is to use Redis.
Storage Storage
// Optional message handler. The default is the global Tasks registry.
Handler Handler
inited bool
}
func (opt *QueueOptions) Init() {
if opt.inited {
return
}
opt.inited = true
if opt.Name == "" {
panic("QueueOptions.Name is required")
}
if opt.WorkerLimit > 0 {
opt.MinNumWorker = opt.WorkerLimit
opt.MaxNumWorker = opt.WorkerLimit
} else {
if opt.MinNumWorker == 0 {
opt.MinNumWorker = 1
}
if opt.MaxNumWorker == 0 {
opt.MaxNumWorker = 32 * int32(runtime.NumCPU())
}
}
if opt.MaxNumFetcher == 0 {
opt.MaxNumFetcher = 8 * int32(runtime.NumCPU())
}
switch opt.PauseErrorsThreshold {
case -1:
opt.PauseErrorsThreshold = 0
case 0:
opt.PauseErrorsThreshold = 100
}
if opt.ReservationSize == 0 {
opt.ReservationSize = 10
}
if opt.ReservationTimeout == 0 {
opt.ReservationTimeout = 5 * time.Minute
}
if opt.BufferSize == 0 {
opt.BufferSize = opt.ReservationSize
}
if opt.WaitTimeout == 0 {
opt.WaitTimeout = 10 * time.Second
}
if opt.Storage == nil {
opt.Storage = newRedisStorage(opt.Redis)
}
if !opt.RateLimit.IsZero() && opt.RateLimiter == nil && opt.Redis != nil {
opt.RateLimiter = redis_rate.NewLimiter(opt.Redis)
}
if opt.Handler == nil {
opt.Handler = &Tasks
}
}
//------------------------------------------------------------------------------
type Queue interface {
fmt.Stringer
Name() string
Options() *QueueOptions
Consumer() *Consumer
Len() (int, error)
Add(msg *Message) error
ReserveN(ctx context.Context, n int, waitTimeout time.Duration) ([]Message, error)
Release(msg *Message) error
Delete(msg *Message) error
Purge() error
Close() error
CloseTimeout(timeout time.Duration) error
}