Delay RateLimit call after delayed call triggered
This commit is contained in:
parent
9d09890457
commit
5026f1b0a8
2 changed files with 12 additions and 7 deletions
|
@ -37,6 +37,7 @@ class TestRateLimit:
|
||||||
assert RateLimit.call("counting", allowed_again=0.1, func=obj1.count) == "counted"
|
assert RateLimit.call("counting", allowed_again=0.1, func=obj1.count) == "counted"
|
||||||
assert around(time.time() - s, 0.1) # Delays second call within interval
|
assert around(time.time() - s, 0.1) # Delays second call within interval
|
||||||
assert obj1.counted == 2
|
assert obj1.counted == 2
|
||||||
|
time.sleep(0.1) # Wait the cooldown time
|
||||||
|
|
||||||
# Call 3 times async
|
# Call 3 times async
|
||||||
s = time.time()
|
s = time.time()
|
||||||
|
@ -50,6 +51,11 @@ class TestRateLimit:
|
||||||
assert [thread.value for thread in threads] == ["counted", "counted", "counted"]
|
assert [thread.value for thread in threads] == ["counted", "counted", "counted"]
|
||||||
assert around(time.time() - s, 0.2)
|
assert around(time.time() - s, 0.2)
|
||||||
|
|
||||||
|
# Wait 0.1s cooldown
|
||||||
|
assert not RateLimit.isAllowed("counting", 0.1)
|
||||||
|
time.sleep(0.1)
|
||||||
|
assert RateLimit.isAllowed("counting", 0.1)
|
||||||
|
|
||||||
# No queue = instant again
|
# No queue = instant again
|
||||||
s = time.time()
|
s = time.time()
|
||||||
assert RateLimit.isAllowed("counting", 0.1)
|
assert RateLimit.isAllowed("counting", 0.1)
|
||||||
|
@ -83,12 +89,12 @@ class TestRateLimit:
|
||||||
assert obj1.counted == 2
|
assert obj1.counted == 2
|
||||||
assert obj1.last_called == "call #4"
|
assert obj1.last_called == "call #4"
|
||||||
|
|
||||||
# Allowed again instantly
|
# Just called, not allowed again
|
||||||
assert RateLimit.isAllowed("counting async", 0.1)
|
assert not RateLimit.isAllowed("counting async", 0.1)
|
||||||
s = time.time()
|
s = time.time()
|
||||||
RateLimit.callAsync("counting async", allowed_again=0.1, func=obj1.count, back="call #5").join()
|
t4 = RateLimit.callAsync("counting async", allowed_again=0.1, func=obj1.count, back="call #5").join()
|
||||||
assert obj1.counted == 3
|
assert obj1.counted == 3
|
||||||
assert around(time.time() - s, 0.0)
|
assert around(time.time() - s, 0.1)
|
||||||
assert not RateLimit.isAllowed("counting async", 0.1)
|
assert not RateLimit.isAllowed("counting async", 0.1)
|
||||||
time.sleep(0.11)
|
time.sleep(0.11)
|
||||||
assert RateLimit.isAllowed("counting async", 0.1)
|
assert RateLimit.isAllowed("counting async", 0.1)
|
||||||
|
|
|
@ -37,7 +37,7 @@ def delayLeft(event, allowed_again=10):
|
||||||
def callQueue(event):
|
def callQueue(event):
|
||||||
func, args, kwargs, thread = queue_db[event]
|
func, args, kwargs, thread = queue_db[event]
|
||||||
log.debug("Calling: %s" % event)
|
log.debug("Calling: %s" % event)
|
||||||
del called_db[event]
|
called(event)
|
||||||
del queue_db[event]
|
del queue_db[event]
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
@ -78,8 +78,7 @@ def call(event, allowed_again=10, func=None, *args, **kwargs):
|
||||||
called(event, time_left)
|
called(event, time_left)
|
||||||
time.sleep(time_left)
|
time.sleep(time_left)
|
||||||
back = func(*args, **kwargs)
|
back = func(*args, **kwargs)
|
||||||
if event in called_db:
|
called(event)
|
||||||
del called_db[event]
|
|
||||||
return back
|
return back
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue