From 375fb9bd18e5e1a28c0fae337d65c3179223205c Mon Sep 17 00:00:00 2001 From: j Date: Sun, 31 Mar 2024 01:34:06 -0400 Subject: [PATCH] Allow for bursting up to 1000 API calls with refresh over time --- src/rdrama/session/SessionManager.ts | 33 ++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/rdrama/session/SessionManager.ts b/src/rdrama/session/SessionManager.ts index 6bce2b8..47d624a 100644 --- a/src/rdrama/session/SessionManager.ts +++ b/src/rdrama/session/SessionManager.ts @@ -16,7 +16,7 @@ class SessionManager { // Initialize the Bottleneck limiter this.limiter = new Bottleneck({ - id: "rDramaAPI-limiter", + id: "rDramaAPI-Oregon-limiter", datastore: "ioredis", clearDatastore: false, clientOptions: { @@ -25,14 +25,21 @@ class SessionManager { password: process.env.REDIS_PASSWORD || undefined, // Use undefined if no password is set 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 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({ @@ -52,6 +59,10 @@ class SessionManager { }); } + /** + * Retrieves the singleton instance of the SessionManager. + * @returns {SessionManager} The singleton instance. + */ public static getInstance(): SessionManager { if (!SessionManager.instance) { SessionManager.instance = new SessionManager(); @@ -59,6 +70,10 @@ class SessionManager { return SessionManager.instance; } + /** + * Shuts down the session manager, disconnecting from any external resources. + * @returns {Promise} A promise that resolves when the shutdown process is complete. + */ public async shutdown(): Promise { 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 { this.axiosInstance.defaults.headers.common['Authorization'] = `${token}`; }