mirror of
https://github.com/status-im/nim-chronos.git
synced 2025-01-20 16:29:12 +00:00
ratelimit: set cancellation earlier (#402)
future may be completed by worker before cancellation is set
This commit is contained in:
parent
a6ac5f2213
commit
2ef34c7339
@ -102,10 +102,6 @@ proc consume*(bucket: TokenBucket, tokens: int): Future[void] =
|
||||
retFuture.complete()
|
||||
return retFuture
|
||||
|
||||
bucket.pendingRequests.add(BucketWaiter(future: retFuture, value: tokens))
|
||||
if isNil(bucket.workFuture) or bucket.workFuture.finished():
|
||||
bucket.workFuture = worker(bucket)
|
||||
|
||||
proc cancellation(udata: pointer) =
|
||||
for index in 0..<bucket.pendingRequests.len:
|
||||
if bucket.pendingRequests[index].future == retFuture:
|
||||
@ -115,6 +111,12 @@ proc consume*(bucket: TokenBucket, tokens: int): Future[void] =
|
||||
bucket.manuallyReplenished.fire()
|
||||
break
|
||||
retFuture.cancelCallback = cancellation
|
||||
|
||||
bucket.pendingRequests.add(BucketWaiter(future: retFuture, value: tokens))
|
||||
|
||||
if isNil(bucket.workFuture) or bucket.workFuture.finished():
|
||||
bucket.workFuture = worker(bucket)
|
||||
|
||||
return retFuture
|
||||
|
||||
proc replenish*(bucket: TokenBucket, tokens: int) =
|
||||
|
Loading…
x
Reference in New Issue
Block a user