Skip to content

Commit cc089ff

Browse files
Merge pull request #72 from marcoferrer/bugfix-unblock
check for nil in lifo unblock
2 parents cba3c8a + b4c8f11 commit cc089ff

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

limiter/lifo_blocking.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -119,26 +119,29 @@ func (l *LifoBlockingListener) unblock() {
119119
l.limiter.mu.Lock()
120120
defer l.limiter.mu.Unlock()
121121

122-
if l.limiter.backlog.len() > 0 {
123-
124-
evict, nextEvent := l.limiter.backlog.peek()
125-
listener, ok := l.limiter.delegate.Acquire(nextEvent.ctx)
126-
127-
if ok && listener != nil {
128-
// We successfully acquired a listener from the
129-
// delegate. Now we can evict the element from
130-
// the queue
131-
evict()
132-
133-
// If the listener is not accepted due to subtle timings
134-
// between setListener being invoked and the element
135-
// expiration elapsing we need to be sure to release it.
136-
if accepted := nextEvent.setListener(listener); !accepted {
137-
listener.OnIgnore()
138-
}
122+
evict, nextEvent := l.limiter.backlog.peek()
123+
124+
// The queue is empty
125+
if nextEvent == nil {
126+
return
127+
}
128+
129+
listener, ok := l.limiter.delegate.Acquire(nextEvent.ctx)
130+
131+
if ok && listener != nil {
132+
// We successfully acquired a listener from the
133+
// delegate. Now we can evict the element from
134+
// the queue
135+
evict()
136+
137+
// If the listener is not accepted due to subtle timings
138+
// between setListener being invoked and the element
139+
// expiration elapsing we need to be sure to release it.
140+
if accepted := nextEvent.setListener(listener); !accepted {
141+
listener.OnIgnore()
139142
}
140-
// otherwise: still can't acquire the limit. unblock will be called again next time the limit is released.
141143
}
144+
// otherwise: still can't acquire the limit. unblock will be called again next time the limit is released.
142145
}
143146

144147
// OnDropped is called to indicate the request failed and was dropped due to being rejected by an external limit or
@@ -175,7 +178,6 @@ type LifoBlockingLimiter struct {
175178
maxBacklogTimeout time.Duration
176179

177180
backlog lifoQueue
178-
c *sync.Cond
179181
mu sync.RWMutex
180182
}
181183

@@ -191,13 +193,11 @@ func NewLifoBlockingLimiter(
191193
if maxBacklogTimeout == 0 {
192194
maxBacklogTimeout = time.Millisecond * 1000
193195
}
194-
mu := sync.Mutex{}
195196
return &LifoBlockingLimiter{
196197
delegate: delegate,
197198
maxBacklogSize: uint64(maxBacklogSize),
198199
maxBacklogTimeout: maxBacklogTimeout,
199200
backlog: lifoQueue{},
200-
c: sync.NewCond(&mu),
201201
}
202202
}
203203

0 commit comments

Comments
 (0)