Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0d2814d45c | ||
|
|
c9b26641ea |
@@ -17,6 +17,7 @@ var defaultTickerCoolDown = time.Second * 10
|
|||||||
type ResettableFunctionTicker struct {
|
type ResettableFunctionTicker struct {
|
||||||
Ticker *time.Ticker
|
Ticker *time.Ticker
|
||||||
ResetChan chan struct{} // channel used to reset the ticker
|
ResetChan chan struct{} // channel used to reset the ticker
|
||||||
|
StopChan chan struct{} // channel used to stop the ticker
|
||||||
duration time.Duration
|
duration time.Duration
|
||||||
Started bool
|
Started bool
|
||||||
name string
|
name string
|
||||||
@@ -44,6 +45,7 @@ func GetTicker(name string, option ...ResettableFunctionTickerOption) *Resettabl
|
|||||||
func NewResettableFunctionTicker(name string, option ...ResettableFunctionTickerOption) *ResettableFunctionTicker {
|
func NewResettableFunctionTicker(name string, option ...ResettableFunctionTickerOption) *ResettableFunctionTicker {
|
||||||
t := &ResettableFunctionTicker{
|
t := &ResettableFunctionTicker{
|
||||||
ResetChan: make(chan struct{}, 1),
|
ResetChan: make(chan struct{}, 1),
|
||||||
|
StopChan: make(chan struct{}, 1),
|
||||||
name: name,
|
name: name,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,6 +72,11 @@ func (t *ResettableFunctionTicker) Do(f func()) {
|
|||||||
case <-t.ResetChan:
|
case <-t.ResetChan:
|
||||||
// reset signal received. creating new ticker.
|
// reset signal received. creating new ticker.
|
||||||
t.Ticker = time.NewTicker(t.duration)
|
t.Ticker = time.NewTicker(t.duration)
|
||||||
|
case <-t.StopChan:
|
||||||
|
// reset signal received. creating new ticker.
|
||||||
|
t.Ticker = nil
|
||||||
|
tickerMap.Remove(t.name)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ package mutex
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/LightningTipBot/LightningTipBot/internal/runtime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
@@ -118,8 +120,14 @@ func Lock(s string) {
|
|||||||
} else {
|
} else {
|
||||||
m := &sync.Mutex{}
|
m := &sync.Mutex{}
|
||||||
m.Lock()
|
m.Lock()
|
||||||
|
tick := runtime.NewResettableFunctionTicker(s, runtime.WithDuration(time.Hour))
|
||||||
|
tick.Do(
|
||||||
|
func() {
|
||||||
|
Unlock(s)
|
||||||
|
})
|
||||||
// write into mutex map
|
// write into mutex map
|
||||||
mutexMapSync.Lock()
|
mutexMapSync.Lock()
|
||||||
|
mutexMap.Set(fmt.Sprintf("ticker_%s", s), tick)
|
||||||
mutexMap.Set(s, m)
|
mutexMap.Set(s, m)
|
||||||
mutexMapSync.Unlock()
|
mutexMapSync.Unlock()
|
||||||
}
|
}
|
||||||
@@ -131,6 +139,11 @@ func Unlock(s string) {
|
|||||||
mutexMapSync.Lock()
|
mutexMapSync.Lock()
|
||||||
if m, ok := mutexMap.Get(s); ok {
|
if m, ok := mutexMap.Get(s); ok {
|
||||||
mutexMap.Remove(s)
|
mutexMap.Remove(s)
|
||||||
|
tickerKey := fmt.Sprintf("ticker_%s", s)
|
||||||
|
if tick, ok := mutexMap.Get(tickerKey); ok {
|
||||||
|
tick.(*runtime.ResettableFunctionTicker).StopChan <- struct{}{}
|
||||||
|
mutexMap.Remove(tickerKey)
|
||||||
|
}
|
||||||
m.(*sync.Mutex).Unlock()
|
m.(*sync.Mutex).Unlock()
|
||||||
log.Tracef("[Mutex] Unlocked %s", s)
|
log.Tracef("[Mutex] Unlocked %s", s)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user