mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 08:47:35 +08:00
retries for connecting to broker
This commit is contained in:
14
mq/mq.go
14
mq/mq.go
@@ -156,9 +156,17 @@ func SetupMQTT() mqtt.Client {
|
|||||||
opts := mqtt.NewClientOptions()
|
opts := mqtt.NewClientOptions()
|
||||||
broker := servercfg.GetMessageQueueEndpoint()
|
broker := servercfg.GetMessageQueueEndpoint()
|
||||||
opts.AddBroker(broker)
|
opts.AddBroker(broker)
|
||||||
client := mqtt.NewClient(opts)
|
tperiod := time.Now().Add(10 * time.Second)
|
||||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
for {
|
||||||
log.Fatal(token.Error())
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||||
|
logger.Log(2, "unable to connect to broker, retrying ...")
|
||||||
|
if time.Now().Afer(tperiod) {
|
||||||
|
log.Fatal(0, "could not connect to broker, exiting ...", token.Error())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
}
|
}
|
||||||
logger.Log(2, "connected to message queue", broker)
|
logger.Log(2, "connected to message queue", broker)
|
||||||
return client
|
return client
|
||||||
|
@@ -13,6 +13,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
"github.com/gravitl/netmaker/logger"
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
"github.com/gravitl/netmaker/netclient/config"
|
"github.com/gravitl/netmaker/netclient/config"
|
||||||
"github.com/gravitl/netmaker/netclient/local"
|
"github.com/gravitl/netmaker/netclient/local"
|
||||||
@@ -70,8 +71,17 @@ func SetupMQTT(cfg *config.ClientConfig) mqtt.Client {
|
|||||||
}
|
}
|
||||||
opts.SetDefaultPublishHandler(All)
|
opts.SetDefaultPublishHandler(All)
|
||||||
client := mqtt.NewClient(opts)
|
client := mqtt.NewClient(opts)
|
||||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
tperiod := time.Now().Add(10 * time.Second)
|
||||||
log.Fatal(token.Error())
|
for {
|
||||||
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||||
|
logger.Log(2, "unable to connect to broker, retrying ...")
|
||||||
|
if time.Now().Afer(tperiod) {
|
||||||
|
log.Fatal(0, "could not connect to broker, exiting ...", token.Error())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
}
|
}
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user