put some global objects into an objects to avoid inter service testing issues

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2024-05-16 14:14:16 +02:00
parent 94e3db7d8d
commit 444a6c4f5a
No known key found for this signature in database

View File

@ -18,8 +18,6 @@ import (
) )
var MonitorMQTTClient util.MQTTClientI var MonitorMQTTClient util.MQTTClientI
var clientMutex sync.Mutex
var localMqttClient util.MQTTClientI
type MqttMonitor struct { type MqttMonitor struct {
db *leveldb.DB db *leveldb.DB
@ -34,6 +32,8 @@ type MqttMonitor struct {
maxRetries time.Duration maxRetries time.Duration
lostConnection bool lostConnection bool
lostConnectionMutex sync.Mutex lostConnectionMutex sync.Mutex
clientMutex sync.Mutex
localMqttClient util.MQTTClientI
} }
func (mms *MqttMonitor) Terminate() { func (mms *MqttMonitor) Terminate() {
@ -202,15 +202,15 @@ func (mms *MqttMonitor) onConnectionLost(_ mqtt.Client, err error) {
} }
func (mms *MqttMonitor) MonitorActiveParticipants() { func (mms *MqttMonitor) MonitorActiveParticipants() {
clientMutex.Lock() mms.clientMutex.Lock()
if localMqttClient != nil { if mms.localMqttClient != nil {
log.Println("[app] [Monitor] client is still working") log.Println("[app] [Monitor] client is still working")
clientMutex.Unlock() mms.clientMutex.Unlock()
return return
} }
localMqttClient = mms.lazyLoadMonitorMQTTClient() mms.localMqttClient = mms.lazyLoadMonitorMQTTClient()
mqttClient := localMqttClient mqttClient := mms.localMqttClient
clientMutex.Unlock() mms.clientMutex.Unlock()
// Maximum reconnection attempts (adjust as needed) // Maximum reconnection attempts (adjust as needed)
mms.SetMaxRetries() mms.SetMaxRetries()
@ -256,9 +256,9 @@ func (mms *MqttMonitor) MonitorActiveParticipants() {
log.Println("[app] [Monitor] Reached maximum reconnection attempts. Exiting. New client will be activated soon.") log.Println("[app] [Monitor] Reached maximum reconnection attempts. Exiting. New client will be activated soon.")
} }
clientMutex.Lock() mms.clientMutex.Lock()
localMqttClient = nil mms.localMqttClient = nil
clientMutex.Unlock() mms.clientMutex.Unlock()
} }
func SendUpdateMessage(mqttClient util.MQTTClientI) { func SendUpdateMessage(mqttClient util.MQTTClientI) {