Skip to content

Commit

Permalink
feat: new flag: mqtt-reconnect-delay
Browse files Browse the repository at this point in the history
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 <link@sub-pop.net>
  • Loading branch information
subpop committed Sep 15, 2023
1 parent fdf6c33 commit 4f337f4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/yggd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions internal/transport/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 4f337f4

Please sign in to comment.