|
@@ -8,8 +8,8 @@ import (
|
|
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
"github.com/gravitl/netmaker/logger"
|
|
"github.com/gravitl/netmaker/logger"
|
|
- "github.com/gravitl/netmaker/logic"
|
|
|
|
"github.com/gravitl/netmaker/servercfg"
|
|
"github.com/gravitl/netmaker/servercfg"
|
|
|
|
+ "golang.org/x/exp/slog"
|
|
)
|
|
)
|
|
|
|
|
|
// KEEPALIVE_TIMEOUT - time in seconds for timeout
|
|
// KEEPALIVE_TIMEOUT - time in seconds for timeout
|
|
@@ -27,12 +27,12 @@ var mqclient mqtt.Client
|
|
func setMqOptions(user, password string, opts *mqtt.ClientOptions) {
|
|
func setMqOptions(user, password string, opts *mqtt.ClientOptions) {
|
|
broker, _ := servercfg.GetMessageQueueEndpoint()
|
|
broker, _ := servercfg.GetMessageQueueEndpoint()
|
|
opts.AddBroker(broker)
|
|
opts.AddBroker(broker)
|
|
- id := logic.RandomString(23)
|
|
|
|
- opts.ClientID = id
|
|
|
|
|
|
+ opts.ClientID = user
|
|
opts.SetUsername(user)
|
|
opts.SetUsername(user)
|
|
opts.SetPassword(password)
|
|
opts.SetPassword(password)
|
|
opts.SetAutoReconnect(true)
|
|
opts.SetAutoReconnect(true)
|
|
opts.SetConnectRetry(true)
|
|
opts.SetConnectRetry(true)
|
|
|
|
+ opts.SetCleanSession(true)
|
|
opts.SetConnectRetryInterval(time.Second * 4)
|
|
opts.SetConnectRetryInterval(time.Second * 4)
|
|
opts.SetKeepAlive(time.Minute)
|
|
opts.SetKeepAlive(time.Minute)
|
|
opts.SetCleanSession(true)
|
|
opts.SetCleanSession(true)
|
|
@@ -40,7 +40,7 @@ func setMqOptions(user, password string, opts *mqtt.ClientOptions) {
|
|
}
|
|
}
|
|
|
|
|
|
// SetupMQTT creates a connection to broker and return client
|
|
// SetupMQTT creates a connection to broker and return client
|
|
-func SetupMQTT() {
|
|
|
|
|
|
+func SetupMQTT(fatal bool) {
|
|
if servercfg.GetBrokerType() == servercfg.EmqxBrokerType {
|
|
if servercfg.GetBrokerType() == servercfg.EmqxBrokerType {
|
|
if emqx.GetType() == servercfg.EmqxOnPremDeploy {
|
|
if emqx.GetType() == servercfg.EmqxOnPremDeploy {
|
|
time.Sleep(10 * time.Second) // wait for the REST endpoint to be ready
|
|
time.Sleep(10 * time.Second) // wait for the REST endpoint to be ready
|
|
@@ -70,6 +70,7 @@ func SetupMQTT() {
|
|
|
|
|
|
opts := mqtt.NewClientOptions()
|
|
opts := mqtt.NewClientOptions()
|
|
setMqOptions(servercfg.GetMqUserName(), servercfg.GetMqPassword(), opts)
|
|
setMqOptions(servercfg.GetMqUserName(), servercfg.GetMqPassword(), opts)
|
|
|
|
+ logger.Log(0, "Mq Client Connecting with Random ID: ", opts.ClientID)
|
|
opts.SetOnConnectHandler(func(client mqtt.Client) {
|
|
opts.SetOnConnectHandler(func(client mqtt.Client) {
|
|
serverName := servercfg.GetServer()
|
|
serverName := servercfg.GetServer()
|
|
if token := client.Subscribe(fmt.Sprintf("update/%s/#", serverName), 0, mqtt.MessageHandler(UpdateNode)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
|
|
if token := client.Subscribe(fmt.Sprintf("update/%s/#", serverName), 0, mqtt.MessageHandler(UpdateNode)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
|
|
@@ -92,6 +93,13 @@ func SetupMQTT() {
|
|
opts.SetOrderMatters(false)
|
|
opts.SetOrderMatters(false)
|
|
opts.SetResumeSubs(true)
|
|
opts.SetResumeSubs(true)
|
|
})
|
|
})
|
|
|
|
+ opts.SetConnectionLostHandler(func(c mqtt.Client, e error) {
|
|
|
|
+ slog.Warn("detected broker connection lost", "err", e.Error())
|
|
|
|
+ c.Disconnect(250)
|
|
|
|
+ slog.Info("re-initiating MQ connection")
|
|
|
|
+ SetupMQTT(false)
|
|
|
|
+
|
|
|
|
+ })
|
|
mqclient = mqtt.NewClient(opts)
|
|
mqclient = mqtt.NewClient(opts)
|
|
tperiod := time.Now().Add(10 * time.Second)
|
|
tperiod := time.Now().Add(10 * time.Second)
|
|
for {
|
|
for {
|
|
@@ -99,9 +107,16 @@ func SetupMQTT() {
|
|
logger.Log(2, "unable to connect to broker, retrying ...")
|
|
logger.Log(2, "unable to connect to broker, retrying ...")
|
|
if time.Now().After(tperiod) {
|
|
if time.Now().After(tperiod) {
|
|
if token.Error() == nil {
|
|
if token.Error() == nil {
|
|
- logger.FatalLog("could not connect to broker, token timeout, exiting ...")
|
|
|
|
|
|
+ if fatal {
|
|
|
|
+ logger.FatalLog("could not connect to broker, token timeout, exiting ...")
|
|
|
|
+ }
|
|
|
|
+ logger.Log(0, "could not connect to broker, token timeout, exiting ...")
|
|
|
|
+
|
|
} else {
|
|
} else {
|
|
- logger.FatalLog("could not connect to broker, exiting ...", token.Error().Error())
|
|
|
|
|
|
+ if fatal {
|
|
|
|
+ logger.FatalLog("could not connect to broker, exiting ...", token.Error().Error())
|
|
|
|
+ }
|
|
|
|
+ logger.Log(0, "could not connect to broker, exiting ...", token.Error().Error())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
@@ -125,7 +140,7 @@ func Keepalive(ctx context.Context) {
|
|
|
|
|
|
// IsConnected - function for determining if the mqclient is connected or not
|
|
// IsConnected - function for determining if the mqclient is connected or not
|
|
func IsConnected() bool {
|
|
func IsConnected() bool {
|
|
- return mqclient != nil && mqclient.IsConnected()
|
|
|
|
|
|
+ return mqclient != nil && mqclient.IsConnectionOpen()
|
|
}
|
|
}
|
|
|
|
|
|
// CloseClient - function to close the mq connection from server
|
|
// CloseClient - function to close the mq connection from server
|