-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
243 lines (215 loc) · 6.57 KB
/
Copy pathconfig.go
File metadata and controls
243 lines (215 loc) · 6.57 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package senna
import (
"crypto/tls"
"time"
"github.com/redis/go-redis/v9"
)
// Config configures shared Senna settings used by clients and workers.
type Config struct {
Redis RedisConfig
Namespace string
Worker WorkerSettings
Client ClientSettings
Encryption EncryptionSettings
}
// RedisConfig configures the Redis connection used by Senna.
type RedisConfig struct {
Addr string
Password string
DB int
PoolSize int
MinIdleConns int
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
TLSConfig *tls.Config
}
// Options converts the RedisConfig into go-redis client options.
func (c RedisConfig) Options() *redis.Options {
opts := &redis.Options{
Addr: c.Addr,
Password: c.Password,
DB: c.DB,
TLSConfig: c.TLSConfig,
ContextTimeoutEnabled: true,
}
if c.PoolSize > 0 {
opts.PoolSize = c.PoolSize
}
if c.MinIdleConns > 0 {
opts.MinIdleConns = c.MinIdleConns
}
if c.DialTimeout > 0 {
opts.DialTimeout = c.DialTimeout
}
if c.ReadTimeout > 0 {
opts.ReadTimeout = c.ReadTimeout
}
if c.WriteTimeout > 0 {
opts.WriteTimeout = c.WriteTimeout
}
return opts
}
// WorkerSettings configures worker runtime behavior.
type WorkerSettings struct {
Concurrency int
Queues []QueueConfig
ShutdownTimeout time.Duration
PollInterval time.Duration
BlockTimeout time.Duration
ScheduledPollInterval time.Duration
HeartbeatRate time.Duration
ReaperOperationTimeout time.Duration
ReaperInterval time.Duration
SequentialLockTTL time.Duration
SequentialLockRenewInterval time.Duration
IterableMaxRuntime time.Duration
PeriodicPollInterval time.Duration
PeriodicEnabled bool
StrictPriority bool // If true, always process higher priority queues first (can starve lower priority)
}
const (
// DefaultQueueName is the default queue used by clients and workers.
DefaultQueueName = "default"
// DefaultRetryCount is the default retry count assigned to new jobs.
DefaultRetryCount = 25
// DefaultBulkChunkSize bounds bulk enqueue Redis command size by default.
DefaultBulkChunkSize = 1000
)
// DefaultWorkerSettings returns the default worker settings.
func DefaultWorkerSettings() WorkerSettings {
return WorkerSettings{
Concurrency: 10,
Queues: []QueueConfig{{Name: DefaultQueueName, Priority: 1}},
ShutdownTimeout: 30 * time.Second,
PollInterval: 100 * time.Millisecond,
BlockTimeout: 2 * time.Second,
ScheduledPollInterval: 5 * time.Second,
HeartbeatRate: 5 * time.Second,
ReaperOperationTimeout: 5 * time.Second,
ReaperInterval: 30 * time.Second,
SequentialLockTTL: 30 * time.Second,
SequentialLockRenewInterval: 10 * time.Second,
IterableMaxRuntime: 30 * time.Second,
PeriodicPollInterval: 15 * time.Second,
}
}
// QueueConfig configures a worker queue and its scheduling behavior.
type QueueConfig struct {
Name string
Priority int
Paused bool
Sequential bool // Only one worker globally processes this queue at a time
}
// ClientSettings configures enqueue defaults for a client.
type ClientSettings struct {
DefaultQueue string
DefaultRetry int
BulkChunkSize int
}
// DefaultClientSettings returns the default client settings.
func DefaultClientSettings() ClientSettings {
return ClientSettings{
DefaultQueue: DefaultQueueName,
DefaultRetry: DefaultRetryCount,
BulkChunkSize: DefaultBulkChunkSize,
}
}
// EncryptionSettings configures job argument encryption.
type EncryptionSettings struct {
Enabled bool
Key []byte
}
// Option mutates a Config.
type Option func(*Config)
// WithNamespace sets the Redis key namespace.
func WithNamespace(ns string) Option {
return func(c *Config) {
c.Namespace = ns
}
}
// WithConcurrency sets the worker concurrency.
func WithConcurrency(n int) Option {
return func(c *Config) {
c.Worker.Concurrency = n
}
}
// WithQueues sets the worker queue configuration.
func WithQueues(queues ...QueueConfig) Option {
return func(c *Config) {
c.Worker.Queues = queues
}
}
// WithShutdownTimeout sets the worker shutdown timeout.
func WithShutdownTimeout(d time.Duration) Option {
return func(c *Config) {
c.Worker.ShutdownTimeout = d
}
}
// WithReaperOperationTimeout sets the per-Redis-command timeout for orphan recovery.
func WithReaperOperationTimeout(d time.Duration) Option {
return func(c *Config) {
c.Worker.ReaperOperationTimeout = d
}
}
// WithReaperInterval sets how often workers scan for orphaned in-flight jobs.
func WithReaperInterval(d time.Duration) Option {
return func(c *Config) {
c.Worker.ReaperInterval = d
}
}
// WithSequentialLockTTL sets how long a sequential queue lock remains valid.
func WithSequentialLockTTL(d time.Duration) Option {
return func(c *Config) {
c.Worker.SequentialLockTTL = d
}
}
// WithSequentialLockRenewInterval sets how often workers renew held sequential queue locks.
func WithSequentialLockRenewInterval(d time.Duration) Option {
return func(c *Config) {
c.Worker.SequentialLockRenewInterval = d
}
}
// WithIterableMaxRuntime sets how long an iterable job can run before yielding.
func WithIterableMaxRuntime(d time.Duration) Option {
return func(c *Config) {
c.Worker.IterableMaxRuntime = d
}
}
// WithPeriodicPollInterval sets how often workers check periodic job schedules.
func WithPeriodicPollInterval(d time.Duration) Option {
return func(c *Config) {
c.Worker.PeriodicPollInterval = d
}
}
// WithBlockTimeout sets how long a worker can block in Redis waiting for jobs.
func WithBlockTimeout(d time.Duration) Option {
return func(c *Config) {
c.Worker.BlockTimeout = d
}
}
// WithEncryptionConfig enables encryption with the provided key.
func WithEncryptionConfig(key []byte) Option {
return func(c *Config) {
c.Encryption.Enabled = true
c.Encryption.Key = key
}
}
// WithPeriodicEnabled enables periodic job scheduling for workers.
func WithPeriodicEnabled() Option {
return func(c *Config) {
c.Worker.PeriodicEnabled = true
}
}
// WithScheduledPollInterval sets how often a worker checks scheduled jobs.
func WithScheduledPollInterval(d time.Duration) Option {
return func(c *Config) {
c.Worker.ScheduledPollInterval = d
}
}
// WithStrictPriority makes workers always prefer higher-priority queues.
func WithStrictPriority() Option {
return func(c *Config) {
c.Worker.StrictPriority = true
}
}