diff --git a/cmd/yggd/main.go b/cmd/yggd/main.go index 7fce8b8e..c7dd0230 100644 --- a/cmd/yggd/main.go +++ b/cmd/yggd/main.go @@ -50,18 +50,22 @@ func generateDocumentation(c *cli.Context) error { // CLI flags and arguments. func setupDefaultConfig(c *cli.Context) { config.DefaultConfig = config.Config{ - LogLevel: c.String(config.FlagNameLogLevel), - ClientID: c.String(config.FlagNameClientID), - Server: c.StringSlice(config.FlagNameServer), - CertFile: c.String(config.FlagNameCertFile), - KeyFile: c.String(config.FlagNameKeyFile), - CARoot: c.StringSlice(config.FlagNameCaRoot), - PathPrefix: c.String(config.FlagNamePathPrefix), - Protocol: c.String(config.FlagNameProtocol), - DataHost: c.String(config.FlagNameDataHost), - CanonicalFacts: c.String(config.FlagNameCanonicalFacts), - HTTPRetries: c.Int(config.FlagNameHTTPRetries), - HTTPTimeout: c.Duration(config.FlagNameHTTPTimeout), + LogLevel: c.String(config.FlagNameLogLevel), + ClientID: c.String(config.FlagNameClientID), + Server: c.StringSlice(config.FlagNameServer), + CertFile: c.String(config.FlagNameCertFile), + KeyFile: c.String(config.FlagNameKeyFile), + CARoot: c.StringSlice(config.FlagNameCaRoot), + PathPrefix: c.String(config.FlagNamePathPrefix), + Protocol: c.String(config.FlagNameProtocol), + DataHost: c.String(config.FlagNameDataHost), + CanonicalFacts: c.String(config.FlagNameCanonicalFacts), + HTTPRetries: c.Int(config.FlagNameHTTPRetries), + HTTPTimeout: c.Duration(config.FlagNameHTTPTimeout), + MQTTConnectRetry: c.Bool(config.FlagNameMQTTConnectRetry), + MQTTConnectRetryInterval: c.Duration(config.FlagNameMQTTConnectRetryInterval), + MQTTAutoReconnect: c.Bool(config.FlagNameMQTTAutoReconnect), + MQTTReconnectDelay: c.Duration(config.FlagNameMQTTReconnectDelay), } } @@ -505,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..2cfcac12 100644 --- a/internal/transport/mqtt.go +++ b/internal/transport/mqtt.go @@ -99,6 +99,17 @@ func NewMQTTTransport(clientID string, brokers []string, tlsConfig *tls.Config) t.events <- TransporterEventDisconnected }) + opts.SetReconnectingHandler(func(c mqtt.Client, co *mqtt.ClientOptions) { + if config.DefaultConfig.MQTTReconnectDelay > 0 { + log.Infof( + "delaying for %v before reconnecting...", + config.DefaultConfig.MQTTReconnectDelay, + ) + time.Sleep(config.DefaultConfig.MQTTReconnectDelay) + } + log.Debugf("reconnecting to broker: %v", co.Servers) + }) + data, err := json.Marshal(&yggdrasil.ConnectionStatus{ Type: yggdrasil.MessageTypeConnectionStatus, MessageID: uuid.New().String(),