From 6b2b2c4e31a46dff3ed1bab7e5e906c7f6188b14 Mon Sep 17 00:00:00 2001 From: John Jarvis Date: Sat, 22 Jun 2024 03:03:14 +0200 Subject: [PATCH] Fixes deadlock in ExecOnLimitReached (#107) 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. --- limiter/limiter.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/limiter/limiter.go b/limiter/limiter.go index c64a7f2..5561e6c 100644 --- a/limiter/limiter.go +++ b/limiter/limiter.go @@ -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) }