From 2a9d3d4b471116bd9d570ba68848ffc23f9ed3c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Eckel?= Date: Mon, 22 Jan 2024 11:03:11 +0100 Subject: [PATCH] lazy loading of config.GetConfig calls (#293) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * the execution of Config code is not determinitic within init methods. That's why we apply lazy loading. Signed-off-by: Jürgen Eckel --- util/mqtt.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/util/mqtt.go b/util/mqtt.go index 0c263de..302d2dc 100644 --- a/util/mqtt.go +++ b/util/mqtt.go @@ -33,7 +33,11 @@ const ( MqttCmdPrefix = "cmnd/" ) -func init() { +func lazyLoadMQTTClient() { + if MQTTClient != nil { + return + } + conf := config.GetConfig() hostPort := net.JoinHostPort(conf.MqttDomain, strconv.FormatInt(int64(conf.MqttPort), 10)) uri := fmt.Sprintf("tcp://%s", hostPort) @@ -43,7 +47,9 @@ func init() { opts.SetUsername(conf.MqttUser) opts.SetPassword(conf.MqttPassword) MQTTClient = mqtt.NewClient(opts) +} +func init() { mqttMachineByAddressAvailabilityMapping = make(map[string]bool) } @@ -61,6 +67,7 @@ func SendMqttPopInitMessagesToServer(ctx sdk.Context, challenge types.Challenge) } func sendMqttPopInitMessages(challenge types.Challenge) (err error) { + lazyLoadMQTTClient() if token := MQTTClient.Connect(); token.Wait() && token.Error() != nil { err = token.Error() return @@ -86,6 +93,7 @@ func sendMqttPopInitMessages(challenge types.Challenge) (err error) { } func GetMqttStatusOfParticipant(address string) (isAvailable bool, err error) { + lazyLoadMQTTClient() if token := MQTTClient.Connect(); token.Wait() && token.Error() != nil { err = token.Error() return