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

Return the Retry-After or similar headers when rate limited #516

Open
EnzoRobaina opened this issue Apr 16, 2024 · 3 comments
Open

Return the Retry-After or similar headers when rate limited #516

EnzoRobaina opened this issue Apr 16, 2024 · 3 comments

Comments

@EnzoRobaina
Copy link

EnzoRobaina commented Apr 16, 2024

async fetchTweets(user: MyUserInterface) {
    const fetchUserTimeline = async () => {
      const timeline = (await this.client.user.timeline(user.id))
        .list as unknown as RettiwtTweet[]

      return await this.transformTweets(timeline)
    }

    return await backOff(fetchUserTimeline, {
      numOfAttempts: this.maxAttempts,
      startingDelay: this.delay,
      delayFirstAttempt: true,
      retry: (error: any, attemptNumber: number) => {
        if (error.status === 429) {
          console.log(
            `Attempt ${attemptNumber}: Retrying after rate limit error for timeline of ${user.username}`
          )
          return true
        }
        return false
      },
      timeMultiple: this.backoffFactor,
    })
  }

Would it be possible to return how much time must be waited before trying again?
This would be very useful when using in conjunction with some backoff/retry implementations

My suggestion for the error object, given that Twitter does provide this info:

{
message: 'TOO_MANY_REQUESTS',
status: 429,
stack: '...',
retryAfter: 2000,
}

@Rishikant181
Copy link
Owner

I think it's possible. Working on it now.

@Rishikant181
Copy link
Owner

Rishikant181 commented Apr 16, 2024

@EnzoRobaina What you can do in the time being is, while creating an instance of Rettiwt, you can pass a custom error handler class which implements the IErrorHandler interface. Then handle method can be implemented such that on Error 429, it returns the retry after value.

The implemented error handler can be passed as follows:

// 'CustomErrorHandler' is the class written by you that implements 'IErrorHandler' interface
const handler = new CustomErrorHandler();

const rettiwt = new Rettiwt({ apiKey: API_KEY, errorHandler: handler });

@EnzoRobaina
Copy link
Author

Thats great! Thanks for the quick reply!

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

No branches or pull requests

2 participants