diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-01-24 02:10:43 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-01-24 02:10:43 +0800 |
commit | d965fdb11dfba5631c1f98ad3e309876c199999a (patch) | |
tree | ec10b7341707cfcc6841d1b5fcbf0d3852536a79 /packages/testnet-faucets/src/ts/request_queue.ts | |
parent | 8a858501f2b633f47cec9576b25e6f99911bbc09 (diff) | |
download | dexon-sol-tools-d965fdb11dfba5631c1f98ad3e309876c199999a.tar dexon-sol-tools-d965fdb11dfba5631c1f98ad3e309876c199999a.tar.gz dexon-sol-tools-d965fdb11dfba5631c1f98ad3e309876c199999a.tar.bz2 dexon-sol-tools-d965fdb11dfba5631c1f98ad3e309876c199999a.tar.lz dexon-sol-tools-d965fdb11dfba5631c1f98ad3e309876c199999a.tar.xz dexon-sol-tools-d965fdb11dfba5631c1f98ad3e309876c199999a.tar.zst dexon-sol-tools-d965fdb11dfba5631c1f98ad3e309876c199999a.zip |
Rename to testnet-faucets
Diffstat (limited to 'packages/testnet-faucets/src/ts/request_queue.ts')
-rw-r--r-- | packages/testnet-faucets/src/ts/request_queue.ts | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/packages/testnet-faucets/src/ts/request_queue.ts b/packages/testnet-faucets/src/ts/request_queue.ts new file mode 100644 index 000000000..2b42ca4bf --- /dev/null +++ b/packages/testnet-faucets/src/ts/request_queue.ts @@ -0,0 +1,56 @@ +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. +// Filed issue: https://github.com/ethereum/web3.js/issues/844 +// 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(() => { + const recipientAddress = this.queue.shift(); + if (_.isUndefined(recipientAddress)) { + return; + } + // 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'); + } +} |