1 |
|
2 |
|
3 | import Bottleneck from "bottleneck";
|
4 |
|
5 | function assert(b: boolean): void { }
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function withCb(foo: number, bar: () => void, cb: (err: any, result: string) => void) {
|
17 | let s: string = `cb ${foo}`;
|
18 | cb(null, s);
|
19 | }
|
20 |
|
21 | console.log(Bottleneck);
|
22 |
|
23 | let limiter = new Bottleneck({
|
24 | maxConcurrent: 5,
|
25 | minTime: 1000,
|
26 | highWater: 20,
|
27 | strategy: Bottleneck.strategy.LEAK,
|
28 | reservoirRefreshInterval: 1000 * 60,
|
29 | reservoirRefreshAmount: 10,
|
30 | reservoirIncreaseInterval: 1000 * 60,
|
31 | reservoirIncreaseAmount: 2,
|
32 | reservoirIncreaseMaximum: 15
|
33 | });
|
34 |
|
35 | limiter.ready().then(() => { console.log('Ready') });
|
36 | limiter.clients().client;
|
37 | limiter.disconnect();
|
38 |
|
39 | limiter.currentReservoir().then(function (x) {
|
40 | if (x != null) {
|
41 | let i: number = x;
|
42 | }
|
43 | });
|
44 |
|
45 | limiter.incrementReservoir(5).then(function (x) {
|
46 | if (x != null) {
|
47 | let i: number = x;
|
48 | }
|
49 | });
|
50 |
|
51 | limiter.running().then(function (x) {
|
52 | let i: number = x;
|
53 | });
|
54 |
|
55 | limiter.clusterQueued().then(function (x) {
|
56 | let i: number = x;
|
57 | });
|
58 |
|
59 | limiter.done().then(function (x) {
|
60 | let i: number = x;
|
61 | });
|
62 |
|
63 | limiter.submit(withCb, 1, () => {}, (err, result) => {
|
64 | let s: string = result;
|
65 | console.log(s);
|
66 | assert(s == "cb 1");
|
67 | });
|
68 |
|
69 | function withPromise(foo: number, bar: () => void): PromiseLike<string> {
|
70 | let s: string = `promise ${foo}`;
|
71 | return Promise.resolve(s);
|
72 | }
|
73 |
|
74 | let foo: Promise<string> = limiter.schedule(withPromise, 1, () => {});
|
75 | foo.then(function (result: string) {
|
76 | let s: string = result;
|
77 | console.log(s);
|
78 | assert(s == "promise 1");
|
79 | });
|
80 |
|
81 | limiter.on("message", (msg) => console.log(msg));
|
82 |
|
83 | limiter.publish(JSON.stringify({ a: "abc", b: { c: 123 }}));
|
84 |
|
85 | function checkEventInfo(info: Bottleneck.EventInfo) {
|
86 | const numArgs: number = info.args.length;
|
87 | const id: string = info.options.id;
|
88 | }
|
89 |
|
90 | limiter.on('dropped', (info) => {
|
91 | checkEventInfo(info)
|
92 | const task: Function = info.task;
|
93 | const promise: Promise<any> = info.promise;
|
94 | })
|
95 |
|
96 | limiter.on('received', (info) => {
|
97 | checkEventInfo(info)
|
98 | })
|
99 |
|
100 | limiter.on('queued', (info) => {
|
101 | checkEventInfo(info)
|
102 | const blocked: boolean = info.blocked;
|
103 | const reachedHWM: boolean = info.reachedHWM;
|
104 | })
|
105 |
|
106 | limiter.on('scheduled', (info) => {
|
107 | checkEventInfo(info)
|
108 | })
|
109 |
|
110 | limiter.on('executing', (info) => {
|
111 | checkEventInfo(info)
|
112 | const count: number = info.retryCount;
|
113 | })
|
114 |
|
115 | limiter.on('failed', (error, info) => {
|
116 | checkEventInfo(info)
|
117 | const message: string = error.message;
|
118 | const count: number = info.retryCount;
|
119 | return Promise.resolve(10)
|
120 | })
|
121 |
|
122 | limiter.on('failed', (error, info) => {
|
123 | checkEventInfo(info)
|
124 | const message: string = error.message;
|
125 | const count: number = info.retryCount;
|
126 | return Promise.resolve(null)
|
127 | })
|
128 |
|
129 | limiter.on('failed', (error, info) => {
|
130 | checkEventInfo(info)
|
131 | const message: string = error.message;
|
132 | const count: number = info.retryCount;
|
133 | return Promise.resolve()
|
134 | })
|
135 |
|
136 | limiter.on('failed', (error, info) => {
|
137 | checkEventInfo(info)
|
138 | const message: string = error.message;
|
139 | const count: number = info.retryCount;
|
140 | return 10
|
141 | })
|
142 |
|
143 | limiter.on('failed', (error, info) => {
|
144 | checkEventInfo(info)
|
145 | const message: string = error.message;
|
146 | const count: number = info.retryCount;
|
147 | return null
|
148 | })
|
149 |
|
150 | limiter.on('failed', (error, info) => {
|
151 | checkEventInfo(info)
|
152 | const message: string = error.message;
|
153 | const count: number = info.retryCount;
|
154 | })
|
155 |
|
156 | limiter.on('retry', (message: string, info) => {
|
157 | checkEventInfo(info)
|
158 | const count: number = info.retryCount;
|
159 | })
|
160 |
|
161 | limiter.on('done', (info) => {
|
162 | checkEventInfo(info)
|
163 | const count: number = info.retryCount;
|
164 | })
|
165 |
|
166 | let group = new Bottleneck.Group({
|
167 | maxConcurrent: 5,
|
168 | minTime: 1000,
|
169 | highWater: 10,
|
170 | strategy: Bottleneck.strategy.LEAK,
|
171 | datastore: "ioredis",
|
172 | clearDatastore: true,
|
173 | clientOptions: {},
|
174 | clusterNodes: []
|
175 | });
|
176 |
|
177 | group.on('created', (limiter, key) => {
|
178 | assert(limiter.empty())
|
179 | assert(key.length > 0)
|
180 | })
|
181 |
|
182 | group.key("foo").submit(withCb, 2, () => {}, (err, result) => {
|
183 | let s: string = `${result} foo`;
|
184 | console.log(s);
|
185 | assert(s == "cb 2 foo");
|
186 | });
|
187 |
|
188 | group.key("bar").submit({ priority: 4 }, withCb, 3, () => {}, (err, result) => {
|
189 | let s: string = `${result} bar`;
|
190 | console.log(s);
|
191 | assert(s == "cb 3 foo");
|
192 | });
|
193 |
|
194 | let f1: Promise<string> = group.key("pizza").schedule(withPromise, 2, () => {});
|
195 | f1.then(function (result: string) {
|
196 | let s: string = result;
|
197 | console.log(s);
|
198 | assert(s == "promise 2");
|
199 | });
|
200 |
|
201 | let f2: Promise<string> = group.key("pie").schedule({ priority: 4 }, withPromise, 3, () => {});
|
202 | f2.then(function (result: string) {
|
203 | let s: string = result;
|
204 | console.log(s);
|
205 | assert(s == "promise 3");
|
206 | });
|
207 |
|
208 | let wrapped = limiter.wrap((a: number, b: number) => {
|
209 | let s: string = `Total: ${a + b}`;
|
210 | return Promise.resolve(s);
|
211 | });
|
212 |
|
213 | wrapped(1, 2).then((x) => {
|
214 | let s: string = x;
|
215 | console.log(s);
|
216 | assert(s == "Total: 3");
|
217 | });
|
218 |
|
219 | wrapped.withOptions({ priority: 1, id: 'some-id' }, 9, 9).then((x) => {
|
220 | let s: string = x;
|
221 | console.log(s);
|
222 | assert(s == "Total: 18");
|
223 | })
|
224 |
|
225 | let counts = limiter.counts();
|
226 | console.log(`${counts.EXECUTING + 2}`);
|
227 | console.log(limiter.jobStatus('some-id'))
|
228 | console.log(limiter.jobs());
|
229 | console.log(limiter.jobs(Bottleneck.Status.RUNNING));
|
230 |
|
231 |
|
232 | group.deleteKey("pizza")
|
233 | .then(function (deleted: boolean) {
|
234 | console.log(deleted)
|
235 | });
|
236 | group.updateSettings({ timeout: 5, maxConcurrent: null, reservoir: null });
|
237 |
|
238 | let keys: string[] = group.keys();
|
239 | assert(keys.length == 3);
|
240 |
|
241 | group.clusterKeys()
|
242 | .then(function (allKeys: string[]) {
|
243 | let count = allKeys.length;
|
244 | })
|
245 |
|
246 | let queued: number = limiter.chain(group.key("pizza")).queued();
|
247 |
|
248 | limiter.stop({
|
249 | dropWaitingJobs: true,
|
250 | dropErrorMessage: "Begone!",
|
251 | enqueueErrorMessage: "Denied!"
|
252 | }).then(() => {
|
253 | console.log('All stopped.')
|
254 | })
|
255 |
|
256 | wrapped(4, 5).catch((e) => {
|
257 | assert(e.message === "Denied!")
|
258 | })
|
259 |
|
260 | const id: string = limiter.id;
|
261 | const datastore: string = limiter.datastore;
|
262 | const channel: string = limiter.channel();
|
263 |
|
264 | const redisConnection = new Bottleneck.RedisConnection({
|
265 | client: "NodeRedis client object",
|
266 | clientOptions: {}
|
267 | })
|
268 |
|
269 | redisConnection.ready()
|
270 | .then(function (redisConnectionClients) {
|
271 | const client = redisConnectionClients.client;
|
272 | const subscriber = redisConnectionClients.subscriber;
|
273 | })
|
274 |
|
275 | redisConnection.on("error", (err) => {
|
276 | console.log(err.message)
|
277 | })
|
278 |
|
279 | const limiterWithConn = new Bottleneck({
|
280 | connection: redisConnection
|
281 | })
|
282 |
|
283 | const ioredisConnection = new Bottleneck.IORedisConnection({
|
284 | client: "ioredis client object",
|
285 | clientOptions: {},
|
286 | clusterNodes: []
|
287 | })
|
288 |
|
289 | ioredisConnection.ready()
|
290 | .then(function (ioredisConnectionClients) {
|
291 | const client = ioredisConnectionClients.client;
|
292 | const subscriber = ioredisConnectionClients.subscriber;
|
293 | })
|
294 |
|
295 | ioredisConnection.on("error", (err: Bottleneck.BottleneckError) => {
|
296 | console.log(err.message)
|
297 | })
|
298 |
|
299 | const groupWithConn = new Bottleneck.Group({
|
300 | connection: ioredisConnection
|
301 | })
|
302 |
|
303 | const limiterWithConnFromGroup = new Bottleneck({
|
304 | connection: groupWithConn.connection
|
305 | })
|
306 |
|
307 | const groupWithConnFromLimiter = new Bottleneck.Group({
|
308 | connection: limiterWithConn.connection
|
309 | })
|
310 |
|
311 |
|
312 | const batcher = new Bottleneck.Batcher({
|
313 | maxTime: 1000,
|
314 | maxSize: 10
|
315 | })
|
316 |
|
317 | batcher.on("batch", (batch) => {
|
318 | const len: number = batch.length
|
319 | console.log("Number of elements:", len)
|
320 | })
|
321 |
|
322 | batcher.on("error", (err: Bottleneck.BottleneckError) => {
|
323 | console.log(err.message)
|
324 | })
|
325 |
|
326 | batcher.add("abc")
|
327 | batcher.add({ xyz: 5 })
|
328 | .then(() => console.log("Flushed!"))
|
329 |
|
330 | const object = {}
|
331 | const emitter = new Bottleneck.Events(object)
|
332 | const listenerCount: number = emitter.listenerCount('info')
|
333 | emitter.trigger('info', 'hello', 'world', 123).then(function (result) {
|
334 | console.log(result)
|
335 | })
|