This repository has been archived by the owner on Aug 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[util] fix SimpleLimiter not limiting correctly
This would cause our GatewayClient to get rate limited when guild member syncing was enabled for clients with a large number of guilds. The bug had to do with the limiter releaseing all the requests after the initial period it blocked for. This was resolved by rewriting the SimpleLimiter to use a semaphore.
- Loading branch information
1 parent
7b47011
commit 32085b4
Showing
2 changed files
with
52 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import time | ||
import gevent | ||
from unittest import TestCase | ||
|
||
from disco.util.limiter import SimpleLimiter | ||
|
||
|
||
class TestSimpleLimiter(TestCase): | ||
def test_many_wait_ratelimiter(self): | ||
limit = SimpleLimiter(5, 1) | ||
many = [] | ||
|
||
def check(lock): | ||
limit.check() | ||
lock.release() | ||
|
||
start = time.time() | ||
for _ in range(16): | ||
lock = gevent.lock.Semaphore() | ||
lock.acquire() | ||
many.append(lock) | ||
gevent.spawn(check, lock) | ||
|
||
for item in many: | ||
item.acquire() | ||
|
||
self.assertGreater(time.time() - start, 3) | ||
|
||
def test_nowait_ratelimiter(self): | ||
limit = SimpleLimiter(5, 1) | ||
|
||
start = time.time() | ||
for _ in range(5): | ||
limit.check() | ||
|
||
self.assertLess(time.time() - start, 1) | ||
|
||
def test_single_wait_ratelimiter(self): | ||
limit = SimpleLimiter(5, 1) | ||
|
||
start = time.time() | ||
for _ in range(10): | ||
limit.check() | ||
|
||
|
||
self.assertEqual(int(time.time() - start), 1) |