-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(concurrency): ability to limit max simultaneous requests (#76)
* rebase to devel current, yahooFinanceFetch typescript * implement our own Queue that relays promises * coverage
- Loading branch information
Showing
5 changed files
with
208 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
export default { | ||
// TODO, keep defaults there too? | ||
import type { ValidationOptions } from "./validateAndCoerceTypes"; | ||
import type { QueueOptions } from "./queue"; | ||
|
||
export interface Options { | ||
queue: QueueOptions; | ||
validation: ValidationOptions; | ||
} | ||
|
||
const options: Options = { | ||
queue: { | ||
concurrency: 4, // Min: 1, Max: Infinity | ||
timeout: 60, | ||
}, | ||
validation: { | ||
logErrors: true, | ||
logOptionsErrors: true, | ||
}, | ||
}; | ||
|
||
export default options; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
interface Job { | ||
func: Function; | ||
resolve: Function; | ||
reject: Function; | ||
} | ||
|
||
export interface QueueOptions { | ||
_queue?: Queue; | ||
concurrency?: number; | ||
timeout?: number; // TODO | ||
} | ||
|
||
export default class Queue { | ||
concurrency: number = 1; | ||
|
||
_running: number = 0; | ||
_queue: Array<Job> = []; | ||
|
||
constructor(opts: QueueOptions = {}) { | ||
if (opts.concurrency) this.concurrency = opts.concurrency; | ||
} | ||
|
||
runNext() { | ||
const job = this._queue.shift(); | ||
if (!job) return; | ||
|
||
this._running++; | ||
job | ||
.func() | ||
.then((result: any) => job.resolve(result)) | ||
.catch((error: any) => job.reject(error)) | ||
.finally(() => { | ||
this._running--; | ||
this.checkQueue(); | ||
}); | ||
} | ||
|
||
checkQueue() { | ||
if (this._running < this.concurrency) this.runNext(); | ||
} | ||
|
||
add(func: Function) { | ||
return new Promise((resolve, reject) => { | ||
this._queue.push({ func, resolve, reject }); | ||
this.checkQueue(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters