Skip to content

Commit 2877fc6

Browse files
committed
Move MaxReconnectInterval to MQTT backend level.
This way it applies to all backends and not only to the generic authentication. Note that auto-reconnect (by the library) was disabled and was handled by the onConnectionLost handler.
1 parent 893b50e commit 2877fc6

File tree

6 files changed

+32
-34
lines changed

6 files changed

+32
-34
lines changed

cmd/lora-gateway-bridge/cmd/configfile.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ marshaler="{{ .Integration.Marshaler }}"
130130
# Command topic template.
131131
command_topic_template="{{ .Integration.MQTT.CommandTopicTemplate }}"
132132
133+
# Maximum interval that will be waited between reconnection attempts when connection is lost.
134+
# Valid units are 'ms', 's', 'm', 'h'. Note that these values can be combined, e.g. '24h30m15s'.
135+
max_reconnect_interval="{{ .Integration.MQTT.MaxReconnectInterval }}"
136+
133137
134138
# MQTT authentication.
135139
[integration.mqtt.auth]
@@ -186,10 +190,6 @@ marshaler="{{ .Integration.Marshaler }}"
186190
# mqtt TLS key file (optional)
187191
tls_key="{{ .Integration.MQTT.Auth.Generic.TLSKey }}"
188192
189-
# Maximum interval that will be waited between reconnection attempts when connection is lost.
190-
# Valid units are 'ms', 's', 'm', 'h'. Note that these values can be combined, e.g. '24h30m15s'.
191-
max_reconnect_interval="{{ .Integration.MQTT.Auth.Generic.MaxReconnectInterval }}"
192-
193193
194194
# Google Cloud Platform Cloud IoT Core authentication.
195195
#

cmd/lora-gateway-bridge/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ func init() {
5454

5555
viper.SetDefault("integration.mqtt.event_topic_template", "gateway/{{ .GatewayID }}/event/{{ .EventType }}")
5656
viper.SetDefault("integration.mqtt.command_topic_template", "gateway/{{ .GatewayID }}/command/#")
57+
viper.SetDefault("integration.mqtt.max_reconnect_interval", 10*time.Minute)
5758

5859
viper.SetDefault("integration.mqtt.auth.generic.server", "tcp://127.0.0.1:1883")
5960
viper.SetDefault("integration.mqtt.auth.generic.clean_session", true)
60-
viper.SetDefault("integration.mqtt.auth.generic.max_reconnect_interval", 10*time.Minute)
6161

6262
viper.SetDefault("integration.mqtt.auth.gcp_cloud_iot_core.server", "ssl://mqtt.googleapis.com:8883")
6363
viper.SetDefault("integration.mqtt.auth.gcp_cloud_iot_core.jwt_expiration", time.Hour*24)

docs/content/install/config.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ marshaler="protobuf"
176176
# Command topic template.
177177
command_topic_template="gateway/{{ .GatewayID }}/command/#"
178178

179+
# Maximum interval that will be waited between reconnection attempts when connection is lost.
180+
# Valid units are 'ms', 's', 'm', 'h'. Note that these values can be combined, e.g. '24h30m15s'.
181+
max_reconnect_interval="10m0s"
182+
179183

180184
# MQTT authentication.
181185
[integration.mqtt.auth]
@@ -232,10 +236,6 @@ marshaler="protobuf"
232236
# mqtt TLS key file (optional)
233237
tls_key=""
234238

235-
# Maximum interval that will be waited between reconnection attempts when connection is lost.
236-
# Valid units are 'ms', 's', 'm', 'h'. Note that these values can be combined, e.g. '24h30m15s'.
237-
max_reconnect_interval="10m0s"
238-
239239

240240
# Google Cloud Platform Cloud IoT Core authentication.
241241
#

internal/config/config.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,23 @@ type Config struct {
4747
Marshaler string `mapstructure:"marshaler"`
4848

4949
MQTT struct {
50-
EventTopicTemplate string `mapstructure:"event_topic_template"`
51-
CommandTopicTemplate string `mapstructure:"command_topic_template"`
50+
EventTopicTemplate string `mapstructure:"event_topic_template"`
51+
CommandTopicTemplate string `mapstructure:"command_topic_template"`
52+
MaxReconnectInterval time.Duration `mapstructure:"max_reconnect_interval"`
5253

5354
Auth struct {
5455
Type string `mapstructure:"type"`
5556

5657
Generic struct {
57-
Server string `mapstructure:"server"`
58-
Username string `mapstructure:"username"`
59-
Password string `mapstrucure:"password"`
60-
CACert string `mapstructure:"ca_cert"`
61-
TLSCert string `mapstructure:"tls_cert"`
62-
TLSKey string `mapstructure:"tls_key"`
63-
QOS uint8 `mapstructure:"qos"`
64-
CleanSession bool `mapstructure:"clean_session"`
65-
ClientID string `mapstructure:"client_id"`
66-
MaxReconnectInterval time.Duration `mapstructure:"max_reconnect_interval"`
58+
Server string `mapstructure:"server"`
59+
Username string `mapstructure:"username"`
60+
Password string `mapstrucure:"password"`
61+
CACert string `mapstructure:"ca_cert"`
62+
TLSCert string `mapstructure:"tls_cert"`
63+
TLSKey string `mapstructure:"tls_key"`
64+
QOS uint8 `mapstructure:"qos"`
65+
CleanSession bool `mapstructure:"clean_session"`
66+
ClientID string `mapstructure:"client_id"`
6767
} `mapstructure:"generic"`
6868

6969
GCPCloudIoTCore struct {

internal/integration/mqtt/auth/generic.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ import (
1212

1313
// GenericAuthentication implements a generic MQTT authentication.
1414
type GenericAuthentication struct {
15-
server string
16-
username string
17-
password string
18-
cleanSession bool
19-
clientID string
20-
maxReconnectInterval time.Duration
15+
server string
16+
username string
17+
password string
18+
cleanSession bool
19+
clientID string
2120

2221
tlsConfig *tls.Config
2322
}
@@ -36,12 +35,11 @@ func NewGenericAuthentication(conf config.Config) (Authentication, error) {
3635
return &GenericAuthentication{
3736
tlsConfig: tlsConfig,
3837

39-
server: conf.Integration.MQTT.Auth.Generic.Server,
40-
username: conf.Integration.MQTT.Auth.Generic.Username,
41-
password: conf.Integration.MQTT.Auth.Generic.Password,
42-
cleanSession: conf.Integration.MQTT.Auth.Generic.CleanSession,
43-
clientID: conf.Integration.MQTT.Auth.Generic.ClientID,
44-
maxReconnectInterval: conf.Integration.MQTT.Auth.Generic.MaxReconnectInterval,
38+
server: conf.Integration.MQTT.Auth.Generic.Server,
39+
username: conf.Integration.MQTT.Auth.Generic.Username,
40+
password: conf.Integration.MQTT.Auth.Generic.Password,
41+
cleanSession: conf.Integration.MQTT.Auth.Generic.CleanSession,
42+
clientID: conf.Integration.MQTT.Auth.Generic.ClientID,
4543
}, nil
4644
}
4745

@@ -52,7 +50,6 @@ func (a *GenericAuthentication) Init(opts *mqtt.ClientOptions) error {
5250
opts.SetPassword(a.password)
5351
opts.SetCleanSession(a.cleanSession)
5452
opts.SetClientID(a.clientID)
55-
opts.SetMaxReconnectInterval(a.maxReconnectInterval)
5653

5754
if a.tlsConfig != nil {
5855
opts.SetTLSConfig(a.tlsConfig)

internal/integration/mqtt/backend.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func NewBackend(conf config.Config) (*Backend, error) {
121121
b.clientOpts.SetAutoReconnect(true) // this is required for buffering messages in case offline!
122122
b.clientOpts.SetOnConnectHandler(b.onConnected)
123123
b.clientOpts.SetConnectionLostHandler(b.onConnectionLost)
124+
b.clientOpts.SetMaxReconnectInterval(conf.Integration.MQTT.MaxReconnectInterval)
124125

125126
if err = b.auth.Init(b.clientOpts); err != nil {
126127
return nil, errors.Wrap(err, "mqtt: init authentication error")

0 commit comments

Comments
 (0)