From 47ef5baee3ee1fa7d6afb8537761bb850b82fea3 Mon Sep 17 00:00:00 2001 From: gohumble <55599638+gohumble@users.noreply.github.com> Date: Fri, 24 Dec 2021 14:25:35 +0100 Subject: [PATCH] fix mutex final last v2 maybe (#202) * remove mutex on unlock * add pprof * fix mutex lock * fix logs --- internal/runtime/mutex/mutex.go | 12 +++++++++--- main.go | 3 +++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/internal/runtime/mutex/mutex.go b/internal/runtime/mutex/mutex.go index aef00ba..22dd739 100644 --- a/internal/runtime/mutex/mutex.go +++ b/internal/runtime/mutex/mutex.go @@ -39,7 +39,7 @@ func LockWithContext(ctx context.Context, s string) { if nLocks == 0 { Lock(s) } else { - log.Tracef("[Mutex] LockSoft (nLocks: %d)", nLocks) + log.Tracef("[Mutex] Skip lock (nLocks: %d)", nLocks) } nLocks++ mutexMap.Set(fmt.Sprintf("nLocks:%s", uid), nLocks) @@ -58,7 +58,7 @@ func UnlockWithContext(ctx context.Context, s string) { if nLocks == 0 { Unlock(s) } else { - log.Tracef("[Mutex] UnlockSoft with nLocks: %d ", nLocks) + log.Tracef("[Mutex] Skip unlock (nLocks: %d)", nLocks) } Unlock(fmt.Sprintf("mutex-sync:%s:%s", s, uid)) mutexMap.Remove(fmt.Sprintf("mutex-sync:%s:%s", s, uid)) @@ -66,9 +66,13 @@ func UnlockWithContext(ctx context.Context, s string) { // Lock locks a mutex in the mutexMap. func Lock(s string) { + log.Tracef("[Mutex] Attempt Lock %s", s) if m, ok := mutexMap.Get(s); ok { + log.Tracef("[Mutex] Attempt %s already in mutexMap", s) m.(*sync.Mutex).Lock() + mutexMap.Set(s, m) } else { + log.Tracef("[Mutex] Attempt %s not in mutexMap", s) m := &sync.Mutex{} m.Lock() mutexMap.Set(s, m) @@ -80,7 +84,9 @@ func Lock(s string) { func Unlock(s string) { if m, ok := mutexMap.Get(s); ok { log.Tracef("[Mutex] Unlock %s", s) + mutexMap.Remove(s) m.(*sync.Mutex).Unlock() - + } else { + log.Errorf("[Mutex] ⚠⚠⚠️ Unlock %s not in mutexMap. Skip.", s) } } diff --git a/main.go b/main.go index 8d27736..b0985e7 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "net/http" "runtime/debug" "github.com/LightningTipBot/LightningTipBot/internal/lnbits/webhook" @@ -8,6 +9,7 @@ import ( "github.com/LightningTipBot/LightningTipBot/internal/price" "github.com/LightningTipBot/LightningTipBot/internal/telegram" log "github.com/sirupsen/logrus" + _ "net/http/pprof" ) // setLogger will initialize the log format @@ -22,6 +24,7 @@ func setLogger() { func main() { // set logger setLogger() + go http.ListenAndServe("0.0.0.0:6060", nil) defer withRecovery() bot := telegram.NewBot() webhook.NewServer(&bot)