-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.go
161 lines (139 loc) · 3.39 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
package redisdb
import (
"context"
"crypto/tls"
"github.com/golang-queue/queue"
"github.com/golang-queue/queue/core"
)
// Option for queue system
type Option func(*options)
type options struct {
runFunc func(context.Context, core.QueuedMessage) error
logger queue.Logger
addr string
db int
connectionString string
username string
password string
channelName string
channelSize int
cluster bool
sentinel bool
masterName string
tls *tls.Config
}
// WithAddr setup the addr of redis
func WithAddr(addr string) Option {
return func(w *options) {
w.addr = addr
}
}
// WithPassword redis password
func WithDB(db int) Option {
return func(w *options) {
w.db = db
}
}
// WithCluster redis cluster
func WithCluster(enable bool) Option {
return func(w *options) {
w.cluster = enable
}
}
// WithSentinel redis sentinel
func WithSentinel(enable bool) Option {
return func(w *options) {
w.sentinel = enable
}
}
// WithTLS returns an Option that configures the use of TLS for the connection.
// It sets the minimum TLS version to TLS 1.2.
func WithTLS() Option {
return func(w *options) {
w.tls = &tls.Config{
MinVersion: tls.VersionTLS12,
}
}
}
// WithSkipTLSVerify returns an Option that configures the TLS settings to skip
// verification of the server's certificate. This is useful for connecting to
// servers with self-signed certificates or when certificate verification is
// not required. Use this option with caution as it makes the connection
// susceptible to man-in-the-middle attacks.
func WithSkipTLSVerify() Option {
return func(w *options) {
if w.tls == nil {
w.tls = &tls.Config{
InsecureSkipVerify: true, //nolint: gosec
}
return
}
w.tls.InsecureSkipVerify = true
}
}
// WithMasterName sentinel master name
func WithMasterName(masterName string) Option {
return func(w *options) {
w.masterName = masterName
}
}
// WithChannelSize redis channel size
func WithChannelSize(size int) Option {
return func(w *options) {
w.channelSize = size
}
}
// WithUsername redis username
func WithUsername(username string) Option {
return func(w *options) {
w.username = username
}
}
// WithPassword redis password
func WithPassword(passwd string) Option {
return func(w *options) {
w.password = passwd
}
}
// WithConnectionString redis connection string
func WithConnectionString(connectionString string) Option {
return func(w *options) {
w.connectionString = connectionString
}
}
// WithChannel setup the channel of redis
func WithChannel(channel string) Option {
return func(w *options) {
w.channelName = channel
}
}
// WithRunFunc setup the run func of queue
func WithRunFunc(fn func(context.Context, core.QueuedMessage) error) Option {
return func(w *options) {
w.runFunc = fn
}
}
// WithLogger set custom logger
func WithLogger(l queue.Logger) Option {
return func(w *options) {
w.logger = l
}
}
func newOptions(opts ...Option) options {
defaultOpts := options{
addr: "127.0.0.1:6379",
channelName: "queue",
// default channel size in go-redis package
channelSize: 100,
logger: queue.NewLogger(),
runFunc: func(context.Context, core.QueuedMessage) error {
return nil
},
}
// Loop through each option
for _, opt := range opts {
// Call the option giving the instantiated
opt(&defaultOpts)
}
return defaultOpts
}