lazy loading of config.GetConfig calls (#293)

* the execution of Config code is not determinitic within init methods. That's why we apply lazy loading.

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2024-01-22 11:03:11 +01:00 committed by GitHub
parent 7aa0c7f6cc
commit 2a9d3d4b47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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