From 07ccc6cc2886d6b76e98e3dcdbd2668e9986c162 Mon Sep 17 00:00:00 2001 From: LightningTipBot <88730856+LightningTipBot@users.noreply.github.com> Date: Sun, 26 Dec 2021 14:51:10 +0100 Subject: [PATCH] Unlock admin (#240) * add mutex http endpoints * remove mutex by id * remove mutex by id Co-authored-by: lngohumble --- internal/runtime/mutex/mutex.go | 12 +++++++----- main.go | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/runtime/mutex/mutex.go b/internal/runtime/mutex/mutex.go index 77e4218..b92bede 100644 --- a/internal/runtime/mutex/mutex.go +++ b/internal/runtime/mutex/mutex.go @@ -3,6 +3,7 @@ package mutex import ( "context" "fmt" + "github.com/gorilla/mux" "net/http" "sync" @@ -21,13 +22,14 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request) { } func UnlockHTTP(w http.ResponseWriter, r *http.Request) { - var i []string - for k, m := range mutexMap.Items() { - i = append(i, k) + vars := mux.Vars(r) + if m, ok := mutexMap.Get(vars["id"]); ok { m.(*sync.Mutex).Unlock() + w.Write([]byte(fmt.Sprintf("Unlocked %s mutexe.\nCurrent number of locks: %d\nLocks: %+v", + vars["id"], len(mutexMap.Keys()), mutexMap.Keys()))) + return } - w.Write([]byte(fmt.Sprintf("Unlocked %d mutexes. \n%+v\nCurrent number of locks: %d\nLocks: %+v", - len(i), i, len(mutexMap.Keys()), mutexMap.Keys()))) + w.Write([]byte(fmt.Sprintf("Mutex %s not found!", vars["id"]))) } // checkSoftLock checks in mutexMap how often an existing mutex was already SoftLocked. diff --git a/main.go b/main.go index f37a34a..960e5ab 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ func main() { router := mux.NewRouter() router.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux) router.Handle("/mutex", http.HandlerFunc(mutex.ServeHTTP)) - router.Handle("/mutex/unlock", http.HandlerFunc(mutex.UnlockHTTP)) + router.Handle("/mutex/unlock/{id}", http.HandlerFunc(mutex.UnlockHTTP)) go http.ListenAndServe("0.0.0.0:6060", router) defer withRecovery() bot := telegram.NewBot()