-
Notifications
You must be signed in to change notification settings - Fork 16
/
options.go
87 lines (77 loc) · 2.03 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
// SPDX-License-Identifier: Apache-2.0
package mqtt
import (
"go.opentelemetry.io/otel/trace"
"net/url"
"time"
)
type connectionDetails struct {
mqttBrokerUrls []*url.URL
mqttPrefix string
mqttConnectTimeout time.Duration
mqttConnectRetryDelay time.Duration
mqttKeepAliveInterval uint16
}
type Opt[T any] func(h *T)
func WithMqttBrokerUrl[T Emitter | Listener](brokerUrl *url.URL) Opt[T] {
return func(h *T) {
switch x := any(h).(type) {
case *Emitter:
x.mqttBrokerUrls = append(x.mqttBrokerUrls, brokerUrl)
case *Listener:
x.mqttBrokerUrls = append(x.mqttBrokerUrls, brokerUrl)
}
}
}
func WithMqttBrokerUrls[T Emitter | Listener](brokerUrls []*url.URL) Opt[T] {
return func(h *T) {
switch x := any(h).(type) {
case *Emitter:
x.mqttBrokerUrls = brokerUrls
case *Listener:
x.mqttBrokerUrls = brokerUrls
}
}
}
func WithMqttPrefix[T Emitter | Listener](mqttPrefix string) Opt[T] {
return func(h *T) {
switch x := any(h).(type) {
case *Emitter:
x.mqttPrefix = mqttPrefix
case *Listener:
x.mqttPrefix = mqttPrefix
}
}
}
func WithMqttConnectSettings[T Emitter | Listener](mqttConnectTimeout, mqttConnectRetryDelay, mqttKeepAliveInterval time.Duration) Opt[T] {
return func(h *T) {
switch x := any(h).(type) {
case *Emitter:
x.mqttConnectTimeout = mqttConnectTimeout
x.mqttConnectRetryDelay = mqttConnectRetryDelay
x.mqttKeepAliveInterval = uint16(mqttKeepAliveInterval.Round(time.Second).Seconds())
case *Listener:
x.mqttConnectTimeout = mqttConnectTimeout
x.mqttConnectRetryDelay = mqttConnectRetryDelay
x.mqttKeepAliveInterval = uint16(mqttKeepAliveInterval.Round(time.Second).Seconds())
}
}
}
func WithOtelTracer[T Emitter | Listener](tracer trace.Tracer) Opt[T] {
return func(h *T) {
switch x := any(h).(type) {
case *Emitter:
x.tracer = tracer
case *Listener:
x.tracer = tracer
}
}
}
func WithMqttGroup[T Listener](mqttGroup string) Opt[T] {
return func(h *T) {
switch x := any(h).(type) {
case *Listener:
x.mqttGroup = mqttGroup
}
}
}