-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from secretkeylabs/vic/rate-limit-fix
Replace the axios rate limiter lib with own
- Loading branch information
Showing
4 changed files
with
117 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* inspired by axios-rate-limit with a few adjustments and bug fixes | ||
* https://github.com/aishek/axios-rate-limit | ||
*/ | ||
|
||
import { AxiosInstance, InternalAxiosRequestConfig } from 'axios'; | ||
|
||
type RateLimitRequestHandler = { | ||
resolve: () => boolean; | ||
}; | ||
|
||
type RateLimitOptions = | ||
| { | ||
maxRPS: number; | ||
} | ||
| { | ||
maxRequests: number; | ||
perMilliseconds: number; | ||
}; | ||
|
||
function throwIfCancellationRequested(config: InternalAxiosRequestConfig<any>) { | ||
if (config.cancelToken) { | ||
config.cancelToken.throwIfRequested(); | ||
} | ||
} | ||
|
||
export class AxiosRateLimit { | ||
private queue: RateLimitRequestHandler[]; | ||
|
||
private timeslotRequests: number; | ||
|
||
private perMilliseconds: number; | ||
|
||
private maxRequests: number; | ||
|
||
constructor(axios: AxiosInstance, options: RateLimitOptions) { | ||
this.queue = []; | ||
this.timeslotRequests = 0; | ||
|
||
if ('maxRPS' in options) { | ||
this.perMilliseconds = 1000; | ||
this.maxRequests = options.maxRPS; | ||
} else { | ||
this.perMilliseconds = options.perMilliseconds; | ||
this.maxRequests = options.maxRequests; | ||
} | ||
|
||
function handleError(error: unknown) { | ||
return Promise.reject(error); | ||
} | ||
|
||
axios.interceptors.request.use(this.handleRequest, handleError); | ||
} | ||
|
||
private handleRequest = (request: InternalAxiosRequestConfig<any>) => { | ||
return new Promise<InternalAxiosRequestConfig<any>>((resolve, reject) => { | ||
this.push({ | ||
resolve: function () { | ||
try { | ||
throwIfCancellationRequested(request); | ||
} catch (error) { | ||
reject(error); | ||
return false; | ||
} | ||
resolve(request); | ||
return true; | ||
}, | ||
}); | ||
}); | ||
}; | ||
|
||
private push = (requestHandler: RateLimitRequestHandler) => { | ||
this.queue.push(requestHandler); | ||
this.shift(); | ||
}; | ||
|
||
private onRequestTimerMet = () => { | ||
this.timeslotRequests--; | ||
this.shift(); | ||
}; | ||
|
||
private shift = () => { | ||
if (this.timeslotRequests >= this.maxRequests) return; | ||
|
||
const queued = this.queue.shift(); | ||
if (!queued) return; | ||
|
||
const resolved = queued.resolve(); | ||
|
||
if (!resolved) { | ||
this.shift(); // rejected request --> shift another request | ||
return; | ||
} | ||
|
||
this.timeslotRequests++; | ||
setTimeout(this.onRequestTimerMet, this.perMilliseconds); | ||
}; | ||
} |