A basic request throttler in TypeScript to schedule API requests to help manage constraints of limited workers and API rate limits.
throttle()
method takes in an activity and converts it into a Job. The Job is assigned to a PlatformManager based on which platform it makes requests to.Jobs
contain a promise which will be resolved once they are carried out. They are managed by PlatformManagers.PlatformManager
hands jobs over to the WorkerManager based on their priority. Each PlatformManager has a RequestTracker which helps to track if a job would go past a rate limit.RequestTrackers
track the available quota for API calls. The quota is updated whenever a request is made, and a timer is set to automatically update when the time window passes.WorkerManager
handles finding the most important job from all PlatformManagers, and will assign these jobs to workers whenever possible.
Edit throttler.ts
with the workers and jobs that you would like to test, then run
npx ts-node throttler.ts