Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[server] Fixed rare race condition for TokenBucket tryConsume()
TokenBucket relies on consumption itself to update/refill its tokens. There is a rare race condition that could happen and cause very few consumption requests to be rejected even when the token bucket is refilled and has sufficient tokens. The race is caused by the old implementation of tryConsume() return noRetryTryConsume(tokensToConsume) || (update() && noRetryTryConsume(tokensToConsume)); This is problematic because inside update() there is a small window where token is just updated by another thread so the first nextUpdateTime check will be false and hence returning false from update() and causing the tryConsume() to return false when in fact there are plenty of token available and was just refilled. An alternative fix would be to check the bucket within update() but we might as well always try to consume again.
- Loading branch information