fix mutex final last v2 maybe (#202)

* remove mutex on unlock

* add pprof

* fix mutex lock

* fix logs
This commit is contained in:
gohumble 2021-12-24 14:25:35 +01:00 committed by GitHub
parent 78eadf1a0e
commit 47ef5baee3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View file

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

View file

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