Unlock admin (#240)

* add mutex http endpoints

* remove mutex by id

* remove mutex by id

Co-authored-by: lngohumble <lngohumble@gmail.com>
This commit is contained in:
LightningTipBot 2021-12-26 14:51:10 +01:00 committed by GitHub
parent 2ef798e56a
commit 07ccc6cc28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 6 deletions

View file

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

View file

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