-
Notifications
You must be signed in to change notification settings - Fork 36
/
options.go
168 lines (134 loc) · 4.04 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
package workers
import (
"crypto/tls"
"errors"
"log"
"os"
"strings"
"time"
"github.com/digitalocean/go-workers2/storage"
"github.com/go-redis/redis/v8"
)
const (
defaultHeartbeatInterval = 5 * time.Second
defaultHeartbeatTTL = 60 * time.Second
)
// Options contains the set of configuration options for a manager and/or producer
type Options struct {
ProcessID string
Namespace string
PollInterval time.Duration
Database int
Password string
PoolSize int
// Provide one of ServerAddr or (SentinelAddrs + RedisMasterName)
ServerAddr string
SentinelAddrs string
RedisMasterName string
RedisTLSConfig *tls.Config
// Optional display name used when displaying manager stats
ManagerDisplayName string
ManagerStartInactive bool
// Define Heartbeat to enable heartbeat
Heartbeat *HeartbeatOptions
// Log
Logger *log.Logger
client *redis.Client
store storage.Store
}
func (o *Options) Client() *redis.Client {
return o.client
}
type HeartbeatOptions struct {
// Optional heartbeat interval config
Interval time.Duration
// redis eviction ttl config
HeartbeatTTL time.Duration
PrioritizedManager *PrioritizedManagerOptions
}
type PrioritizedManagerOptions struct {
ManagerPriority int
TotalActiveManagers int
}
func processOptions(options Options) (Options, error) {
options, err := validateGeneralOptions(options)
if err != nil {
return Options{}, err
}
//redis options
if options.PoolSize == 0 {
options.PoolSize = 1
}
redisIdleTimeout := 240 * time.Second
if options.ServerAddr != "" {
options.client = redis.NewClient(&redis.Options{
IdleTimeout: redisIdleTimeout,
Password: options.Password,
DB: options.Database,
PoolSize: options.PoolSize,
Addr: options.ServerAddr,
TLSConfig: options.RedisTLSConfig,
})
} else if options.SentinelAddrs != "" {
if options.RedisMasterName == "" {
return Options{}, errors.New("Sentinel configuration requires a master name")
}
options.client = redis.NewFailoverClient(&redis.FailoverOptions{
IdleTimeout: redisIdleTimeout,
Password: options.Password,
DB: options.Database,
PoolSize: options.PoolSize,
SentinelAddrs: strings.Split(options.SentinelAddrs, ","),
MasterName: options.RedisMasterName,
TLSConfig: options.RedisTLSConfig,
})
} else {
return Options{}, errors.New("Options requires either the Server or Sentinels option")
}
if options.Logger == nil {
options.Logger = log.New(os.Stdout, "go-workers2: ", log.Ldate|log.Lmicroseconds)
}
redisStore := storage.NewRedisStore(options.Namespace, options.client, options.Logger)
options.store = redisStore
if options.Heartbeat != nil {
if options.Heartbeat.Interval <= 0 {
options.Heartbeat.Interval = defaultHeartbeatInterval
}
if options.Heartbeat.HeartbeatTTL <= 0 {
options.Heartbeat.HeartbeatTTL = defaultHeartbeatTTL
}
}
return options, nil
}
func processOptionsWithRedisClient(options Options, client *redis.Client) (Options, error) {
options, err := validateGeneralOptions(options)
if err != nil {
return Options{}, err
}
if client == nil {
return Options{}, errors.New("redis client is nil; Redis client is not configured")
}
options.client = client
if options.Logger == nil {
options.Logger = log.New(os.Stdout, "go-workers2: ", log.Ldate|log.Lmicroseconds)
}
redisStore := storage.NewRedisStore(options.Namespace, options.client, options.Logger)
options.store = redisStore
return options, nil
}
func validateGeneralOptions(options Options) (Options, error) {
if options.ProcessID == "" {
return Options{}, errors.New("options requires a ProcessID, which uniquely identifies this instance")
}
if options.Namespace != "" {
options.Namespace += ":"
}
if options.PollInterval <= 0 {
options.PollInterval = 15 * time.Second
}
if options.Heartbeat != nil &&
options.Heartbeat.Interval >= options.Heartbeat.HeartbeatTTL {
return Options{}, errors.New("invalid heartbeat configuration, heartbeat interval longer than or equal to heartbeat tll")
}
return options, nil
}