Skip to content

Commit

Permalink
Fixes deadlock in ExecOnLimitReached (#107)
Browse files Browse the repository at this point in the history
This moves the the mutex unlock in `ExecOnLimitReached` so
that it isn't around the function that gets executed.

Including the function in the lock may result in a deadlock if there are any method
calls in the function that call `RLock` again.
  • Loading branch information
jarv committed Jun 22, 2024
1 parent da07373 commit 6b2b2c4
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions limiter/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ func (l *Limiter) SetOnLimitReached(fn func(w http.ResponseWriter, r *http.Reque
// ExecOnLimitReached is thread-safe way of executing after-rejection function when limit is reached.
func (l *Limiter) ExecOnLimitReached(w http.ResponseWriter, r *http.Request) {
l.RLock()
defer l.RUnlock()

fn := l.onLimitReached
l.RUnlock()

if fn != nil {
fn(w, r)
}
Expand Down

0 comments on commit 6b2b2c4

Please sign in to comment.