export default class LeakyBucket { private queue; private totalCost; private currentCapacity; private lastRefill; private refillRate; private timer; private emptyPromiseResolver; private emptyPromise; private capacity; private maxCapacity; private timeout; private interval; /** * Sets up the leaky bucket. The bucket is designed so that it can * burst by the capacity it is given. after that items can be queued * until a timeout of n seonds is reached. * * example: throttle 10 actions per minute that have each a cost of 1, reject * everything theat is overflowing. there will no more than 10 items queued * at any time * capacity: 10 * interval: 60 * timeout: 60 * * example: throttle 100 actions per minute that have a cost of 1, reject * items that have to wait more thatn 2 minutes. there will be no more than * 200 items queued at any time. of those 200 items 100 will be bursted within * a minute, the rest will be executed evenly spread over a mintue. * capacity: 100 * interval: 60 * timeout: 120 * * @param {number} capacity the capacity the bucket has per interval * @param {number} timeout the total time items are allowed to wait for execution * @param {number} interval the interval for the capacity in seconds */ constructor({ capacity, timeout, interval, }?: { capacity?: number; timeout?: number; interval?: number; }); get length(): number; /** * the throttle method is used to throttle things. it is async and will resolve either * immediatelly, if there is space in the bucket, than can be bursted, or it will wait * until there is enough capacity left to execute the item with the given cost. if the * bucket is overflowing, and the item cannot be executed within the timeout of the bucket, * the call will be rejected with an error. * * @param {number} cost=1 the cost of the item to be throttled. is the cost is unknown, * the cost can be payed after execution using the pay method. * defaults to 1. * @param {boolean} append = true set to false if the item needs ot be added to the * beginning of the queue * @param {boolean} isPause = false defines if the element is a pause elemtn, if yes, it * will not be cleaned off of the queue when checking * for overflowing elements * @returns {promise} resolves when the item can be executed, rejects if the item cannot * be executed in time */ throttle(cost?: number, append?: boolean, isPause?: boolean): Promise; /** * either executes directly when enough capacity is present or delays the * execution until enough capacity is available. * * @private */ startTimer(): void; /** * removes the first item in the queue, resolves the promise that indicated * that the bucket is empty and no more items are waiting * * @private */ shiftQueue(): void; /** * is resolved as soon as the bucket is empty. is basically an event * that is emitted */ isEmpty(): Promise; /** * ends the bucket. The bucket may be recycled after this call */ end(): void; /** * removes all items from the queue, does not stop the timer though * * @privae */ clear(): void; /** * can be used to pay costs for items where the cost is clear after exection * this will devcrease the current capacity availabe on the bucket. * * @param {number} cost the ost to pay */ pay(cost: any): void; /** * stops the running times * * @private */ stopTimer(): void; /** * refills the bucket with capacity which has become available since the * last refill. starts to refill after a call has started using capacity * * @private */ refill(): void; /** * gets the currenlty avilable max capacity, respecintg * the capacity that is already used in the moment * * @private */ getCurrentMaxCapacity(): number; /** * removes all items that cannot be executed in time due to items * that were added in front of them in the queue (mostly pause items) * * @private */ cleanQueue(): void; /** * returns the first item from the queue * * @private */ getFirstItem(): any; /** * pasue the bucket for the given cost. means that an item is added in the * front of the queue with the cost passed to this method * * @param {number} cost the cost to pasue by */ pauseByCost(cost: any): void; /** * pause the bucket for n seconds. means that an item with the cost for one * second is added at the beginning of the queue * * @param {number} seconds the number of seconds to pause the bucket by */ pause(seconds?: number): void; /** * drains the bucket, so that nothing can be exuted at the moment * * @private */ drain(): void; /** * set the timeout value for the bucket. this is the amount of time no item * may longer wait for. * * @param {number} timeout in seonds */ setTimeout(timeout: any): this; /** * set the interval within whch the capacity can be used * * @param {number} interval in seonds */ setInterval(interval: any): this; /** * set the capacity of the bucket. this si the capacity that can be used per interval * * @param {number} capacity */ setCapacity(capacity: any): this; /** * claculates the values of some frequently used variables on the bucket * * @private */ updateVariables(): void; }