-
Notifications
You must be signed in to change notification settings - Fork 73
/
config.go
275 lines (250 loc) · 7.01 KB
/
config.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package redisbp
import (
"fmt"
"time"
"github.com/go-redis/redis/v8"
)
// ClientConfig can be used to configure a redis-go "Client". See the docs for
// redis.Options in redis-go for details on what each value means and what its
// defaults are:
// https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#Options
//
// Can be deserialized from YAML.
//
// Examples:
//
// Minimal YAML:
//
// redis:
// url: redis://localhost:6379
//
// Full YAML:
//
// redis:
// url: redis://localhost:6379
// pool:
// size: 10
// minIdleConnections: 5
// maxConnectionAge: 1m
// timeout: 10s
// retries:
// max: 2
// minBackoff: 1ms
// maxBackoff: 10ms
// timeouts:
// dial: 1s
// read: 100ms
// write: 200ms
type ClientConfig struct {
// URL is passed to redis.ParseURL to initialize the client options. This is
// a required field.
//
// https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#ParseURL
URL string `yaml:"url"`
Pool PoolOptions `yaml:"pool"`
Retries RetryOptions `yaml:"retries"`
Timeouts TimeoutOptions `yaml:"timeouts"`
}
// Options returns a redis.Options populated using the values from cfg.
func (cfg ClientConfig) Options() (*redis.Options, error) {
options, err := redis.ParseURL(cfg.URL)
if err != nil {
return nil, fmt.Errorf("redisbp: error parsing configured redis url. %w", err)
}
cfg.Pool.ApplyOptions(options)
cfg.Retries.ApplyOptions(options)
cfg.Timeouts.ApplyOptions(options)
return options, nil
}
// ClusterConfig can be used to configure a redis-go "ClusterClient". See the
// docs for redis.ClusterOptions in redis-go for details on what each value
// means and what its defaults are:
// https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#ClusterOptions
//
// Can be deserialized from YAML.
//
// Examples:
//
// Minimal YAML:
//
// redis:
// addrs:
// - localhost:6379
// - localhost:6380
//
// Full YAML:
//
// redis:
// addrs:
// - localhost:6379
// - localhost:6380
// pool:
// size: 10
// minIdleConnections: 5
// maxConnectionAge: 1m
// timeout: 10s
// retries:
// max: 2
// minBackoff: 1ms
// maxBackoff: 10ms
// timeouts:
// dial: 1s
// read: 100ms
// write: 200ms
type ClusterConfig struct {
// Addrs is the seed list of cluster nodes in the format "host:port". This is
// a required field.
//
// Maps to Addrs on redis.ClusterClient.
Addrs []string `yaml:"addrs"`
Pool PoolOptions `yaml:"pool"`
Retries RetryOptions `yaml:"retries"`
Timeouts TimeoutOptions `yaml:"timeouts"`
}
// Options returns a redis.ClusterOptions populated using the values from cfg.
func (cfg ClusterConfig) Options() *redis.ClusterOptions {
options := &redis.ClusterOptions{
Addrs: cfg.Addrs,
}
cfg.Pool.ApplyClusterOptions(options)
cfg.Retries.ApplyClusterOptions(options)
cfg.Timeouts.ApplyClusterOptions(options)
return options
}
// PoolOptions is used to configure the pool attributes of a redis-go Client or
// ClusterClient. If any value is not set, it will use whatever default is
// defined by redis-go.
//
// See https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#Options for details
// on the specific fields.
//
// Can be deserialized from YAML.
type PoolOptions struct {
// Maps to PoolSize on the redis-go options.
Size int `yaml:"size"`
// Maps to MinIdleConnections on the redis-go options.
MinIdleConnections int `yaml:"minIdleConnections"`
// Maps to MaxConnAge on the redis-go options.
MaxConnectionAge time.Duration `yaml:"maxConnectionAge"`
// Maps to PoolTimeout on the redis-go options.
Timeout time.Duration `yaml:"timeout"`
}
// ApplyOptions applies the PoolOptions to the redis.Options.
func (opts PoolOptions) ApplyOptions(options *redis.Options) {
if opts.MinIdleConnections != 0 {
options.MinIdleConns = opts.MinIdleConnections
}
if opts.MaxConnectionAge != 0 {
options.MaxConnAge = opts.MaxConnectionAge
}
if opts.Size != 0 {
options.PoolSize = opts.Size
}
if opts.Timeout != 0 {
options.PoolTimeout = opts.Timeout
}
}
// ApplyClusterOptions applies the PoolOptions to the redis.ClusterOptions.
func (opts PoolOptions) ApplyClusterOptions(options *redis.ClusterOptions) {
if opts.MinIdleConnections != 0 {
options.MinIdleConns = opts.MinIdleConnections
}
if opts.MaxConnectionAge != 0 {
options.MaxConnAge = opts.MaxConnectionAge
}
if opts.Size != 0 {
options.PoolSize = opts.Size
}
if opts.Timeout != 0 {
options.PoolTimeout = opts.Timeout
}
}
// RetryOptions is used to configure the retry behavior of a redis-go Client or
// ClusterClient.
//
// See https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#Options for details
// on the specific fields.
//
// Can be deserialized from YAML.
type RetryOptions struct {
// Maps to MaxRetries on the redis-go options.
Max int `yaml:"max"`
// Maps to MinRetryBackoff on the redis-go options.
MinBackoff time.Duration `yaml:"minBackoff"`
// Maps to MaxRetryBackoff on the redis-go options.
MaxBackoff time.Duration `yaml:"maxBackoff"`
}
// ApplyOptions applies the RetryOptions to the redis.Options.
func (opts RetryOptions) ApplyOptions(options *redis.Options) {
if opts.Max != 0 {
options.MaxRetries = opts.Max
}
if opts.MinBackoff != 0 {
options.MinRetryBackoff = opts.MinBackoff
}
if opts.MaxBackoff != 0 {
options.MaxRetryBackoff = opts.MaxBackoff
}
}
// ApplyClusterOptions applies the RetryOptions to the redis.ClusterOptions.
func (opts RetryOptions) ApplyClusterOptions(options *redis.ClusterOptions) {
if opts.Max != 0 {
options.MaxRetries = opts.Max
}
if opts.MinBackoff != 0 {
options.MinRetryBackoff = opts.MinBackoff
}
if opts.MaxBackoff != 0 {
options.MaxRetryBackoff = opts.MaxBackoff
}
}
// TimeoutOptions is used to configure the timeout behavior of a redis-go Client
// or ClusterClient.
//
// See https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#Options for details
// on the specific fields.
//
// Can be deserialized from YAML.
type TimeoutOptions struct {
Dial time.Duration `yaml:"dial"`
Read time.Duration `yaml:"read"`
Write time.Duration `yaml:"write"`
}
// ApplyOptions applies the TimeoutOptions to the redis.Options.
func (opts TimeoutOptions) ApplyOptions(options *redis.Options) {
if opts.Dial != 0 {
options.DialTimeout = opts.Dial
}
if opts.Read != 0 {
options.ReadTimeout = opts.Read
}
if opts.Write != 0 {
options.WriteTimeout = opts.Write
}
}
// ApplyClusterOptions applies the TimeoutOptions to the redis.ClusterOptions.
func (opts TimeoutOptions) ApplyClusterOptions(options *redis.ClusterOptions) {
if opts.Dial != 0 {
options.DialTimeout = opts.Dial
}
if opts.Read != 0 {
options.ReadTimeout = opts.Read
}
if opts.Write != 0 {
options.WriteTimeout = opts.Write
}
}
// OptionsMust can be combine with ClientOptions.Options() to either return
// the *redis.Options object or panic if an error was returned. This allows
// you to just pass this into redis.NewClient.
//
// Ex:
//
// var opts redisbp.ClientOptions
// client := redis.NewClient(redisbp.OptionsMust(opts.Options()))
func OptionsMust(options *redis.Options, err error) *redis.Options {
if err != nil {
panic(err)
}
return options
}