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 | });
|
31 |
|
32 | limiter.ready().then(() => { console.log('Ready') });
|
33 | limiter.clients().client;
|
34 | limiter.disconnect();
|
35 |
|
36 | limiter.currentReservoir().then(function (x) {
|
37 | if (x != null) {
|
38 | let i: number = x;
|
39 | }
|
40 | });
|
41 |
|
42 | limiter.incrementReservoir(5).then(function (x) {
|
43 | if (x != null) {
|
44 | let i: number = x;
|
45 | }
|
46 | });
|
47 |
|
48 | limiter.running().then(function (x) {
|
49 | let i: number = x;
|
50 | });
|
51 |
|
52 | limiter.done().then(function (x) {
|
53 | let i: number = x;
|
54 | });
|
55 |
|
56 | limiter.submit(withCb, 1, () => {}, (err, result) => {
|
57 | let s: string = result;
|
58 | console.log(s);
|
59 | assert(s == "cb 1");
|
60 | });
|
61 |
|
62 | function withPromise(foo: number, bar: () => void): PromiseLike<string> {
|
63 | let s: string = `promise ${foo}`;
|
64 | return Promise.resolve(s);
|
65 | }
|
66 |
|
67 | let foo: Promise<string> = limiter.schedule(withPromise, 1, () => {});
|
68 | foo.then(function (result: string) {
|
69 | let s: string = result;
|
70 | console.log(s);
|
71 | assert(s == "promise 1");
|
72 | });
|
73 |
|
74 | limiter.on("message", (msg) => console.log(msg));
|
75 |
|
76 | limiter.publish(JSON.stringify({ a: "abc", b: { c: 123 }}));
|
77 |
|
78 | let group = new Bottleneck.Group({
|
79 | maxConcurrent: 5,
|
80 | minTime: 1000,
|
81 | highWater: 10,
|
82 | strategy: Bottleneck.strategy.LEAK,
|
83 | datastore: "ioredis",
|
84 | clearDatastore: true,
|
85 | clientOptions: {},
|
86 | clusterNodes: []
|
87 | });
|
88 |
|
89 | group.on('created', (limiter, key) => {
|
90 | assert(limiter.empty())
|
91 | assert(key.length > 0)
|
92 | })
|
93 |
|
94 | group.key("foo").submit(withCb, 2, () => {}, (err, result) => {
|
95 | let s: string = `${result} foo`;
|
96 | console.log(s);
|
97 | assert(s == "cb 2 foo");
|
98 | });
|
99 |
|
100 | group.key("bar").submit({ priority: 4 }, withCb, 3, () => {}, (err, result) => {
|
101 | let s: string = `${result} bar`;
|
102 | console.log(s);
|
103 | assert(s == "cb 3 foo");
|
104 | });
|
105 |
|
106 | let f1: Promise<string> = group.key("pizza").schedule(withPromise, 2, () => {});
|
107 | f1.then(function (result: string) {
|
108 | let s: string = result;
|
109 | console.log(s);
|
110 | assert(s == "promise 2");
|
111 | });
|
112 |
|
113 | let f2: Promise<string> = group.key("pie").schedule({ priority: 4 }, withPromise, 3, () => {});
|
114 | f2.then(function (result: string) {
|
115 | let s: string = result;
|
116 | console.log(s);
|
117 | assert(s == "promise 3");
|
118 | });
|
119 |
|
120 | let wrapped = limiter.wrap((a: number, b: number) => {
|
121 | let s: string = `Total: ${a + b}`;
|
122 | return Promise.resolve(s);
|
123 | });
|
124 |
|
125 | wrapped(1, 2).then((x) => {
|
126 | let s: string = x;
|
127 | console.log(s);
|
128 | assert(s == "Total: 3");
|
129 | });
|
130 |
|
131 | wrapped.withOptions({ priority: 1, id: 'some-id' }, 9, 9).then((x) => {
|
132 | let s: string = x;
|
133 | console.log(s);
|
134 | assert(s == "Total: 18");
|
135 | })
|
136 |
|
137 | let counts = limiter.counts();
|
138 | console.log(`${counts.EXECUTING + 2}`);
|
139 | console.log(limiter.jobStatus('some-id'))
|
140 | console.log(limiter.jobs());
|
141 | console.log(limiter.jobs(Bottleneck.Status.RUNNING));
|
142 |
|
143 |
|
144 | group.deleteKey("pizza")
|
145 | .then(function (deleted: boolean) {
|
146 | console.log(deleted)
|
147 | });
|
148 | group.updateSettings({ timeout: 5, maxConcurrent: null, reservoir: null });
|
149 |
|
150 | let keys: string[] = group.keys();
|
151 | assert(keys.length == 3);
|
152 |
|
153 | group.clusterKeys()
|
154 | .then(function (allKeys: string[]) {
|
155 | let count = allKeys.length;
|
156 | })
|
157 |
|
158 | let queued: number = limiter.chain(group.key("pizza")).queued();
|
159 |
|
160 | limiter.stop({
|
161 | dropWaitingJobs: true,
|
162 | dropErrorMessage: "Begone!",
|
163 | enqueueErrorMessage: "Denied!"
|
164 | }).then(() => {
|
165 | console.log('All stopped.')
|
166 | })
|
167 |
|
168 | wrapped(4, 5).catch((e) => {
|
169 | assert(e.message === "Denied!")
|
170 | })
|
171 |
|
172 | const id: string = limiter.id;
|
173 | const datastore: string = limiter.datastore;
|
174 | const channel: string = limiter.channel();
|
175 |
|
176 | const redisConnection = new Bottleneck.RedisConnection({
|
177 | client: "NodeRedis client object",
|
178 | clientOptions: {}
|
179 | })
|
180 |
|
181 | redisConnection.ready()
|
182 | .then(function (redisConnectionClients) {
|
183 | const client = redisConnectionClients.client;
|
184 | const subscriber = redisConnectionClients.subscriber;
|
185 | })
|
186 |
|
187 | redisConnection.on("error", (err) => {
|
188 | console.log(err.message)
|
189 | })
|
190 |
|
191 | const limiterWithConn = new Bottleneck({
|
192 | connection: redisConnection
|
193 | })
|
194 |
|
195 | const ioredisConnection = new Bottleneck.IORedisConnection({
|
196 | client: "ioredis client object",
|
197 | clientOptions: {},
|
198 | clusterNodes: []
|
199 | })
|
200 |
|
201 | ioredisConnection.ready()
|
202 | .then(function (ioredisConnectionClients) {
|
203 | const client = ioredisConnectionClients.client;
|
204 | const subscriber = ioredisConnectionClients.subscriber;
|
205 | })
|
206 |
|
207 | ioredisConnection.on("error", (err: Bottleneck.BottleneckError) => {
|
208 | console.log(err.message)
|
209 | })
|
210 |
|
211 | const groupWithConn = new Bottleneck.Group({
|
212 | connection: ioredisConnection
|
213 | })
|
214 |
|
215 | const limiterWithConnFromGroup = new Bottleneck({
|
216 | connection: groupWithConn.connection
|
217 | })
|
218 |
|
219 | const groupWithConnFromLimiter = new Bottleneck.Group({
|
220 | connection: limiterWithConn.connection
|
221 | })
|
222 |
|
223 |
|
224 | const batcher = new Bottleneck.Batcher({
|
225 | maxTime: 1000,
|
226 | maxSize: 10
|
227 | })
|
228 |
|
229 | batcher.on("batch", (batch) => {
|
230 | const len: number = batch.length
|
231 | console.log("Number of elements:", len)
|
232 | })
|
233 |
|
234 | batcher.on("error", (err: Bottleneck.BottleneckError) => {
|
235 | console.log(err.message)
|
236 | })
|
237 |
|
238 | batcher.add("abc")
|
239 | batcher.add({ xyz: 5 })
|
240 | .then(() => console.log("Flushed!"))
|
241 |
|
242 | const object = {}
|
243 | const emitter = new Bottleneck.Events(object)
|
244 | const listenerCount: number = emitter.listenerCount('info')
|
245 | emitter.trigger('info', 'hello', 'world', 123).then(function (result) {
|
246 | console.log(result)
|
247 | })
|