|
| 1 | +import { CryptoAlgorithm } from '../CryptographyTypes'; |
| 2 | +import { CryptoItem } from '../Scanneable/CryptoItem'; |
| 3 | +import { IWorkerResponse, Worker } from './Worker'; |
| 4 | + |
| 5 | +export class ThreadPool { |
| 6 | + |
| 7 | + private readonly maxWorkers: number; |
| 8 | + |
| 9 | + private readonly workers: Array<Worker> = []; |
| 10 | + |
| 11 | + private readonly jobsQueue: any; |
| 12 | + |
| 13 | + private readonly cryptoRules: Map<string, RegExp>; |
| 14 | + |
| 15 | + private readonly cryptoMapper: Map<string, CryptoAlgorithm>; |
| 16 | + |
| 17 | + private results = []; |
| 18 | + |
| 19 | + private activeWorkers = 0; |
| 20 | + |
| 21 | + private resolve; |
| 22 | + |
| 23 | + private reject; |
| 24 | + constructor(maxWorkers = 3, rules:Map<string, RegExp>, cryptoMapper: Map<string, CryptoAlgorithm> ) { |
| 25 | + this.maxWorkers = maxWorkers; |
| 26 | + this.workers = []; |
| 27 | + this.jobsQueue = []; |
| 28 | + this.cryptoRules = rules; |
| 29 | + this.cryptoMapper = cryptoMapper; |
| 30 | + } |
| 31 | + |
| 32 | + enqueueTask(item: CryptoItem) { |
| 33 | + return new Promise((resolve, reject) => { |
| 34 | + const job = { item, resolve, reject }; |
| 35 | + this.jobsQueue.push(job); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + async init(): Promise<void> { |
| 40 | + for (let i = 0; i < this.maxWorkers; i++) { |
| 41 | + const worker = new Worker(); |
| 42 | + worker.on('message', async(item: IWorkerResponse) => { |
| 43 | + this.results.push(item.result); |
| 44 | + this.releaseWorker(item.id); |
| 45 | + await this.next(); |
| 46 | + }); |
| 47 | + |
| 48 | + // TODO: See what can be done in case an error on the thread |
| 49 | + worker.on('error', async (error) => { |
| 50 | + console.log(error); |
| 51 | + this.releaseWorker(worker.getId()); |
| 52 | + await this.next(); |
| 53 | + }); |
| 54 | + |
| 55 | + this.workers.push(worker); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private releaseWorker(id: number): number { |
| 60 | + const wId = this.workers.findIndex(w => w.getId() === id); |
| 61 | + const w = this.workers[wId]; |
| 62 | + w.release(); |
| 63 | + return w.getId(); |
| 64 | + } |
| 65 | + |
| 66 | + private async next(){ |
| 67 | + this.activeWorkers -= 1; |
| 68 | + if (this.activeWorkers === 0 && this.jobsQueue.length === 0) { |
| 69 | + await this.destroyAllWorkers(); |
| 70 | + this.resolve(this.results); |
| 71 | + } else { |
| 72 | + this.processItem(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private async destroyAllWorkers() { |
| 77 | + for (const worker of this.workers) { |
| 78 | + await worker.terminate(); // Terminate each worker |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + async processQueue(): Promise<Array<CryptoItem>> { |
| 83 | + return new Promise(async(resolve, reject) => { |
| 84 | + this.resolve = resolve; |
| 85 | + this.reject = reject; |
| 86 | + this.processItem(); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + private processItem() { |
| 91 | + if (this.workers.length > 0 && this.jobsQueue.length > 0) { |
| 92 | + const freeWorkerIndices = this.workers.reduce((indices, worker, index) => { |
| 93 | + if (worker.isFree()) { |
| 94 | + indices.push(index); |
| 95 | + } |
| 96 | + return indices; |
| 97 | + }, []); |
| 98 | + |
| 99 | + freeWorkerIndices.forEach(workerIndex => { |
| 100 | + if (this.jobsQueue.length <= 0) return; |
| 101 | + const { item, reject } = this.jobsQueue.shift(); |
| 102 | + const worker = this.workers[workerIndex]; |
| 103 | + worker.run({ item, rules: this.cryptoRules, cryptoMapper: this.cryptoMapper }); |
| 104 | + this.activeWorkers += 1; |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | + |
0 commit comments