Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic handling of RATE_LIMIT_HIT errors #858

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tanmayg06
Copy link

This PR adds automatic handling of RATE_LIMIT_HIT errors in the Zulip API client. When a rate limit error is encountered, the client will now automatically wait for the specified period (from the Retry-After header if available) and retry the request, rather than requiring each application to implement this logic separately.

Fixes: The issue where applications need to implement their own rate limit handling logic when using the Zulip API client.

Changes include:

  1. Enhanced do_api_query method to detect RATE_LIMIT_HIT errors
  2. Added logic to extract and use the Retry-After header value
  3. Implemented automatic retry mechanism with appropriate sleep duration
  4. Added fallback to existing error retry mechanism when no Retry-After header is provided
  5. Added comprehensive tests for the new functionality
  6. Updated README.md with documentation about the automatic rate limit handling

Code example of the implementation:

# Handle rate limiting automatically
if json_result.get("result") == "error" and json_result.get("code") == "RATE_LIMIT_HIT":
    retry_after = None
    # Check for Retry-After header (in seconds)
    if "Retry-After" in res.headers:
        try:
            retry_after = int(res.headers["Retry-After"])
        except (ValueError, TypeError):
            pass
    
    # If we have a valid retry_after value, sleep and retry
    if retry_after and retry_after > 0:
        if self.verbose:
            print(f"Rate limit hit. Retrying after {retry_after} seconds...")
        time.sleep(retry_after)
        continue
    # If no valid retry_after header, use a default backoff
    elif error_retry(" (rate limited)"):
        continue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants