refactor(mqtt): defer unlock where applicable

also directly return values instead of assigning them first.

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
This commit is contained in:
Julian Strobl 2024-11-18 10:32:20 +01:00
parent ae086b9b37
commit 989be4c4aa
No known key found for this signature in database
GPG Key ID: E0A8F9AD733499A7
3 changed files with 11 additions and 15 deletions

View File

@ -31,8 +31,8 @@ func (mms *MqttMonitor) AddParticipant(address string, lastSeenTS int64) (err er
mms.setNumDBElements(mms.getNumDBElements() + 1)
}
mms.dbMutex.Lock()
defer mms.dbMutex.Unlock()
err = mms.db.Put([]byte(address), lastSeenBytes, nil)
mms.dbMutex.Unlock()
if err != nil {
Log("error storing addresses in DB: " + err.Error())
} else {
@ -45,9 +45,8 @@ func (mms *MqttMonitor) AddParticipant(address string, lastSeenTS int64) (err er
func (mms *MqttMonitor) deleteEntry(key []byte) (err error) {
mms.setNumDBElements(mms.getNumDBElements() - 1)
mms.dbMutex.Lock()
err = mms.db.Delete(key, nil)
mms.dbMutex.Unlock()
return
defer mms.dbMutex.Unlock()
return mms.db.Delete(key, nil)
}
func (mms *MqttMonitor) getAmountOfElements() (amount int64, err error) {

View File

@ -21,8 +21,8 @@ var mqttMonitorInstance MQTTMonitorClientI
func SetMqttMonitorInstance(monitorInstance MQTTMonitorClientI) {
monitorMutex.Lock()
defer monitorMutex.Unlock()
mqttMonitorInstance = monitorInstance
monitorMutex.Unlock()
}
func GetMqttMonitorInstance() (monitorInstance MQTTMonitorClientI) {
@ -54,9 +54,8 @@ func LazyMqttMonitorLoader(logger log.Logger, homeDir string) {
func SelectPoPParticipantsOutOfActiveActors() (challenger string, challengee string, err error) {
monitorMutex.RLock()
challenger, challengee, err = mqttMonitorInstance.SelectPoPParticipantsOutOfActiveActors()
monitorMutex.RUnlock()
return
defer monitorMutex.RUnlock()
return mqttMonitorInstance.SelectPoPParticipantsOutOfActiveActors()
}
func Start() (err error) {
@ -66,9 +65,8 @@ func Start() (err error) {
func AddParticipant(address string, lastSeenTS int64) (err error) {
monitorMutex.RLock()
err = mqttMonitorInstance.AddParticipant(address, lastSeenTS)
monitorMutex.RUnlock()
return
defer monitorMutex.RUnlock()
return mqttMonitorInstance.AddParticipant(address, lastSeenTS)
}
func GetActiveActorCount() (count uint64) {

View File

@ -40,15 +40,14 @@ type MqttMonitor struct {
func (mms *MqttMonitor) Terminate() {
mms.terminationMutex.Lock()
defer mms.terminationMutex.Unlock()
mms.isTerminated = true
mms.terminationMutex.Unlock()
}
func (mms *MqttMonitor) IsTerminated() (isTerminated bool) {
mms.terminationMutex.RLock()
isTerminated = mms.isTerminated
mms.terminationMutex.RUnlock()
return
defer mms.terminationMutex.RUnlock()
return mms.isTerminated
}
func (mms *MqttMonitor) getNumDBElements() int64 {