mirror of
https://github.com/gravitl/netmaker.git
synced 2025-11-03 02:53:29 +08:00
refactor client mq connection logic
This commit is contained in:
@@ -248,22 +248,29 @@ func setupMQTT(cfg *config.ClientConfig, publish bool) (mqtt.Client, error) {
|
|||||||
logger.Log(0, "detected broker connection lost for", cfg.Server.Server)
|
logger.Log(0, "detected broker connection lost for", cfg.Server.Server)
|
||||||
})
|
})
|
||||||
client := mqtt.NewClient(opts)
|
client := mqtt.NewClient(opts)
|
||||||
for token := client.Connect(); !token.WaitTimeout(30*time.Second) || token.Error() != nil; token = client.Connect() {
|
for count := 0; count < 3; count++ {
|
||||||
logger.Log(0, "unable to connect to broker, retrying ...")
|
if token := client.Connect(); !token.WaitTimeout(30*time.Second) || token.Error() != nil {
|
||||||
var err error
|
logger.Log(0, "unable to connect to broker, retrying ...")
|
||||||
if token.Error() == nil {
|
var err error = nil
|
||||||
err = errors.New("connect timeout")
|
if token.Error() == nil {
|
||||||
} else {
|
err = errors.New("connect timeout")
|
||||||
err = token.Error()
|
} else {
|
||||||
}
|
err = token.Error()
|
||||||
if err := checkBroker(cfg.Server.Server, cfg.Server.MQPort); err != nil {
|
}
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
err = checkBroker(cfg.Server.Server, cfg.Server.MQPort)
|
||||||
logger.Log(0, "could not connect to broker", cfg.Server.Server, err.Error())
|
}
|
||||||
if strings.Contains(err.Error(), "connectex") || strings.Contains(err.Error(), "connect timeout") {
|
logger.Log(0, "could not connect to broker", cfg.Server.Server, err.Error())
|
||||||
reRegisterWithServer(cfg)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
reRegisterWithServer(cfg)
|
||||||
|
}
|
||||||
|
//try after re-registering
|
||||||
|
if token := client.Connect(); !token.WaitTimeout(30*time.Second) || token.Error() != nil {
|
||||||
|
return client, errors.New("unable to connect to broker")
|
||||||
|
}
|
||||||
|
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user