private checkMessageWorkerId (message: MessageValue<Response>): void {
if (
message.workerId != null &&
- this.getWorkerNodeKeyByWorkerId(message.workerId) == null
+ this.getWorkerNodeKeyByWorkerId(message.workerId) === -1
) {
throw new Error(
`Worker message received from unknown worker '${message.workerId}'`
* @param worker - The worker.
* @returns The worker node key if found in the pool worker nodes, `-1` otherwise.
*/
- private getWorkerNodeKey (worker: Worker): number {
+ private getWorkerNodeKeyByWorker (worker: Worker): number {
return this.workerNodes.findIndex(
workerNode => workerNode.worker === worker
)
* Gets the worker node key given its worker id.
*
* @param workerId - The worker id.
- * @returns The worker node key if the worker id is found in the pool worker nodes, `undefined` otherwise.
+ * @returns The worker node key if the worker id is found in the pool worker nodes, `-1` otherwise.
*/
- private getWorkerNodeKeyByWorkerId (workerId: number): number | undefined {
- for (const [workerNodeKey, workerNode] of this.workerNodes.entries()) {
- if (workerNode.info.id === workerId) {
- return workerNodeKey
- }
- }
+ private getWorkerNodeKeyByWorkerId (workerId: number): number {
+ return this.workerNodes.findIndex(
+ workerNode => workerNode.info.id === workerId
+ )
}
/** @inheritDoc */
worker.on('message', this.opts.messageHandler ?? EMPTY_FUNCTION)
worker.on('error', this.opts.errorHandler ?? EMPTY_FUNCTION)
worker.on('error', error => {
- const workerNodeKey = this.getWorkerNodeKey(worker)
+ const workerNodeKey = this.getWorkerNodeKeyByWorker(worker)
const workerInfo = this.getWorkerInfo(workerNodeKey)
workerInfo.ready = false
this.workerNodes[workerNodeKey].closeChannel()
this.registerWorkerMessageListener(workerNodeKey, message => {
const localWorkerNodeKey = this.getWorkerNodeKeyByWorkerId(
message.workerId
- ) as number
+ )
const workerUsage = this.workerNodes[localWorkerNodeKey].usage
if (
isKillBehavior(KillBehaviors.HARD, message.kill) ||
private handleWorkerReadyResponse (message: MessageValue<Response>): void {
this.getWorkerInfo(
- this.getWorkerNodeKeyByWorkerId(message.workerId) as number
+ this.getWorkerNodeKeyByWorkerId(message.workerId)
).ready = message.ready as boolean
if (this.emitter != null && this.ready) {
this.emitter.emit(PoolEvents.ready, this.info)
workerNode.info.ready = true
}
this.workerNodes.push(workerNode)
- const workerNodeKey = this.getWorkerNodeKey(worker)
+ const workerNodeKey = this.getWorkerNodeKeyByWorker(worker)
if (workerNodeKey === -1) {
throw new Error('Worker node not found')
}
* @param worker - The worker.
*/
private removeWorkerNode (worker: Worker): void {
- const workerNodeKey = this.getWorkerNodeKey(worker)
+ const workerNodeKey = this.getWorkerNodeKeyByWorker(worker)
if (workerNodeKey !== -1) {
this.workerNodes.splice(workerNodeKey, 1)
this.workerChoiceStrategyContext.remove(workerNodeKey)