Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | 2x 2x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 14x 18x 25x 25x 6x 6x 6x 6x 25x 7x 6x 6x 6x 6x 6x 1x 1x 18x 15x 15x 1x 4x 14x 14x 2x 14x 14x 13x 13x 13x 13x 13x 2x 2x 2x 13x 3x 3x 3x 3x 13x 9x 8x 8x 8x 8x 8x 13x 2x 1x 1x 1x 13x 14x 12x 12x 12x 12x 3x 3x 3x 12x 15x 14x 13x 13x 12x 13x 1x 14x 14x 4x 4x | /*
Copyright (C) 2023 Sber
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { EventEmitter } from 'node:events';
import { Worker } from 'worker_threads';
const currentJob = Symbol('currentJob');
const replaced = Symbol('replaced');
// The same thread is handed out again after a successful message, so a worker
// has to stay alive once it has posted a result. A thread that exits after a
// reply is dropped and replaced; this pool is built for long-lived workers
// (smartants), not for scripts that exit after one postMessage.
export class WorkerStack {
constructor(workerCount, source, workerTimeout, requestTimeout) {
this.stack = [];
this.max = workerCount;
this.source = source;
this.workerTimeout = workerTimeout;
this.queueTimeout = requestTimeout;
this.taskQueue = [];
this.dequeueing = false;
this.emitter = new EventEmitter();
this.dequeue = this.dequeue.bind(this);
this.emitter.on('ready', this.dequeue);
if (source) {
for (let i = 0; i < workerCount; i++) {
this.stack.push(this.createWorker());
}
}
}
createWorker() {
const worker = new Worker(this.source);
worker.on('exit', () => {
const job = worker[currentJob];
worker[currentJob] = undefined;
if (job) job();
this.replace(worker);
});
return worker;
}
// One replacement per dead thread. Exit of an idle worker and a later pop
// of that same corpse must not each start a new thread.
replace(worker) {
if (worker[replaced]) return;
worker[replaced] = true;
const index = this.stack.indexOf(worker);
if (index !== -1) this.stack.splice(index, 1);
const next = this.createWorker();
if (!this.put(next)) {
next[replaced] = true;
next.terminate();
}
}
// threadId is -1 after the thread has stopped.
acquire() {
while (this.stack.length > 0) {
const worker = this.stack.pop();
if (worker.threadId !== -1) return worker;
this.replace(worker);
}
return undefined;
}
async execute(data) {
const timeout = new Promise((_resolve, reject) => {
setTimeout(() => {
reject('Queue timeout');
}, this.queueTimeout);
});
const result = new Promise((resolve, reject) => {
this.retrieve().then((worker) => {
worker.removeAllListeners('message');
worker.removeAllListeners('error');
let settled = false;
let killed = false;
const controller = setTimeout(() => {
Iif (settled) return;
killed = true;
worker.terminate();
}, this.workerTimeout);
worker[currentJob] = () => {
clearTimeout(controller);
Iif (settled) return;
settled = true;
reject('Worker terminated');
};
worker.on('message', (outcome) => {
if (settled || killed) return;
settled = true;
clearTimeout(controller);
worker[currentJob] = undefined;
Eif (worker.threadId !== -1) this.put(worker);
resolve(outcome);
});
worker.on('error', (error) => {
if (settled) return;
settled = true;
clearTimeout(controller);
reject(error);
});
worker.postMessage(data);
});
});
return Promise.race([timeout, result]);
}
dequeue() {
Iif (this.dequeueing) return;
this.dequeueing = true;
try {
while (this.taskQueue.length > 0) {
const worker = this.acquire();
Iif (!worker) break;
this.taskQueue.shift()(worker);
}
} finally {
this.dequeueing = false;
}
}
put(worker) {
if (worker.threadId === -1) return false;
if (this.stack.length < this.max) {
this.stack.push(worker);
if (this.stack.length === 1) {
this.emitter.emit('ready');
}
return true;
} else return false;
}
async retrieve() {
const worker = this.acquire();
if (worker) return worker;
return new Promise((resolve) => {
this.taskQueue.push(resolve);
});
}
}
|