-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
340 lines (319 loc) · 9.78 KB
/
option.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* MIT License
*
* Copyright (c) 2025 Arsene Tochemey Gandote
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package distcache
import (
"time"
"github.com/tochemey/distcache/hash"
"github.com/tochemey/distcache/log"
)
// Option defines a configuration option that can be applied to a Config.
//
// Implementations of this interface modify the configuration when applied.
type Option interface {
// Apply applies the configuration option to the given Config instance.
Apply(config *Config)
}
// enforce compilation error if OptionFunc does not implement Option
var _ Option = OptionFunc(nil)
// OptionFunc is a function type that implements the Option interface.
//
// It allows functions to be used as configuration options for Config.
type OptionFunc func(config *Config)
// Apply applies the OptionFunc to the given Config.
//
// This enables the use of functions as dynamic configuration options.
func (f OptionFunc) Apply(config *Config) {
f(config)
}
// WithLogger configures the config to use a custom logger.
//
// Parameters:
// - logger: An instance of log.Logger used for logging.
//
// Returns:
// - An Option that applies the custom logger to the Config.
//
// Usage:
//
// config := NewConfig(WithLogger(myLogger))
func WithLogger(logger log.Logger) Option {
return OptionFunc(
func(config *Config) {
config.logger = logger
},
)
}
// WithBindAddr configures the config to use a custom bind address.
//
// BindAddr denotes the address that distcache will bind to for communication
// with other distcache nodes.
//
// Parameters:
// - addr: The bind address to use.
//
// Returns:
// - An Option that applies the custom bind addr to the Config.
//
// Usage:
//
// config := NewConfig(WithBindAddr(addr))
func WithBindAddr(addr string) Option {
return OptionFunc(
func(config *Config) {
config.bindAddr = addr
},
)
}
// WithInterface configures the config to use a custom bind interface.
//
// Interface denotes a binding interface. It can be used instead of BindAddr
// if the interface is known but not the address. If both are provided, then
// distcache verifies that the interface has the bind address that is provided.
//
// Parameters:
// - ifname: The bind interface to use.
//
// Returns:
// - An Option that applies the custom bind addr to the Config.
//
// Usage:
//
// config := NewConfig(WithInterface(ifname))
func WithInterface(ifname string) Option {
return OptionFunc(
func(config *Config) {
config.ifname = ifname
},
)
}
// WithBindPort configures the config to use a custom bind port.
//
// BindPort denotes the address that distcache will bind to for communication
// with other distcache nodes.
//
// Parameters:
// - port: The bind port to use.
//
// Returns:
// - An Option that applies the custom bind port to the Config.
//
// Usage:
//
// config := NewConfig(WithBindPort(port))
func WithBindPort(port int) Option {
return OptionFunc(func(config *Config) {
config.bindPort = port
})
}
// WithDiscoveryPort configures the config to use a custom discovery port.
//
// Parameters:
// - port: The discovery port to use.
//
// Returns:
// - An Option that applies the custom discovery port to the Config.
//
// Usage:
//
// config := NewConfig(WithDiscoveryPort(port))
func WithDiscoveryPort(port int) Option {
return OptionFunc(func(config *Config) {
config.discoveryPort = port
})
}
// WithKeepAlivePeriod configures the config to use a custom keepAlive period.
//
// KeepAlivePeriod denotes whether the operating system should send
// keep-alive messages on the connection.
//
// Parameters:
// - period: The keep alive period.
//
// Returns:
// - An Option that applies the custom keep alive period to the Config.
//
// Usage:
//
// config := NewConfig(WithKeepAlivePeriod(period))
func WithKeepAlivePeriod(period time.Duration) Option {
return OptionFunc(func(config *Config) {
config.keepAlivePeriod = period
})
}
// WithBootstrapTimeout configures the config to use a custom bootstrap timeout.
//
// A distcache node checks operation status before taking any action for the
// cluster events, responding incoming requests and running API functions.
// Bootstrapping status is one of the most important checkpoints for an
// "operable" distcache node. BootstrapTimeout sets a deadline to check
// bootstrapping status without blocking indefinitely.
//
// Parameters:
// - timeout: The custom bootstrap timeout.
//
// Returns:
// - An Option that applies the custom bootstrap timeout to the Config.
//
// Usage:
//
// config := NewConfig(WithBootstrapTimeout(timeout))
func WithBootstrapTimeout(timeout time.Duration) Option {
return OptionFunc(func(config *Config) {
config.bootstrapTimeout = timeout
})
}
// WithReplicaCount configures the config to use a custom replica count.
//
// Parameters:
// - count: The custom replica count.
//
// Returns:
// - An Option that applies the custom replica count to the Config.
//
// Usage:
//
// config := NewConfig(WithReplicaCount(count))
func WithReplicaCount(count int) Option {
return OptionFunc(func(config *Config) {
config.replicaCount = count
})
}
// WithShutdownTimeout configures the config to use a custom shutdown timeout.
//
// distcache will broadcast a leave message but will not shut down the background
// listeners, meaning the node will continue participating in gossip and state
// updates.
//
// Sending a leave message will block until the leave message is successfully
// broadcast to a member of the cluster, if any exist or until a specified timeout
// is reached.
//
// Parameters:
// - timeout: The custom shutdown timeout.
//
// Returns:
// - An Option that applies the custom shutdown timeout to the Config.
//
// Usage:
//
// config := NewConfig(WithShutdownTimeout(timeout))
func WithShutdownTimeout(timeout time.Duration) Option {
return OptionFunc(
func(config *Config) {
config.shutdownTimeout = timeout
},
)
}
// WithJoinRetryInterval configures the config to use a custom join retry interval.
//
// JoinRetryInterval is the time gap between attempts to join an existing
// cluster.
//
// Parameters:
// - interval: The custom join retry interval.
//
// Returns:
// - An Option that applies the custom retry interval to the Config.
//
// Usage:
//
// config := NewConfig(WithJoinRetryInterval(interval))
func WithJoinRetryInterval(interval time.Duration) Option {
return OptionFunc(func(config *Config) {
config.joinRetryInterval = interval
})
}
// WithMaxJoinAttempts configures the config to use a custom max join attempts.
//
// MaxJoinAttempts denotes the maximum number of attempts to join an existing
// cluster before forming a new one.
//
// Parameters:
// - maxAttempts: The custom maximum join attempts.
//
// Returns:
// - An Option that applies the custom maximum join attempts to the Config.
//
// Usage:
//
// config := NewConfig(WithMaxJoinAttempts(maxAttempts))
func WithMaxJoinAttempts(maxAttempts int) Option {
return OptionFunc(func(config *Config) {
config.maxJoinAttempts = maxAttempts
})
}
// WithMinimumPeersQuorum configures the config to use a custom minimum peers quorum.
//
// MinimumPeersQuorum denotes the minimum number of peers
// required to form a cluster.
//
// Parameters:
// - minQuorum: The custom minimum peers quorum.
//
// Returns:
// - An Option that applies the custom minimum peers quorum to the Config.
//
// Usage:
//
// config := NewConfig(WithMinimumPeersQuorum(minQuorum)
func WithMinimumPeersQuorum(minQuorum int) Option {
return OptionFunc(func(config *Config) {
config.minimumPeersQuorum = minQuorum
})
}
// WithTLS configures the cache engine to use the specified TLS settings for both the Server and Client.
//
// Ensure that both the Server and Client are configured with the same
// root Certificate Authority (CA) to enable successful handshake and
// mutual authentication.
//
// This option allows secure communication by setting a custom TLS configuration
// for encrypting data in transit.
//
// Parameters:
// - info: A pointer to TLSInfo struct that defines TLS settings,
// such as certificates, cipher suites, and authentication options.
//
// Returns:
// - Option: A functional option that applies the TLS configuration to the cache engine.
func WithTLS(info *TLSInfo) Option {
return OptionFunc(func(config *Config) {
config.tlsInfo = info
})
}
// WithHasher configures the cache engine to use a custom hashing function.
//
// This option allows you to specify a custom hash function for key hashing,
// which can be useful for controlling hash collisions, performance, or cryptographic security.
//
// Parameters:
// - hashFn: A hash.Hasher that defines the hashing function to be used for key hashing.
//
// Returns:
// - Option: A functional option that applies the specified hashing function to the cache engine.
func WithHasher(hashFn hash.Hasher) Option {
return OptionFunc(func(config *Config) {
config.hasher = hashFn
})
}