aboutsummaryrefslogtreecommitdiffstats
path: root/packages/testnet-faucets/src/ts/dispatch_queue.ts
diff options
context:
space:
mode:
authorBrandon Millman <brandon@0xproject.com>2018-02-08 03:16:24 +0800
committerGitHub <noreply@github.com>2018-02-08 03:16:24 +0800
commit852811b314de3d62ee9068e3c5cbcfaa0650b058 (patch)
tree2740ad3525a18986ebf09e7e24bba2a755fc35f4 /packages/testnet-faucets/src/ts/dispatch_queue.ts
parente443cb2a3b58003c5e0dffc0ea2aaa955fc12366 (diff)
parent2404ff030422e5ad021536135d57a19e98bedcd8 (diff)
downloaddexon-sol-tools-852811b314de3d62ee9068e3c5cbcfaa0650b058.tar
dexon-sol-tools-852811b314de3d62ee9068e3c5cbcfaa0650b058.tar.gz
dexon-sol-tools-852811b314de3d62ee9068e3c5cbcfaa0650b058.tar.bz2
dexon-sol-tools-852811b314de3d62ee9068e3c5cbcfaa0650b058.tar.lz
dexon-sol-tools-852811b314de3d62ee9068e3c5cbcfaa0650b058.tar.xz
dexon-sol-tools-852811b314de3d62ee9068e3c5cbcfaa0650b058.tar.zst
dexon-sol-tools-852811b314de3d62ee9068e3c5cbcfaa0650b058.zip
Merge pull request #375 from 0xProject/feature/testnet-faucets/queue-by-network
Organize async task queues by network
Diffstat (limited to 'packages/testnet-faucets/src/ts/dispatch_queue.ts')
-rw-r--r--packages/testnet-faucets/src/ts/dispatch_queue.ts54
1 files changed, 54 insertions, 0 deletions
diff --git a/packages/testnet-faucets/src/ts/dispatch_queue.ts b/packages/testnet-faucets/src/ts/dispatch_queue.ts
new file mode 100644
index 000000000..672511619
--- /dev/null
+++ b/packages/testnet-faucets/src/ts/dispatch_queue.ts
@@ -0,0 +1,54 @@
+import { intervalUtils } from '@0xproject/utils';
+import * as _ from 'lodash';
+
+import { errorReporter } from './error_reporter';
+import { utils } from './utils';
+
+const MAX_QUEUE_SIZE = 500;
+const DEFAULT_QUEUE_INTERVAL_MS = 1000;
+
+export class DispatchQueue {
+ private _queueIntervalMs: number;
+ private _queue: Array<() => Promise<void>>;
+ private _queueIntervalIdIfExists?: NodeJS.Timer;
+ constructor() {
+ this._queueIntervalMs = DEFAULT_QUEUE_INTERVAL_MS;
+ this._queue = [];
+ this._start();
+ }
+ public add(taskAsync: () => Promise<void>): boolean {
+ if (this.isFull()) {
+ return false;
+ }
+ this._queue.push(taskAsync);
+ return true;
+ }
+ public size(): number {
+ return this._queue.length;
+ }
+ public isFull(): boolean {
+ return this.size() >= MAX_QUEUE_SIZE;
+ }
+ public stop() {
+ if (!_.isUndefined(this._queueIntervalIdIfExists)) {
+ intervalUtils.clearAsyncExcludingInterval(this._queueIntervalIdIfExists);
+ }
+ }
+ private _start() {
+ this._queueIntervalIdIfExists = intervalUtils.setAsyncExcludingInterval(
+ async () => {
+ const taskAsync = this._queue.shift();
+ if (_.isUndefined(taskAsync)) {
+ return Promise.resolve();
+ }
+ await taskAsync();
+ },
+ this._queueIntervalMs,
+ (err: Error) => {
+ utils.consoleLog(`Unexpected err: ${err} - ${JSON.stringify(err)}`);
+ // tslint:disable-next-line:no-floating-promises
+ errorReporter.reportAsync(err);
+ },
+ );
+ }
+}