Allow for bursting up to 1000 API calls with refresh over time

master
j 2024-03-31 01:34:06 -04:00
parent e7b0f350c5
commit 375fb9bd18
1 changed files with 26 additions and 7 deletions

View File

@ -16,7 +16,7 @@ class SessionManager {
// Initialize the Bottleneck limiter // Initialize the Bottleneck limiter
this.limiter = new Bottleneck({ this.limiter = new Bottleneck({
id: "rDramaAPI-limiter", id: "rDramaAPI-Oregon-limiter",
datastore: "ioredis", datastore: "ioredis",
clearDatastore: false, clearDatastore: false,
clientOptions: { clientOptions: {
@ -25,14 +25,21 @@ class SessionManager {
password: process.env.REDIS_PASSWORD || undefined, // Use undefined if no password is set password: process.env.REDIS_PASSWORD || undefined, // Use undefined if no password is set
enableOfflineQueue: true enableOfflineQueue: true
}, },
// Initial reservoir value set to the maximum daily limit
reservoir: 1000,
// Amount to replenish the reservoir every interval
reservoirIncreaseAmount: 41,
// Interval for replenishing the reservoir (1 hour in milliseconds)
reservoirIncreaseInterval: 60 * 60 * 1000, // 3600000 ms
// Maximum value the reservoir can reach through replenishment
reservoirIncreaseMaximum: 1000,
// Safety configurations
maxConcurrent: 1, // Maximum number of concurrent requests maxConcurrent: 1, // Maximum number of concurrent requests
minTime: 1000, // Minimum time between dispatches of requests in milliseconds minTime: 1000, // Minimum time between dispatches of requests in milliseconds
reservoir: 41, // Initial reservoir value for the first hour
reservoirRefreshAmount: 41, // Reservoir value to reset to every hour
reservoirRefreshInterval: 60 * 60 * 1000, // Interval to reset the reservoir (1 hour in milliseconds)
//For now will reply to every comment given enoug time even if severely rate limited
//highWater: 5, // Maximum number of queued jobs.
//strategy: Bottleneck.strategy.OVERFLOW_PRIORITY // Strategy to drop the oldest jobs in the queue when highWater is reached
}); });
this.axiosInstance = axios.create({ this.axiosInstance = axios.create({
@ -52,6 +59,10 @@ class SessionManager {
}); });
} }
/**
* Retrieves the singleton instance of the SessionManager.
* @returns {SessionManager} The singleton instance.
*/
public static getInstance(): SessionManager { public static getInstance(): SessionManager {
if (!SessionManager.instance) { if (!SessionManager.instance) {
SessionManager.instance = new SessionManager(); SessionManager.instance = new SessionManager();
@ -59,6 +70,10 @@ class SessionManager {
return SessionManager.instance; return SessionManager.instance;
} }
/**
* Shuts down the session manager, disconnecting from any external resources.
* @returns {Promise<void>} A promise that resolves when the shutdown process is complete.
*/
public async shutdown(): Promise<void> { public async shutdown(): Promise<void> {
await this.limiter.disconnect(); await this.limiter.disconnect();
} }
@ -104,6 +119,10 @@ class SessionManager {
}; };
} }
/**
* Sets the authorization token for all outgoing requests.
* @param {string} token - The authorization token to be used.
*/
public setAuthorizationToken(token: string): void { public setAuthorizationToken(token: string): void {
this.axiosInstance.defaults.headers.common['Authorization'] = `${token}`; this.axiosInstance.defaults.headers.common['Authorization'] = `${token}`;
} }