feat: Implement thread-safe rate limiting for network session classes#306
Open
itsmeknt wants to merge 13 commits intospeedyapply:mainfrom
Open
feat: Implement thread-safe rate limiting for network session classes#306itsmeknt wants to merge 13 commits intospeedyapply:mainfrom
itsmeknt wants to merge 13 commits intospeedyapply:mainfrom
Conversation
Remove unused import statement for 'request'.
|
Bump @cullenwatson |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This pull request introduces a new, thread-safe rate-limiting capability to the RequestsRotating and TLSRotating session classes. Users can now specify a minimum and maximum delay between requests to avoid overwhelming servers or triggering anti-bot mechanisms. A delay interval, randomly chosen between the minimum and maximum, will then be chosen.
Motivation
When performing automated requests at a high frequency, it's common to encounter rate limits imposed by the target server (e.g., HTTP 429 "Too Many Requests"). This can result in temporary or permanent IP blocks.
Implementing a client-side rate limiter provides a robust mechanism to control request frequency, increasing the reliability and success rate of long-running tasks and promoting more responsible scraping/automation practices.
Implementation Details
New RateLimiter Class:
A dedicated, reusable RateLimiter class has been created to encapsulate all rate-limiting logic.
It uses a threading.Lock to ensure atomicity, making it safe for use across multiple threads sharing the same session object.
It utilizes time.monotonic() for accurate time interval measurement, which is not affected by system time changes.
It supports both fixed delays (by providing rate_delay_min only) and randomized delays within a range (by providing both rate_delay_min and rate_delay_max).
Integration with Session Classes:
The init methods of RequestsRotating and TLSRotating have been updated to accept rate_delay_min and rate_delay_max arguments.
An instance of RateLimiter is created within each session.
A call to self.rate_limiter.enforce_delay() has been added at the very beginning of the request() and execute_request() methods to ensure the delay is enforced before any network activity occurs.
How to Use
Users can now instantiate the session classes with the new rate-limiting parameters:
code