-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.go
432 lines (378 loc) · 11.3 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package unitdb
import (
"crypto/tls"
"net/url"
"regexp"
"strings"
"time"
)
const (
defaultBatchDuration = 100 * time.Millisecond
maxBatchDuration = 15 * time.Second
// publish request (containing a batch of messages) in bytes. Must be lower
// than the gRPC limit of 4 MiB.
maxPubBytes = 3.5 * 1024 * 1024
maxPubCount = 1000
)
// ConnectionHandler is a callback that is called when connection to the server is established.
type ConnectionHandler func(Client)
// ConnectionLostHandler is a callback that is set to be executed
// upon an uninteded disconnection from server.
type ConnectionLostHandler func(Client, error)
type options struct {
servers []*url.URL
clientID string
sessionKey uint32
insecureFlag bool
username string
password []byte
cleanSession bool
tLSConfig *tls.Config
keepAlive int32
pingTimeout time.Duration
connectTimeout time.Duration
storePath string
storeSize int
storeLogReleaseDuration time.Duration
connectionHandler ConnectionHandler
connectionLostHandler ConnectionLostHandler
writeTimeout time.Duration
batchDuration time.Duration
batchByteThreshold int
batchCountThreshold int
resumeSubs bool
}
func (o *options) addServer(target string) {
re := regexp.MustCompile(`%(25)?`)
if len(target) > 0 && target[0] == ':' {
target = "127.0.0.1" + target
}
if !strings.Contains(target, "://") {
target = "grpc://" + target
}
target = re.ReplaceAllLiteralString(target, "%25")
uri, err := url.Parse(target)
if err != nil {
return
}
o.servers = append(o.servers, uri)
}
func (o *options) setClientID(clientID string) {
o.clientID = clientID
}
// Options it contains configurable options for client
type Options interface {
set(*options)
}
// fOption wraps a function that modifies options into an
// implementation of the Option interface.
type fOption struct {
f func(*options)
}
func (fo *fOption) set(o *options) {
fo.f(o)
}
func newFuncOption(f func(*options)) *fOption {
return &fOption{
f: f,
}
}
// WithDefaultOptions will create client connection with some default values.
// CleanSession: True
// KeepAlive: 30 (seconds)
// ConnectTimeout: 30 (seconds)
func WithDefaultOptions() Options {
return newFuncOption(func(o *options) {
o.servers = nil
o.clientID = ""
o.sessionKey = 0
o.insecureFlag = false
o.username = ""
o.password = nil
o.cleanSession = false
o.keepAlive = 30
o.pingTimeout = 30 * time.Second
o.connectTimeout = 30 * time.Second
o.writeTimeout = 30 * time.Second // 0 represents timeout disabled
o.storePath = "/tmp/unitdb"
o.storeSize = 1 << 27
if o.writeTimeout > 0 {
o.storeLogReleaseDuration = o.writeTimeout
} else {
o.storeLogReleaseDuration = 1 * time.Minute // must be greater than WriteTimeout
}
o.batchDuration = defaultBatchDuration
o.batchByteThreshold = maxPubBytes
o.batchCountThreshold = maxPubCount
o.resumeSubs = false
})
}
// AddServer returns an Option which makes client connection and set server url
func AddServer(target string) Options {
return newFuncOption(func(o *options) {
re := regexp.MustCompile(`%(25)?`)
if len(target) > 0 && target[0] == ':' {
target = "127.0.0.1" + target
}
if !strings.Contains(target, "://") {
target = "tcp://" + target
}
target = re.ReplaceAllLiteralString(target, "%25")
uri, err := url.Parse(target)
if err != nil {
return
}
o.servers = append(o.servers, uri)
})
}
// WithClientID returns an Option which makes client connection and set ClientID
func WithClientID(clientID string) Options {
return newFuncOption(func(o *options) {
o.clientID = clientID
})
}
// WithSessionKey returns an Option which makes client connection with an existing SessionKey
func WithSessionKey(sessKey uint32) Options {
return newFuncOption(func(o *options) {
o.sessionKey = sessKey
})
}
// WithInsecure returns an Option which makes client connection
// with insecure flag so that client can provide topic with key prefix.
// Use insecure flag only for test and debug connection and not for live client.
func WithInsecure() Options {
return newFuncOption(func(o *options) {
o.insecureFlag = true
})
}
// WithUserName returns an Option which makes client connection and pass UserName
func WithUserNamePassword(userName string, password []byte) Options {
return newFuncOption(func(o *options) {
o.username = userName
o.password = password
})
}
// WithCleanSession returns an Option which makes client connection and set CleanSession
func WithCleanSession() Options {
return newFuncOption(func(o *options) {
o.cleanSession = true
})
}
// WithTLSConfig will set an SSL/TLS configuration to be used when connecting
// to server.
func WithTLSConfig(t *tls.Config) Options {
return newFuncOption(func(o *options) {
o.tLSConfig = t
})
}
// WithKeepAlive will set the amount of time (in seconds) that the client
// should wait before sending a PING request to the server. This will
// allow the client to know that a connection has not been lost with the
// server.
func WithKeepAlive(k time.Duration) Options {
return newFuncOption(func(o *options) {
o.keepAlive = int32(k / time.Second)
})
}
// WithPingTimeout will set the amount of time (in seconds) that the client
// will wait after sending a PING request to the server, before deciding
// that the connection has been lost. Default is 10 seconds.
func WithPingTimeout(k time.Duration) Options {
return newFuncOption(func(o *options) {
o.pingTimeout = k
})
}
// WithWriteTimeout puts a limit on how long a publish should block until it unblocks with a
// timeout error. A duration of 0 never times out. Default never times out
func WithWriteTimeout(t time.Duration) Options {
return newFuncOption(func(o *options) {
o.writeTimeout = t
})
}
// WithConnectTimeout limits how long the client will wait when trying to open a connection
// to server before timing out and erroring the attempt. A duration of 0 never times out.
// Default 30 seconds.
func WithConnectTimeout(t time.Duration) Options {
return newFuncOption(func(o *options) {
o.connectTimeout = t
})
}
// WithStoreDir sets database directory.
func WithStorePath(path string) Options {
return newFuncOption(func(o *options) {
o.storePath = path
})
}
// WithStoreSize sets buffer size store will use to write messages into log.
func WithStoreSize(size int) Options {
return newFuncOption(func(o *options) {
o.storeSize = size
})
}
// WithStoreLogReleaseDuration sets log release duration, it must be greater than WriteTimeout.
func WithStoreLogReleaseDuration(dur time.Duration) Options {
return newFuncOption(func(o *options) {
if dur > o.writeTimeout {
o.storeLogReleaseDuration = dur
}
})
}
// WithConnectionHandler sets handler function to be called when client is connected.
func WithConnectionHandler(handler ConnectionHandler) Options {
return newFuncOption(func(o *options) {
o.connectionHandler = handler
})
}
// WithConnectionLostHandler sets handler function to be called
// when connection to the client is lost.
func WithConnectionLostHandler(handler ConnectionLostHandler) Options {
return newFuncOption(func(o *options) {
o.connectionLostHandler = handler
})
}
// WithBatchDuration sets batch duration to group publish requestes into single group.
func WithBatchDuration(dur time.Duration) Options {
return newFuncOption(func(o *options) {
if dur < maxBatchDuration {
o.batchDuration = dur
} else {
o.batchDuration = maxBatchDuration
}
})
}
// WithBatchByteThreshold sets byte threshold for publish batch.
func WithBatchByteThreshold(size int) Options {
return newFuncOption(func(o *options) {
if size < maxPubBytes {
o.batchByteThreshold = size
}
})
}
// WithBatchCountThreshold sets message count threshold for publish batch.
func WithBatchCountThreshold(count int) Options {
return newFuncOption(func(o *options) {
if count < maxPubCount {
o.batchCountThreshold = count
}
})
}
// WithResumeSubs will enable resuming stored subscribe/unsubscribe messages
// when connecting but not reconnecting if CleanSession is false.
func WithResumeSubs() Options {
return newFuncOption(func(o *options) {
o.resumeSubs = true
})
}
// -------------------------------------------------------------
type pubSubOptions struct {
deliveryMode uint8
delay int32
}
// -------------------------------------------------------------
type pubOptions struct {
pubSubOptions
ttl string
}
// PubOptions it contains configurable options for Publish
type PubOptions interface {
set(*pubOptions)
}
// fPubOption wraps a function that modifies options into an
// implementation of the PubOption interface.
type fPubOption struct {
f func(*pubOptions)
}
func (fo *fPubOption) set(o *pubOptions) {
fo.f(o)
}
func newFuncPubOption(f func(*pubOptions)) *fPubOption {
return &fPubOption{
f: f,
}
}
// WithPubDeliveryMode sets DeliveryMode of publish packet.
// 0 EXPRESS
// 1 RELIEABLE
// 2 BATCH
func WithPubDeliveryMode(deliveryMode uint8) PubOptions {
return newFuncPubOption(func(o *pubOptions) {
o.deliveryMode = deliveryMode
})
}
// WithPubDelay allows to specify delay in milliseconds for delivery of messages
// if DeliveryMode is set to RELIABLE or BATCH.
func WithPubDelay(delay time.Duration) PubOptions {
return newFuncPubOption(func(o *pubOptions) {
o.delay = int32(delay.Milliseconds())
})
}
// WithTTL allows to specify time to live for a publish packet.
func WithTTL(ttl string) PubOptions {
return newFuncPubOption(func(o *pubOptions) {
o.ttl = ttl
})
}
// -------------------------------------------------------------
type relOptions struct {
last string
}
// Re;Options it contains configurable options for Subscribe
type RelOptions interface {
set(*relOptions)
}
// fRelOption wraps a function that modifies options into an
// implementation of the RelOption interface.
type fRelOption struct {
f func(*relOptions)
}
func (fo *fRelOption) set(o *relOptions) {
fo.f(o)
}
func newFuncRelOption(f func(*relOptions)) *fRelOption {
return &fRelOption{
f: f,
}
}
// WithLast allows to specify duration to retrive stored messages on a new relay request.
func WithLast(last string) RelOptions {
return newFuncRelOption(func(o *relOptions) {
o.last = last
})
}
// -------------------------------------------------------------
type subOptions struct {
pubSubOptions
}
// SubOptions it contains configurable options for Subscribe
type SubOptions interface {
set(*subOptions)
}
// fSubOption wraps a function that modifies options into an
// implementation of the SubOption interface.
type fSubOption struct {
f func(*subOptions)
}
func (fo *fSubOption) set(o *subOptions) {
fo.f(o)
}
func newFuncSubOption(f func(*subOptions)) *fSubOption {
return &fSubOption{
f: f,
}
}
// WithSubDeliveryMode sets DeliveryMode of a subscription.
// 0 EXPRESS
// 1 RELIEABLE
// 2 BATCH
func WithSubDeliveryMode(deliveryMode uint8) SubOptions {
return newFuncSubOption(func(o *subOptions) {
o.deliveryMode = deliveryMode
})
}
// WithSubDelay allows to specify delay in milliseconds for delivery of messages
// if DeliveryMode is set to RELIABLE or BATCH.
func WithSubDelay(delay time.Duration) SubOptions {
return newFuncSubOption(func(o *subOptions) {
o.delay = int32(delay.Milliseconds())
})
}