Skip to content

Commit

Permalink
Use regular lock for GetAndDelete (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
alei121 authored Mar 17, 2023
1 parent d92dc52 commit f9984f0
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions internal/pubsub/handlermap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ import (
// No thread running. Expiry checks only when Get or Set is called
// This is constantly getting called as the code consumes every second
type handlerMap struct {
expireDuration time.Duration
newExpireTime time.Time
expireDuration time.Duration
newExpireTime time.Time
currentHandlers map[string]func(*rpc.Response)
olderHandlers map[string]func(*rpc.Response)
sync.RWMutex
sync.Mutex
}

func NewHandlerMap(expireDuration time.Duration) *handlerMap {
return &handlerMap{
currentHandlers: make(map[string]func(*rpc.Response)),
olderHandlers: make(map[string]func(*rpc.Response)),
expireDuration: expireDuration,
newExpireTime: time.Now().Add(expireDuration),
expireDuration: expireDuration,
newExpireTime: time.Now().Add(expireDuration),
}
}

func (h *handlerMap) GetAndDelete(id string) func(*rpc.Response) {
h.expireCheck()
h.RLock()
defer h.RUnlock()
h.Lock()
defer h.Unlock()
handler := h.currentHandlers[id]
delete(h.currentHandlers, id)
if handler == nil {
Expand Down

0 comments on commit f9984f0

Please sign in to comment.