export declare function sleep(ms: number): Promise; /** * squish (debounce) the async function lambda so that * when a call to lambda is in flight subsequent * calls to lambda will short-circuit to return the * Promise from the already running call. * * @param lambda * @return squished lambda */ export declare function squish(lambda: () => Promise): () => Promise; export interface NumberIterator { next(): { done: boolean; value: number; }; [Symbol.iterator](): NumberIterator; } /** * Return an iterator with the number of ms to * backoff before each retry * * @param maxRetries silently clamped to minimum 1, max 10 * @param backoffMs silently clamped to minimum 100ms, max 10000ms * @return iterable iterator with backoff values starting from zero */ export declare function backoffIterator(maxRetriesIn: any, backoffMsIn: any): NumberIterator; /** * Return a lambda that retries up to * maxRetries times with jitter exponential backoff * of backoffMs, 2*backoffMs, ... 2^n*backoffMs * capped at a 10000 ms backoff with a jitter of backoffMs/10. * * Ex: const retryFetch = backoff(function(url, options) { return fetch(url,options); }); * * @param lambda * @param maxRetries default 3, silently clamped to minimum 1, max 10 * @param backoffMs default 500, silently clamped to minimum 100ms, max 10000ms */ export declare function backoff(lambda: (...args: any[]) => Promise, maxRetries?: number, backoffMs?: number): (...args: any[]) => Promise; /** * Proxy that invokes lambda once, and caches the result * * @param lambdaOnce to invoke just once * @param lambdaTwice to invoke the second+ time called - default * just returns the given cached value */ export declare function once(lambdaOnce: () => T, lambdaTwice?: (T: any) => T): () => T; /** * Barrier that resolves or rejects a promise * when signaled or canceled */ export declare class Barrier { private resolver; private rejecter; private promise; private barrierState; constructor(); /** * Resolve the wait() promise with value * * @param value * @return true if value propagated to waiters, * false if barrier was already signaled * or canceled */ signal(value: T | Promise): boolean; /** * Propagate an error to waiters * * @param err * @return true if value propagated to waiters, * false if barrier was already signaled * or canceled */ cancel(err: any): boolean; /** * @return unresolved, resolved, or rejected */ get state(): string; wait(): Promise; } /** * Helper rate limiting, circuite breaking, and mutual exclusion. * Note that throttle and serialize can be used in conjuction with * backoff and squish to retry and debounce requests. */ export declare class Mutex { get maxQueueLen(): number; get maxConcurrency(): number; private maxConcurrencyVal; private maxReqsPerSec; private rateWindowEnd; private rateWindowCount; private maxQueueLenVal; private numRunning; private waitQueue; /** * @param maxConcurrency max number of concurrently running * requests (subsequent requests are queued) - default is 4 * @param maxReqsPerSec max number of requests per second * before throttling kicks in - must be greater than maxConcurrency * @param maxQueueLen max length of the throttle queue before a * fast fail circuit breaker kicks in */ constructor(maxConcurrency?: number, maxReqsPerSec?: number, maxQueueLen?: number); /** * Aquire the mutex, then invoke lambda, then release the mutex * @param lambda */ enter(lambda: () => Promise): Promise; /** * throttle creates a proxy that limits the number of concurrently running requests * to Mutex.maxConcurrency and Mutex.maxReqsPerSec by * queueing them up, and circuit breaks (fast fails) requests once some * queue length threshold is exceeded, so * an asynchronous client could overload a backend service with requests. * * @param lambda that should be throttled * @return throttled wrapper around lambda */ throttle(lambda: (...args: any[]) => Promise): (...args: any[]) => Promise; private popTheQ; private exit; } /** * Map the given asynchronous function over the given list * synchronously, so that at most batchSize elements of the list * are in process simultaneously * * @param {[I]} list * @param {I => T} lambda * @param {int} batchSize max number of elements to run in parallel - between 1 and 100, default 10 * @param {Promise} result initial list to append to - defaults to [] */ export declare function pmap(list: T[], lambda: (T: any) => Promise, batchSize?: number, result?: R[]): Promise; /** * Make a deep copy of the given object, and * optionally freeze. * * @param thing * @param freeze */ export declare function deepCopy(thing: T, freeze?: boolean): T;