Skip to content

Add retry logic with exponential backoff to API client #30

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

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 60 additions & 5 deletions nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,29 @@ const defaultOption: RequestOptions = {
type OptionReturnType<Opt, T> = Opt extends { unwrapData: false } ? AxiosResponse<T> : Opt extends { unwrapData: true } ? T : T

export type APIClientOptions = {
wrapResponseErrors: boolean
wrapResponseErrors: boolean;
timeout?: number;
retryConfig?: {
maxRetries: number;
baseDelay: number;
};
}

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
Contributor 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

readonly accessToken: string,
public hackmdAPIEndpointURL: string = "https://api.hackmd.io/v1",
public options: APIClientOptions = {
wrapResponseErrors: true,
timeout: 30000,
retryConfig: {
maxRetries: 3,
baseDelay: 100,
},
}
) {
if (!accessToken) {
throw new HackMDErrors.MissingRequiredArgument('Missing access token when creating HackMD client')
}
Expand All @@ -28,7 +44,8 @@ export class API {
baseURL: hackmdAPIEndpointURL,
headers:{
"Content-Type": "application/json",
}
},
timeout: options.timeout
})

this.axios.interceptors.request.use(
Expand Down Expand Up @@ -71,13 +88,51 @@ export class API {
`Received an error response (${err.response.status} ${err.response.statusText}) from HackMD`,
err.response.status,
err.response.statusText,
)
);
}
}
)
);
}
if (options.retryConfig) {
this.createRetryInterceptor(this.axios, options.retryConfig.maxRetries, options.retryConfig.baseDelay);
}
}

private exponentialBackoff(retries: number, baseDelay: number): number {
return Math.pow(2, retries) * baseDelay;
}

private isRetryableError(error: AxiosError): boolean {
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
Contributor 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.

);
}

private createRetryInterceptor(axiosInstance: AxiosInstance, maxRetries: number, baseDelay: number): void {
let retryCount = 0;

axiosInstance.interceptors.response.use(
response => response,
async error => {
if (retryCount < maxRetries && this.isRetryableError(error)) {
const remainingCredits = parseInt(error.response?.headers['x-ratelimit-userremaining'], 10);

if (isNaN(remainingCredits) || remainingCredits > 0) {
retryCount++;
const delay = this.exponentialBackoff(retryCount, baseDelay);
console.warn(`Retrying request... attempt #${retryCount} after delay of ${delay}ms`);
await new Promise(resolve => setTimeout(resolve, delay));
return axiosInstance(error.config);
}
}

retryCount = 0; // Reset retry count after a successful request or when not retrying
return Promise.reject(error);
}
);
}
async getMe<Opt extends RequestOptions> (options = defaultOption as Opt): Promise<OptionReturnType<Opt, GetMe>> {
return this.unwrapData(this.axios.get<GetMe>("me"), options.unwrapData) as unknown as OptionReturnType<Opt, GetMe>
}
Expand Down
2 changes: 1 addition & 1 deletion nodejs/tests/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test('should throw axios error object if set wrapResponseErrors to false', async
})

server.use(
rest.get('https://api.hackmd.io/v1/me', (req, res, ctx) => {
rest.get('https://api.hackmd.io/v1/me', (req: any, res: (arg0: any) => any, ctx: { status: (arg0: number) => any }) => {
return res(ctx.status(429))
}),
)
Expand Down