-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c74ca81
commit 9ec8988
Showing
13 changed files
with
164 additions
and
84 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,40 @@ | ||
import asyncio | ||
import random | ||
import time | ||
|
||
|
||
|
||
class ExponentialBackoff: | ||
def __init__(self, base_interval=1, max_interval=60, jitter_factor=0.5): | ||
self.base_interval = base_interval | ||
self.max_interval = max_interval | ||
self.jitter_factor = jitter_factor | ||
self.current_interval = base_interval | ||
self.last_call_time = time.time() | ||
|
||
def consume(self): | ||
current_time = time.time() | ||
# Check if wait was called after a 10-minute interval | ||
if current_time - self.last_call_time >= 600: | ||
self.current_interval = self.base_interval | ||
else: | ||
# Increase the backoff interval, up to the max_interval | ||
self.current_interval = min(self.current_interval * 2, self.max_interval) | ||
|
||
# Apply jitter | ||
jitter = self.current_interval * self.jitter_factor * random.random() | ||
backoff_with_jitter = self.current_interval + jitter - (self.jitter_factor / 2 * self.current_interval) | ||
backoff_with_jitter = max(self.base_interval, backoff_with_jitter) | ||
|
||
return backoff_with_jitter, current_time | ||
|
||
def sleep(self): | ||
backoff_with_jitter, current_time = self.consume() | ||
time.sleep(backoff_with_jitter) | ||
self.last_call_time = current_time | ||
|
||
async def sleepAsync(self): | ||
backoff_with_jitter, current_time = self.consume() | ||
await asyncio.sleep(backoff_with_jitter) | ||
self.last_call_time = current_time | ||
|
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,26 @@ | ||
import time | ||
|
||
class FrameLimiter: | ||
def __init__(self, max_rate_per_second): | ||
self._stream_info = { | ||
'lastFrameTime': 0, | ||
'frameCount': 0, | ||
'maxRate': max_rate_per_second, | ||
} | ||
|
||
def set_max_rate(self, max_rate_per_second): | ||
self._stream_info['maxRate'] = max_rate_per_second | ||
|
||
def accept(self, points=1): | ||
current_time = int(time.time() * 1000) | ||
stream_info = self._stream_info | ||
|
||
elapsed_time = current_time - stream_info['lastFrameTime'] | ||
desired_interval = 1000 / (stream_info['maxRate'] * points) | ||
|
||
if elapsed_time >= desired_interval or stream_info['frameCount'] == 0: | ||
stream_info['lastFrameTime'] = current_time | ||
stream_info['frameCount'] += 1 | ||
return True | ||
|
||
return False |
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
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
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
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
Oops, something went wrong.