1 | import { PersistentCache } from "./cache";
|
2 | export declare class Deferred<T = void> {
|
3 | promise: Promise<T>;
|
4 | resolve: (arg: T | PromiseLike<T>) => void;
|
5 | reject: (err?: any) => void;
|
6 | constructor();
|
7 | }
|
8 | export declare class DeferredWorker<T = void> extends Deferred<T> {
|
9 | private worker;
|
10 | private cancel?;
|
11 | constructor(worker: () => Promise<T>, cancel?: (() => string | undefined) | undefined);
|
12 | execute(): Promise<void>;
|
13 | }
|
14 | export declare type RetryType = number | ((err: any, retries: number) => boolean);
|
15 | export declare function retryOp<T>(retryN: RetryType, fn: (retries: number) => Promise<T>): Promise<T>;
|
16 | export declare class Funnel<T = void> {
|
17 | concurrency: number;
|
18 | protected shouldRetry?: RetryType | undefined;
|
19 | protected pendingQueue: Set<DeferredWorker<T>>;
|
20 | protected executingQueue: Set<DeferredWorker<T>>;
|
21 | processed: number;
|
22 | errors: number;
|
23 | constructor(concurrency?: number, shouldRetry?: RetryType | undefined);
|
24 | push(worker: () => Promise<T>, shouldRetry?: RetryType, cancel?: () => string | undefined): Promise<T>;
|
25 | clear(): void;
|
26 | promises(): Promise<T>[];
|
27 | all(): Promise<(void | Awaited<T>)[]>;
|
28 | size(): number;
|
29 | setMaxConcurrency(maxConcurrency: number): void;
|
30 | getConcurrency(): number;
|
31 | protected doWork(): void;
|
32 | }
|
33 | /**
|
34 | * @internal
|
35 | */
|
36 | export interface PumpOptions {
|
37 | concurrency: number;
|
38 | verbose?: boolean;
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 | export declare class Pump<T = void> extends Funnel<T | void> {
|
44 | protected options: PumpOptions;
|
45 | protected worker: () => Promise<T>;
|
46 | stopped: boolean;
|
47 | constructor(options: PumpOptions, worker: () => Promise<T>);
|
48 | start(): void;
|
49 | stop(): void;
|
50 | drain(): Promise<(void | Awaited<T>)[]>;
|
51 | setMaxConcurrency(concurrency: number): void;
|
52 | }
|
53 | export declare class RateLimiter<T = void> {
|
54 | protected targetRequestsPerSecond: number;
|
55 | protected burst: number;
|
56 | protected lastTick: number;
|
57 | protected bucket: number;
|
58 | protected queue: Set<DeferredWorker<T>>;
|
59 | constructor(targetRequestsPerSecond: number, burst?: number);
|
60 | push(worker: () => Promise<T>, cancel?: () => string | undefined): Promise<T>;
|
61 | protected updateBucket(): void;
|
62 | protected drainQueue(): Promise<void>;
|
63 | clear(): void;
|
64 | }
|
65 | /**
|
66 | * Specify {@link throttle} limits. These limits shape the way throttle invokes
|
67 | * the underlying function.
|
68 | * @public
|
69 | */
|
70 | export interface Limits {
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 | concurrency: number;
|
77 | |
78 |
|
79 |
|
80 |
|
81 | rate?: number;
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | burst?: number;
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | retry?: number | ((err: any, retries: number) => boolean);
|
103 | /**
|
104 | * If `memoize` is `true`, then every call to the throttled function will be
|
105 | * saved as an entry in a map from arguments to return value. If same
|
106 | * arguments are seen again in a future call, the return value is retrieved
|
107 | * from the Map rather than calling the function again. This can be useful
|
108 | * for avoiding redundant calls that are expected to return the same results
|
109 | * given the same arguments.
|
110 | *
|
111 | * The arguments will be captured with `JSON.stringify`, therefore types
|
112 | * that do not stringify uniquely won't be distinguished from each other.
|
113 | * Care must be taken when specifying `memoize` to ensure avoid incorrect
|
114 | * results.
|
115 | */
|
116 | memoize?: boolean;
|
117 | /**
|
118 | * Similar to `memoize` except the map from function arguments to results is
|
119 | * stored in a persistent cache on disk. This is useful to prevent redundant
|
120 | * calls to APIs which are expected to return the same results for the same
|
121 | * arguments, and which are likely to be called across many faast.js module
|
122 | * instantiations. This is used internally by faast.js for caching cloud
|
123 | * prices for AWS and Google, and for saving the last garbage collection
|
124 | * date for AWS. Persistent cache entries expire after a period of time. See
|
125 | * {@link PersistentCache}.
|
126 | */
|
127 | cache?: PersistentCache;
|
128 | /**
|
129 | * A promise that, if resolved, causes cancellation of pending throttled
|
130 | * invocations. This is typically created using `Deferred`. The idea is to
|
131 | * use the resolving of the promise as an asynchronous signal that any
|
132 | * pending invocations in this throttled function should be cleared.
|
133 | * @internal
|
134 | */
|
135 | cancel?: Promise<void>;
|
136 | }
|
137 | export declare function memoizeFn<A extends any[], R>(fn: (...args: A) => R, cache?: Map<string, R>): (...args: A) => R;
|
138 | export declare function cacheFn<A extends any[], R>(cache: PersistentCache, fn: (...args: A) => Promise<R>): (...args: A) => Promise<any>;
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | export declare function throttle<A extends any[], R>(limits: Limits, fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>;
|
210 | export declare class AsyncQueue<T> {
|
211 | protected deferred: Array<Deferred<T>>;
|
212 | protected enqueued: Promise<T>[];
|
213 | enqueue(value: T | Promise<T>): void;
|
214 | next(): Promise<T>;
|
215 | clear(): void;
|
216 | }
|
217 | export declare class AsyncIterableQueue<T> extends AsyncQueue<IteratorResult<T>> {
|
218 | push(value: T | Promise<T>): void;
|
219 | done(): void;
|
220 | [Symbol.asyncIterator](): this;
|
221 | }
|
222 | export declare class AsyncOrderedQueue<T> {
|
223 | protected queue: AsyncQueue<T>;
|
224 | protected arrived: Map<number, Promise<T>>;
|
225 | protected current: number;
|
226 | push(value: T | Promise<T>, sequence: number): void;
|
227 | pushImmediate(value: T | Promise<T>): void;
|
228 | enqueue(value: Promise<T>, sequence: number): void;
|
229 | next(): Promise<T>;
|
230 | clear(): void;
|
231 | }
|