Skip to content

Commit

Permalink
chore: make limiter's end function idempotent (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
atzoum committed Jul 3, 2023
1 parent 5f83762 commit cd5826d
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions sync/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ type Limiter interface {

// Begin starts a new operation, blocking until a slot becomes available.
// Caller is expected to call the returned function to end the operation, otherwise
// the slot will be reserved indefinitely
// the slot will be reserved indefinitely. End can be called multiple times without any side effects
Begin(key string) (end func())

// BeginWithPriority starts a new operation, blocking until a slot becomes available, respecting the priority.
// Caller is expected to call the returned function to end the operation, otherwise
// the slot will be reserved indefinitely
// the slot will be reserved indefinitely. End can be called multiple times without any side effects
BeginWithPriority(key string, priority LimiterPriorityValue) (end func())
}

Expand Down Expand Up @@ -150,19 +150,23 @@ func (l *limiter) BeginWithPriority(key string, priority LimiterPriorityValue) (
tags := lo.Assign(l.tags, stats.Tags{"key": key})
l.stats.stat.NewTaggedStat(l.name+"_limiter_waiting", stats.TimerType, tags).Since(start)
start = time.Now()
var endOnce sync.Once

end = func() {
defer l.stats.stat.NewTaggedStat(l.name+"_limiter_working", stats.TimerType, tags).Since(start)
l.mu.Lock()
l.count--
if len(l.waitList) == 0 {
endOnce.Do(func() {
defer l.stats.stat.NewTaggedStat(l.name+"_limiter_working", stats.TimerType, tags).Since(start)
l.mu.Lock()
l.count--
if len(l.waitList) == 0 {
l.mu.Unlock()
return
}
next := heap.Pop(&l.waitList).(*queue.Item[chan struct{}])
l.count++
l.mu.Unlock()
return
}
next := heap.Pop(&l.waitList).(*queue.Item[chan struct{}])
l.count++
l.mu.Unlock()
next.Value <- struct{}{}
close(next.Value)
next.Value <- struct{}{}
close(next.Value)
})
}
return end
}
Expand Down

0 comments on commit cd5826d

Please sign in to comment.