diff --git a/go.mod b/go.mod index 2d343b7..cb5880d 100644 --- a/go.mod +++ b/go.mod @@ -11,8 +11,8 @@ require ( ) require ( - github.com/aws/aws-sdk-go v1.51.3 - github.com/gorilla/schema v1.2.1 // indirect + github.com/aws/aws-sdk-go v1.51.8 + github.com/gorilla/schema v1.3.0 // indirect github.com/gorilla/websocket v1.5.1 // indirect github.com/joho/godotenv v1.5.1 github.com/replicate/replicate-go v0.18.1 diff --git a/go.sum b/go.sum index fbf17cc..20c9e7a 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/aws/aws-sdk-go v1.50.37 h1:gnAf6eYPSTb4QpVwugtWFqD07QXOoX7LewRrtLUx3l github.com/aws/aws-sdk-go v1.50.37/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= github.com/aws/aws-sdk-go v1.51.3 h1:OqSyEXcJwf/XhZNVpMRgKlLA9nmbo5X8dwbll4RWxq8= github.com/aws/aws-sdk-go v1.51.3/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= +github.com/aws/aws-sdk-go v1.51.8 h1:tD7gQq5XKuKdhA6UMEH26ZNQH0s+HbL95rzv/ACz5TQ= +github.com/aws/aws-sdk-go v1.51.8/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -10,6 +12,8 @@ github.com/diamondburned/arikawa/v3 v3.3.5/go.mod h1:KPkkWr40xmEithhd15XD2dbkVY8 github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/gorilla/schema v1.2.1 h1:tjDxcmdb+siIqkTNoV+qRH2mjYdr2hHe5MKXbp61ziM= github.com/gorilla/schema v1.2.1/go.mod h1:Dg5SSm5PV60mhF2NFaTV1xuYYj8tV8NOPRo4FggUMnM= +github.com/gorilla/schema v1.3.0 h1:rbciOzXAx3IB8stEFnfTwO3sYa6EWlQk79XdyustPDA= +github.com/gorilla/schema v1.3.0/go.mod h1:Dg5SSm5PV60mhF2NFaTV1xuYYj8tV8NOPRo4FggUMnM= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= diff --git a/lib/timer.go b/lib/timer.go new file mode 100644 index 0000000..d296c78 --- /dev/null +++ b/lib/timer.go @@ -0,0 +1,72 @@ +package lib + +import ( + "sync" + "time" +) + +var ( + mu sync.Mutex + instance *TimerManager +) + +type TimerManager struct { + timers map[string]time.Time + mu sync.Mutex +} + +func NewTimerManager() *TimerManager { + return &TimerManager{ + timers: make(map[string]time.Time), + } +} + +func GetInstance() *TimerManager { + mu.Lock() + defer mu.Unlock() + + if instance == nil { + instance = &TimerManager{ + timers: make(map[string]time.Time), + } + } + + return instance +} + +func (m *TimerManager) StartTimer(userID string, key string, duration time.Duration) { + m.mu.Lock() + defer m.mu.Unlock() + + m.timers[userID+":"+key] = time.Now().Add(duration) +} + +func (m *TimerManager) TimerRunning(userID string, key string) (bool, time.Duration) { + m.mu.Lock() + defer m.mu.Unlock() + + timerEnd, exists := m.timers[userID+":"+key] + if !exists { + return false, 0 + } + + if time.Now().After(timerEnd) { + delete(m.timers, userID+":"+key) + return false, 0 + } + + return true, time.Until(timerEnd) +} + +func CancelTimer(userID string, key string) { + manager := GetInstance() + + // Handle non-existent keys gracefully + if _, exists := manager.timers[userID+":"+key]; !exists { + return + } + + manager.mu.Lock() + defer manager.mu.Unlock() + delete(manager.timers, userID+":"+key) +}