Skip to content

Commit

Permalink
fix for concurrent access issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jatin-chai committed Jun 17, 2021
1 parent 2807267 commit c460c47
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions v1/locks/eager/eager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Lock struct {
interval time.Duration
register struct {
sync.RWMutex
m map[string]int64
m *sync.Map
}
}

Expand All @@ -25,8 +25,10 @@ func New() *Lock {
interval: 5 * time.Second,
register: struct {
sync.RWMutex
m map[string]int64
}{m: make(map[string]int64)},
m *sync.Map
}{
m: new(sync.Map),
},
}
}

Expand All @@ -46,9 +48,9 @@ func (e *Lock) LockWithRetries(key string, value int64) error {
func (e *Lock) Lock(key string, value int64) error {
e.register.RLock()
defer e.register.RUnlock()
timeout, exist := e.register.m[key]
if !exist || time.Now().UnixNano() > timeout {
e.register.m[key] = value
timeout, exist := e.register.m.Load(key)
if !exist || time.Now().UnixNano() > timeout.(int64) {
e.register.m.Store(key, value)
return nil
}
return ErrEagerLockFailed
Expand Down

0 comments on commit c460c47

Please sign in to comment.