Skip to content

Commit

Permalink
fix atomic counter race
Browse files Browse the repository at this point in the history
  • Loading branch information
Termina1 committed Dec 26, 2024
1 parent e587387 commit d963e32
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions atomic_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ func NewAtomicCounter(db *Chotki, rid rdx.ID, offset uint64, updatePeriod time.D
}

func (a *AtomicCounter) load() (any, error) {
now := time.Now()
if a.data.Load() != nil && now.Sub(a.expiration) < 0 {
a.wg.Add(1)
return a.data.Load(), nil
}
a.lock.Lock()
defer a.lock.Unlock()
now := time.Now()
if a.data.Load() != nil && now.Sub(a.expiration) < 0 {
a.wg.Add(1)
return a.data.Load(), nil
}
a.wg.Wait()
Expand Down Expand Up @@ -93,6 +98,7 @@ func (a *AtomicCounter) load() (any, error) {
}
a.data.Store(data)
a.expiration = now.Add(a.updatePeriod)
a.wg.Add(1)
return data, nil
}

Expand All @@ -101,6 +107,7 @@ func (a *AtomicCounter) Get(ctx context.Context) (int64, error) {
if err != nil {
return 0, err
}
defer a.wg.Done()
switch c := data.(type) {
case *atomicNcounter:
return int64(c.total.Load()), nil
Expand All @@ -117,7 +124,6 @@ func (a *AtomicCounter) Increment(ctx context.Context, val int64) (int64, error)
if err != nil {
return 0, err
}
a.wg.Add(1)
defer a.wg.Done()
var dtlv []byte
var result int64
Expand Down

0 comments on commit d963e32

Please sign in to comment.