mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-06-05 21:56:45 +00:00
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:
parent
7aa0c7f6cc
commit
2a9d3d4b47
10
util/mqtt.go
10
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user