-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(internal): revert rust rate limiter [backport 2.10] (#10229)
Rust, just revert back to the original pure python rate limiter. The impact here is fairly low, the Rust rate limiter is much faster, but this code isn't the slow part of a hot path, better to be safe than fast. Fixes #10002 - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
- Loading branch information
1 parent
5b2d1f8
commit 29d24e8
Showing
6 changed files
with
159 additions
and
236 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 |
---|---|---|
@@ -1,49 +0,0 @@ | ||
import typing | ||
|
||
class RateLimiter: | ||
""" | ||
A token bucket rate limiter implementation | ||
""" | ||
|
||
rate_limit: int | ||
time_window: float | ||
effective_rate: float | ||
current_window_rate: float | ||
prev_window_rate: typing.Optional[float] | ||
tokens: float | ||
max_tokens: float | ||
tokens_allowed: int | ||
tokens_total: int | ||
last_update_ns: float | ||
current_window_ns: float | ||
|
||
def __init__(self, rate_limit: int, time_window: float = 1e9): | ||
""" | ||
Constructor for RateLimiter | ||
:param rate_limit: The rate limit to apply for number of requests per second. | ||
rate limit > 0 max number of requests to allow per second, | ||
rate limit == 0 to disallow all requests, | ||
rate limit < 0 to allow all requests | ||
:type rate_limit: :obj:`int` | ||
:param time_window: The time window where the rate limit applies in nanoseconds. default value is 1 second. | ||
:type time_window: :obj:`float` | ||
""" | ||
def is_allowed(self, timestamp_ns: typing.Optional[int] = None) -> bool: | ||
""" | ||
Check whether the current request is allowed or not | ||
This method will also reduce the number of available tokens by 1 | ||
:param int timestamp_ns: timestamp in nanoseconds for the current request. [deprecated] | ||
:returns: Whether the current request is allowed or not | ||
:rtype: :obj:`bool` | ||
""" | ||
def _is_allowed(self, timestamp_ns: int) -> bool: | ||
""" | ||
Internal method to check whether the current request is allowed or not | ||
:param int timestamp_ns: timestamp in nanoseconds for the current request. | ||
:returns: Whether the current request is allowed or not | ||
:rtype: :obj:`bool` | ||
""" | ||
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
4 changes: 4 additions & 0 deletions
4
releasenotes/notes/fix-revert-rust-rate-limiter-e61ca589aa24105b.yaml
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,4 @@ | ||
--- | ||
fixes: | ||
- | | ||
internal: Fix ``Already mutably borrowed`` error by reverting back to pure-python rate limiter. |
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 |
---|---|---|
@@ -1,9 +1,6 @@ | ||
mod rate_limiter; | ||
|
||
use pyo3::prelude::*; | ||
|
||
#[pymodule] | ||
fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> { | ||
m.add_class::<rate_limiter::RateLimiterPy>()?; | ||
fn _core(_: &Bound<'_, PyModule>) -> PyResult<()> { | ||
Ok(()) | ||
} |
Oops, something went wrong.