From 4f337f4fa58bcbb00526b3f73b2198952e8f008f Mon Sep 17 00:00:00 2001
From: Link Dupont
Date: Fri, 15 Sep 2023 13:27:56 -0400
Subject: [PATCH] feat: new flag: mqtt-reconnect-delay
Add a new hidden flag: --mqtt-reconnect-delay. This flag will sleep for
the given duration before attempting to reconnect to the MQTT broker.
This only works if --mqtt-auto-reconnect is enabled (it is true by
default).
Signed-off-by: Link Dupont
---
cmd/yggd/main.go | 7 +++++++
internal/config/config.go | 5 +++++
internal/transport/mqtt.go | 12 ++++++++++++
3 files changed, 24 insertions(+)
diff --git a/cmd/yggd/main.go b/cmd/yggd/main.go
index 046d9ff7..c7dd0230 100644
--- a/cmd/yggd/main.go
+++ b/cmd/yggd/main.go
@@ -65,6 +65,7 @@ func setupDefaultConfig(c *cli.Context) {
MQTTConnectRetry: c.Bool(config.FlagNameMQTTConnectRetry),
MQTTConnectRetryInterval: c.Duration(config.FlagNameMQTTConnectRetryInterval),
MQTTAutoReconnect: c.Bool(config.FlagNameMQTTAutoReconnect),
+ MQTTReconnectDelay: c.Duration(config.FlagNameMQTTReconnectDelay),
}
}
@@ -508,6 +509,12 @@ func main() {
Value: true,
Hidden: true,
}),
+ altsrc.NewDurationFlag(&cli.DurationFlag{
+ Name: config.FlagNameMQTTReconnectDelay,
+ Usage: "Sets the time to wait before attempting to reconnect to `DURATION`",
+ Value: 0 * time.Second,
+ Hidden: true,
+ }),
}
app.EnableBashCompletion = true
diff --git a/internal/config/config.go b/internal/config/config.go
index 5e65fff2..4f8f46a5 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -27,6 +27,7 @@ const (
FlagNameMQTTConnectRetry = "mqtt-connect-retry"
FlagNameMQTTConnectRetryInterval = "mqtt-connect-retry-interval"
FlagNameMQTTAutoReconnect = "mqtt-auto-reconnect"
+ FlagNameMQTTReconnectDelay = "mqtt-reconnect-delay"
)
var DefaultConfig = Config{
@@ -91,6 +92,10 @@ type Config struct {
// MQTTAutoReconnect is the MQTT client option that enables automatic
// reconnection logic when the client unexpectedly disconnects.
MQTTAutoReconnect bool
+
+ // MQTTReconnectDelay is the duration the client with wait before attempting
+ // to reconnect to the MQTT broker.
+ MQTTReconnectDelay time.Duration
}
// CreateTLSConfig creates a tls.Config object from the current configuration.
diff --git a/internal/transport/mqtt.go b/internal/transport/mqtt.go
index 14dc8960..e8150f4e 100644
--- a/internal/transport/mqtt.go
+++ b/internal/transport/mqtt.go
@@ -99,6 +99,18 @@ func NewMQTTTransport(clientID string, brokers []string, tlsConfig *tls.Config)
t.events <- TransporterEventDisconnected
})
+ opts.SetReconnectingHandler(func(c mqtt.Client, co *mqtt.ClientOptions) {
+ log.Debugf("reconnecting to broker: %v", co.Servers)
+
+ if config.DefaultConfig.MQTTReconnectDelay > 0 {
+ log.Infof(
+ "delaying for %v before reconnection",
+ config.DefaultConfig.MQTTReconnectDelay,
+ )
+ time.Sleep(config.DefaultConfig.MQTTReconnectDelay)
+ }
+ })
+
data, err := json.Marshal(&yggdrasil.ConnectionStatus{
Type: yggdrasil.MessageTypeConnectionStatus,
MessageID: uuid.New().String(),