Compare commits
2 Commits
bf7a7a9680
...
d672797bbd
Author | SHA1 | Date |
---|---|---|
j | d672797bbd | |
j | 53930d5bfb |
|
@ -8,6 +8,7 @@
|
||||||
"axios": "^1.6.7",
|
"axios": "^1.6.7",
|
||||||
"axios-request-throttle": "^1.0.0",
|
"axios-request-throttle": "^1.0.0",
|
||||||
"axios-retry": "^4.0.0",
|
"axios-retry": "^4.0.0",
|
||||||
|
"bottleneck": "^2.19.5",
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
"form-data": "^4.0.0",
|
"form-data": "^4.0.0",
|
||||||
"fs": "^0.0.1-security",
|
"fs": "^0.0.1-security",
|
||||||
|
@ -246,6 +247,11 @@
|
||||||
"readable-stream": "^3.4.0"
|
"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": {
|
"node_modules/brace-expansion": {
|
||||||
"version": "1.1.11",
|
"version": "1.1.11",
|
||||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
"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=="
|
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw=="
|
||||||
},
|
},
|
||||||
"node_modules/follow-redirects": {
|
"node_modules/follow-redirects": {
|
||||||
"version": "1.15.5",
|
"version": "1.15.6",
|
||||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz",
|
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
|
||||||
"integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==",
|
"integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==",
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
"type": "individual",
|
"type": "individual",
|
||||||
|
@ -2190,6 +2196,11 @@
|
||||||
"readable-stream": "^3.4.0"
|
"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": {
|
"brace-expansion": {
|
||||||
"version": "1.1.11",
|
"version": "1.1.11",
|
||||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
"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=="
|
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw=="
|
||||||
},
|
},
|
||||||
"follow-redirects": {
|
"follow-redirects": {
|
||||||
"version": "1.15.5",
|
"version": "1.15.6",
|
||||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz",
|
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
|
||||||
"integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw=="
|
"integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA=="
|
||||||
},
|
},
|
||||||
"form-data": {
|
"form-data": {
|
||||||
"version": "4.0.0",
|
"version": "4.0.0",
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
"axios": "^1.6.7",
|
"axios": "^1.6.7",
|
||||||
"axios-request-throttle": "^1.0.0",
|
"axios-request-throttle": "^1.0.0",
|
||||||
"axios-retry": "^4.0.0",
|
"axios-retry": "^4.0.0",
|
||||||
|
"bottleneck": "^2.19.5",
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
"form-data": "^4.0.0",
|
"form-data": "^4.0.0",
|
||||||
"fs": "^0.0.1-security",
|
"fs": "^0.0.1-security",
|
||||||
|
|
|
@ -1,13 +1,18 @@
|
||||||
import axios, {AxiosInstance, AxiosError} from 'axios';
|
import axios, { AxiosInstance, AxiosError, AxiosResponse, AxiosRequestConfig } from 'axios';
|
||||||
import axiosRetry from 'axios-retry';
|
import axiosRetry from 'axios-retry';
|
||||||
import axiosThrottle from 'axios-request-throttle';
|
import Bottleneck from 'bottleneck';
|
||||||
|
|
||||||
class SessionManager {
|
class SessionManager {
|
||||||
private static instance: SessionManager;
|
private static instance: SessionManager;
|
||||||
public readonly axiosInstance: AxiosInstance;
|
public readonly axiosInstance: AxiosInstance;
|
||||||
|
private limiter: Bottleneck;
|
||||||
|
|
||||||
private constructor() {
|
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({
|
this.axiosInstance = axios.create({
|
||||||
baseURL: 'https://rdrama.net/',
|
baseURL: 'https://rdrama.net/',
|
||||||
|
@ -16,6 +21,9 @@ class SessionManager {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Wrap axios requests with the limiter
|
||||||
|
this.wrapAxiosInstance(this.axiosInstance);
|
||||||
|
|
||||||
axiosRetry(this.axiosInstance, {
|
axiosRetry(this.axiosInstance, {
|
||||||
retries: 3,
|
retries: 3,
|
||||||
retryDelay: this.retryDelayStrategy,
|
retryDelay: this.retryDelayStrategy,
|
||||||
|
@ -41,7 +49,33 @@ class SessionManager {
|
||||||
|
|
||||||
private retryCondition(error: AxiosError): boolean {
|
private retryCondition(error: AxiosError): boolean {
|
||||||
const status = error.response?.status ?? 0;
|
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 {
|
public setAuthorizationToken(token: string): void {
|
||||||
|
|
Loading…
Reference in New Issue