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 retry logic with exponential backoff to API client #30

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

Taombawkry
Copy link

Add retry logic with exponential backoff to API client:

  • Implemented retry interceptor for handling transient errors.
  • Added exponential backoff mechanism with a base delay of 100ms.
  • Updated error handling to include retryable conditions (5xx and 429).

#382

@@ -28,7 +28,8 @@ export class API {
baseURL: hackmdAPIEndpointURL,
headers:{
"Content-Type": "application/json",
}
},
timeout: 30000, // Increased timeout for low bandwidth
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide as an option of APIClient options?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APIClient options type has been expand and methods have been made options of it.

}
this.createRetryInterceptor(this.axios, 3); // Add retry interceptor with maxRetries = 3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The maxRetries should also be an option

return (
!error.response ||
(error.response.status >= 500 && error.response.status < 600) ||
error.response.status === 429
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://hackmd.io/@hackmd-api/developer-portal/https%3A%2F%2Fhackmd.io%2F%40hackmd-api%2Fapi-policy#Rate-Limit-Headers

Could you please also check out the headers? If there are no user remaining API credits, prevent retrying.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a mechanism to check API rate limits (x-ratelimit-userlimit, x-ratelimit-userremaining) and trigger retries or failures based on remaining credits.

Copy link
Author

@Taombawkry Taombawkry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments Addressed

@@ -28,7 +28,8 @@ export class API {
baseURL: hackmdAPIEndpointURL,
headers:{
"Content-Type": "application/json",
}
},
timeout: 30000, // Increased timeout for low bandwidth
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APIClient options type has been expand and methods have been made options of it.

}

export class API {
private axios: AxiosInstance

constructor (readonly accessToken: string, public hackmdAPIEndpointURL: string = "https://api.hackmd.io/v1", public options: APIClientOptions = { wrapResponseErrors: true }) {
constructor (
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constructor is easier to read this way

return (
!error.response ||
(error.response.status >= 500 && error.response.status < 600) ||
error.response.status === 429
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a mechanism to check API rate limits (x-ratelimit-userlimit, x-ratelimit-userremaining) and trigger retries or failures based on remaining credits.

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

Successfully merging this pull request may close these issues.

2 participants