fix(api): add rate limiter to TMDb requests to hopefully deal with 429s (#2881)

fixes #2853
This commit is contained in:
Ryan Cohen
2022-08-02 16:00:34 +09:00
committed by GitHub
parent 575da306b0
commit aed1409f29
4 changed files with 23 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
import rateLimit from 'axios-rate-limit';
import NodeCache from 'node-cache';
// 5 minute default TTL (in seconds)
@@ -10,6 +11,10 @@ const DEFAULT_ROLLING_BUFFER = 10000;
interface ExternalAPIOptions {
nodeCache?: NodeCache;
headers?: Record<string, unknown>;
rateLimit?: {
maxRPS: number;
maxRequests: number;
};
}
class ExternalAPI {
@@ -31,6 +36,14 @@ class ExternalAPI {
...options.headers,
},
});
if (options.rateLimit) {
this.axios = rateLimit(this.axios, {
maxRequests: options.rateLimit.maxRequests,
maxRPS: options.rateLimit.maxRPS,
});
}
this.baseUrl = baseUrl;
this.cache = options.nodeCache;
}

View File

@@ -92,6 +92,10 @@ class TheMovieDb extends ExternalAPI {
},
{
nodeCache: cacheManager.getCache('tmdb').data,
rateLimit: {
maxRequests: 20,
maxRPS: 1,
},
}
);
this.region = region;