diff options
author | Brandon Millman <brandon@0xproject.com> | 2017-12-14 07:09:39 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-14 07:09:39 +0800 |
commit | 2ef763a19555b85814a80aa5d93a14c90c51c512 (patch) | |
tree | 5b41b8911e12569f669fc7b30f3ea2cc19a33611 /packages/kovan-faucets/src/ts/request_queue.ts | |
parent | 6bda6a22e3551707e7069e8ad6ea34d1ba0abd4d (diff) | |
parent | 6ba4ed99c42b4976902046625790698b490c3e0e (diff) | |
download | dexon-sol-tools-2ef763a19555b85814a80aa5d93a14c90c51c512.tar.gz dexon-sol-tools-2ef763a19555b85814a80aa5d93a14c90c51c512.tar.zst dexon-sol-tools-2ef763a19555b85814a80aa5d93a14c90c51c512.zip |
Merge pull request #229 from 0xProject/feature/addKovanFaucets
Add kovan faucet project into the mono repo
Diffstat (limited to 'packages/kovan-faucets/src/ts/request_queue.ts')
-rw-r--r-- | packages/kovan-faucets/src/ts/request_queue.ts | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/packages/kovan-faucets/src/ts/request_queue.ts b/packages/kovan-faucets/src/ts/request_queue.ts new file mode 100644 index 000000000..0b5e66ae9 --- /dev/null +++ b/packages/kovan-faucets/src/ts/request_queue.ts @@ -0,0 +1,57 @@ +import * as _ from 'lodash'; +import * as timers from 'timers'; + +// HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang +// because they are using the wrong XHR package. +// Issue: https://github.com/trufflesuite/truffle-contract/issues/14 +// tslint:disable-next-line:ordered-imports +import * as Web3 from 'web3'; + +const MAX_QUEUE_SIZE = 500; +const DEFAULT_QUEUE_INTERVAL_MS = 1000; + +export class RequestQueue { + protected queueIntervalMs: number; + protected queue: string[]; + protected queueIntervalId: NodeJS.Timer; + protected web3: Web3; + constructor(web3: any) { + this.queueIntervalMs = DEFAULT_QUEUE_INTERVAL_MS; + this.queue = []; + + this.web3 = web3; + + this.start(); + } + public add(recipientAddress: string): boolean { + if (this.isFull()) { + return false; + } + this.queue.push(recipientAddress); + return true; + } + public size(): number { + return this.queue.length; + } + public isFull(): boolean { + return this.size() >= MAX_QUEUE_SIZE; + } + protected start() { + this.queueIntervalId = timers.setInterval(() => { + if (this.queue.length === 0) { + return; + } + const recipientAddress = this.queue.shift(); + // tslint:disable-next-line:no-floating-promises + this.processNextRequestFireAndForgetAsync(recipientAddress); + + }, this.queueIntervalMs); + } + protected stop() { + clearInterval(this.queueIntervalId); + } + // tslint:disable-next-line:prefer-function-over-method + protected async processNextRequestFireAndForgetAsync(recipientAddress: string) { + throw new Error('Expected processNextRequestFireAndForgetAsync to be implemented by a superclass'); + } +} |