Compare commits

...

2 Commits

Author SHA1 Message Date
j d672797bbd AxiosThrottle Removed, Bottleneck Configured 2024-03-17 14:38:30 -04:00
j 53930d5bfb Bottleneck Install 2024-03-17 14:37:52 -04:00
3 changed files with 57 additions and 11 deletions

23
package-lock.json generated
View File

@ -8,6 +8,7 @@
"axios": "^1.6.7",
"axios-request-throttle": "^1.0.0",
"axios-retry": "^4.0.0",
"bottleneck": "^2.19.5",
"dotenv": "^16.4.5",
"form-data": "^4.0.0",
"fs": "^0.0.1-security",
@ -246,6 +247,11 @@
"readable-stream": "^3.4.0"
}
},
"node_modules/bottleneck": {
"version": "2.19.5",
"resolved": "https://registry.npmjs.org/bottleneck/-/bottleneck-2.19.5.tgz",
"integrity": "sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw=="
},
"node_modules/brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
@ -597,9 +603,9 @@
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw=="
},
"node_modules/follow-redirects": {
"version": "1.15.5",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz",
"integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==",
"version": "1.15.6",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
"integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==",
"funding": [
{
"type": "individual",
@ -2190,6 +2196,11 @@
"readable-stream": "^3.4.0"
}
},
"bottleneck": {
"version": "2.19.5",
"resolved": "https://registry.npmjs.org/bottleneck/-/bottleneck-2.19.5.tgz",
"integrity": "sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw=="
},
"brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
@ -2449,9 +2460,9 @@
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw=="
},
"follow-redirects": {
"version": "1.15.5",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz",
"integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw=="
"version": "1.15.6",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
"integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA=="
},
"form-data": {
"version": "4.0.0",

View File

@ -3,6 +3,7 @@
"axios": "^1.6.7",
"axios-request-throttle": "^1.0.0",
"axios-retry": "^4.0.0",
"bottleneck": "^2.19.5",
"dotenv": "^16.4.5",
"form-data": "^4.0.0",
"fs": "^0.0.1-security",
@ -21,4 +22,4 @@
"start": "node dist/index.js",
"test": "tsc -p tsconfig.tests.json"
}
}
}

View File

@ -1,13 +1,18 @@
import axios, {AxiosInstance, AxiosError} from 'axios';
import axios, { AxiosInstance, AxiosError, AxiosResponse, AxiosRequestConfig } from 'axios';
import axiosRetry from 'axios-retry';
import axiosThrottle from 'axios-request-throttle';
import Bottleneck from 'bottleneck';
class SessionManager {
private static instance: SessionManager;
public readonly axiosInstance: AxiosInstance;
private limiter: Bottleneck;
private constructor() {
axiosThrottle.use(axios, { requestsPerSecond: 1 }); // Throttle setup
// Initialize the Bottleneck limiter
this.limiter = new Bottleneck({
maxConcurrent: 1, // Maximum number of concurrent requests
minTime: 1000 // Minimum time between dispatches of requests in milliseconds
});
this.axiosInstance = axios.create({
baseURL: 'https://rdrama.net/',
@ -16,6 +21,9 @@ class SessionManager {
},
});
// Wrap axios requests with the limiter
this.wrapAxiosInstance(this.axiosInstance);
axiosRetry(this.axiosInstance, {
retries: 3,
retryDelay: this.retryDelayStrategy,
@ -41,7 +49,33 @@ class SessionManager {
private retryCondition(error: AxiosError): boolean {
const status = error.response?.status ?? 0;
return status === 429 || status >= 400;
return status === 429 || status >= 500;
}
private wrapAxiosInstance(instance: AxiosInstance): void {
// Wrap the get method
const originalGet = instance.get;
instance.get = <T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R> => {
return this.limiter.schedule(() => originalGet.apply(instance, [url, config])) as Promise<R>;
};
// Wrap the post method
const originalPost = instance.post;
instance.post = <T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R> => {
return this.limiter.schedule(() => originalPost.apply(instance, [url, data, config])) as Promise<R>;
};
// Wrap the put method
const originalPut = instance.put;
instance.put = <T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R> => {
return this.limiter.schedule(() => originalPut.apply(instance, [url, data, config])) as Promise<R>;
};
// Wrap the delete method
const originalDelete = instance.delete;
instance.delete = <T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R> => {
return this.limiter.schedule(() => originalDelete.apply(instance, [url, config])) as Promise<R>;
};
}
public setAuthorizationToken(token: string): void {