Skip to content

feat: retry logic to check for rate limit remaining header #104

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 1 commit into from
Jun 6, 2025
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
## Change log
### Version: 1.2.2
#### Date: Jun-09-2025
- Enhancement: Retry logic to check for rate limit remaining header

### Version: 1.2.1
#### Date: Apr-29-2025
- Fix: Updated Regex for resolve the path traversal issue

### Version: 1.2.0
#### Date: Jan-24-2025
- Fix: URL change for Live Preview
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/core",
"version": "1.2.1",
"version": "1.2.2",
"type": "commonjs",
"main": "./dist/cjs/src/index.js",
"types": "./dist/cjs/src/index.d.ts",
Expand Down
25 changes: 16 additions & 9 deletions src/lib/retryPolicy/delivery-sdk-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,26 @@ export const retryResponseErrorHandler = (error: any, config: any, axiosInstance
} else {
throw error;
}
} else if (response.status == 429 || response.status == 401) {
retryCount++;
} else {
const rateLimitRemaining = response.headers['x-ratelimit-remaining'];
if (rateLimitRemaining !== undefined && parseInt(rateLimitRemaining) <= 0) {
return Promise.reject(error.response.data);
}

if (response.status == 429 || response.status == 401) {
retryCount++;

if (retryCount >= config.retryLimit) {
if (error.response && error.response.data) {
return Promise.reject(error.response.data);
if (retryCount >= config.retryLimit) {
if (error.response && error.response.data) {
return Promise.reject(error.response.data);
}

return Promise.reject(error);
}
error.config.retryCount = retryCount;

return Promise.reject(error);
return axiosInstance(error.config);
}
error.config.retryCount = retryCount;

return axiosInstance(error.config);
}

if (config.retryCondition && config.retryCondition(error)) {
Expand Down
216 changes: 155 additions & 61 deletions test/retryPolicy/delivery-sdk-handlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ describe('retryResponseErrorHandler', () => {
await retryResponseErrorHandler(error, config, client);
fail('Expected retryResponseErrorHandler to throw an error');
} catch (err) {
expect(err).toEqual(expect.objectContaining({
code: 'ECONNABORTED',
config: expect.objectContaining({ retryOnError: false }),
}));
expect(err).toEqual(
expect.objectContaining({
code: 'ECONNABORTED',
config: expect.objectContaining({ retryOnError: false }),
})
);
}
});
it('should reject the promise if retryOnError is true', async () => {
const error = { config: { retryOnError: true } };
const config = { retryLimit: 5 };
const client = axios.create();

try {
await retryResponseErrorHandler(error, config, client);
fail('Expected retryResponseErrorHandler to throw an error');
Expand All @@ -73,121 +75,212 @@ describe('retryResponseErrorHandler', () => {
await retryResponseErrorHandler(error, config, client);
fail('Expected retryResponseErrorHandler to throw an error');
} catch (err) {
expect(err).toEqual(expect.objectContaining({
error_code: 408,
error_message: `Timeout of ${config.timeout}ms exceeded`,
errors: null
}));
}
expect(err).toEqual(
expect.objectContaining({
error_code: 408,
error_message: `Timeout of ${config.timeout}ms exceeded`,
errors: null,
})
);
}
});
it('should reject the promise if response status is 429 and retryCount exceeds retryLimit', async () => {
const error = {
config: { retryOnError: true, retryCount: 5 },
response: { status: 429, statusText: 'timeout of 1000ms exceeded' },
response: {
status: 429,
statusText: 'timeout of 1000ms exceeded',
headers: {},
data: {
error_message: 'Rate limit exceeded',
error_code: 429,
errors: null,
},
},
};
const config = { retryLimit: 5, timeout: 1000 };
const client = axios.create();

await expect(retryResponseErrorHandler(error, config, client)).rejects.toBe(error);
await expect(retryResponseErrorHandler(error, config, client)).rejects.toEqual(error.response.data);
});
it('should reject the promise if response status is 401 and retryCount exceeds retryLimit', async () => {
const error = {
config: { retryOnError: true, retryCount: 5 },
response: { status: 401, statusText: 'timeout of 1000ms exceeded' },
response: {
status: 401,
statusText: 'timeout of 1000ms exceeded',
headers: {},
data: {
error_message: 'Unauthorized',
error_code: 401,
errors: null,
},
},
};
const config = { retryLimit: 5, timeout: 1000 };
const client = axios.create();

await expect(retryResponseErrorHandler(error, config, client)).rejects.toBe(error);
await expect(retryResponseErrorHandler(error, config, client)).rejects.toEqual(error.response.data);
});
it('should reject the promise if response status is 429 or 401 and retryCount is within limit', async () => {
const error = {
config: { retryOnError: true, retryCount: 4 },
response: { status: 429, statusText: 'timeout of 1000ms exceeded' },
response: {
status: 429,
statusText: 'timeout of 1000ms exceeded',
headers: {},
data: {
error_message: 'Rate limit exceeded',
error_code: 429,
errors: null,
},
},
request: {
method: 'post',
url: '/retryURL',
data: { key: 'value' },
headers: { 'Content-Type': 'application/json' },
},
};
const config = { retryLimit: 5, timeout: 1000 };
const config = { retryLimit: 4, timeout: 1000 };
const client = axios.create();

const finalResponseObj = {
config: { retryOnError: true, retryCount: 4 },
response: { status: 429, statusText: 'timeout of 1000ms exceeded' },
};

mock.onPost('/retryURL').reply(200, finalResponseObj);

try {
await retryResponseErrorHandler(error, config, client);
throw new Error('Expected retryResponseErrorHandler to throw an error');
} catch (err: any) {
expect(err.response.status).toBe(429);
expect(err.response.statusText).toBe(error.response.statusText);
expect(err.config.retryCount).toBe(error.config.retryCount);
}

await expect(retryResponseErrorHandler(error, config, client)).rejects.toEqual(error.response.data);
});
it('should call the retry function if retryCondition is passed', async () => {
const error = {
config: { retryOnError: true, retryCount: 4 },
response: { status: 200, statusText: 'Success Response but retry needed' },
response: {
status: 200,
statusText: 'Success Response but retry needed',
headers: {},
data: {
error_message: 'Retry needed',
error_code: 200,
errors: null,
},
},
request: {
method: 'post',
url: '/retryURL',
data: { key: 'value' },
headers: { 'Content-Type': 'application/json' },
},
};
// eslint-disable-next-line @typescript-eslint/no-shadow
const retryCondition = (error: any) => true;
const config = { retryLimit: 5, timeout: 1000, retryCondition: retryCondition };
const retryCondition = () => true;
const config = { retryLimit: 5, timeout: 1000, retryCondition };
const client = axios.create();

const finalResponseObj = {
config: { retryOnError: true, retryCount: 5 },
response: { status: 429, statusText: 'timeout of 1000ms exceeded' },
};

mock.onPost('/retryURL').reply(200, finalResponseObj);
mock.onPost('/retryURL').reply(200, { success: true });

const finalResponse: any = await retryResponseErrorHandler(error, config, client);

expect(finalResponse.data).toEqual(finalResponseObj);
const response = (await retryResponseErrorHandler(error, config, client)) as AxiosResponse;
expect(response.status).toBe(200);
});
it('should reject to error when retryCondition is passed but retryLimit is exceeded', async () => {
const error = {
config: { retryOnError: true, retryCount: 5 },
response: { status: 200, statusText: 'Success Response but retry needed' },
response: {
status: 200,
statusText: 'Success Response but retry needed',
headers: {},
data: {
error_message: 'Retry needed',
error_code: 200,
errors: null,
},
},
request: {
method: 'post',
url: '/retryURL',
data: { key: 'value' },
headers: { 'Content-Type': 'application/json' },
},
};
// eslint-disable-next-line @typescript-eslint/no-shadow
const retryCondition = (error: any) => true;
const config = { retryLimit: 5, timeout: 1000, retryCondition: retryCondition };
const config = { retryLimit: 5, timeout: 1000, retryCondition };
const client = axios.create();

const finalResponseObj = {
config: { retryOnError: true, retryCount: 5 },
response: { status: 429, statusText: 'timeout of 1000ms exceeded' },
await expect(retryResponseErrorHandler(error, config, client)).rejects.toEqual(error);
});

it('should retry when response status is 429 and retryCount is less than retryLimit', async () => {
const error = {
config: { retryOnError: true, retryCount: 1 },
response: {
status: 429,
statusText: 'Rate limit exceeded',
headers: {},
data: {
error_message: 'Rate limit exceeded',
error_code: 429,
errors: null,
},
},
};
const config = { retryLimit: 3 };
const client = axios.create();

mock.onAny().reply(200, { success: true });

const response = (await retryResponseErrorHandler(error, config, client)) as AxiosResponse;
expect(response.status).toBe(200);
});

it('should retry when retryCondition is true', async () => {
const error = {
config: { retryOnError: true, retryCount: 1 },
response: {
status: 500,
statusText: 'Internal Server Error',
headers: {},
data: {
error_message: 'Internal Server Error',
error_code: 500,
errors: null,
},
},
};
const retryCondition = jest.fn().mockReturnValue(true);
const config = { retryLimit: 3, retryCondition, retryDelay: 100 };
const client = axios.create();

mock.onPost('/retryURL').reply(200, finalResponseObj);
mock.onAny().reply(200, { success: true });

await expect(retryResponseErrorHandler(error, config, client)).rejects.toBe(error);
const response = (await retryResponseErrorHandler(error, config, client)) as AxiosResponse;
expect(response.status).toBe(200);
expect(retryCondition).toHaveBeenCalledWith(error);
});

it('should retry when response status is 429 and retryCount is less than retryLimit', async () => {
it('should reject with rate limit error when x-ratelimit-remaining is 0', async () => {
const error = {
config: { retryOnError: true, retryCount: 1 },
response: { status: 429, statusText: 'Rate limit exceeded' },
response: {
status: 429,
headers: {
'x-ratelimit-remaining': '0',
},
data: {
error_message: 'Rate limit exceeded',
error_code: 429,
errors: null,
},
},
};
const config = { retryLimit: 3 };
const client = axios.create();

await expect(retryResponseErrorHandler(error, config, client)).rejects.toEqual(error.response.data);
});

it('should retry when x-ratelimit-remaining is greater than 0', async () => {
const error = {
config: { retryOnError: true, retryCount: 1 },
response: {
status: 429,
headers: {
'x-ratelimit-remaining': '5',
},
},
};
const config = { retryLimit: 3 };
const client = axios.create();
Expand All @@ -198,19 +291,20 @@ describe('retryResponseErrorHandler', () => {
expect(response.status).toBe(200);
});

it('should retry when retryCondition is true', async () => {
it('should retry when x-ratelimit-remaining header is not present', async () => {
const error = {
config: { retryOnError: true, retryCount: 1 },
response: { status: 500, statusText: 'Internal Server Error' },
response: {
status: 429,
headers: {},
},
};
const retryCondition = jest.fn().mockReturnValue(true);
const config = { retryLimit: 3, retryCondition, retryDelay: 100 };
const config = { retryLimit: 3 };
const client = axios.create();

mock.onAny().reply(200);

const response: any = await retryResponseErrorHandler(error, config, client);
expect(response.status).toBe(200);
expect(retryCondition).toHaveBeenCalledWith(error);
});
});
Loading