-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.ts
1109 lines (967 loc) · 29.1 KB
/
index.ts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
Worker,
MessageChannel,
MessagePort,
receiveMessageOnPort,
} from 'worker_threads'
import { once } from 'events'
import EventEmitterAsyncResource from './EventEmitterAsyncResource'
import { AsyncResource } from 'async_hooks'
import { fileURLToPath, URL } from 'url'
import { dirname, join, resolve } from 'path'
import { inspect, types } from 'util'
import assert from 'assert'
import { performance } from 'perf_hooks'
import { readFileSync } from 'fs'
import { amount as physicalCpuCount } from './physicalCpuCount'
import {
ReadyMessage,
RequestMessage,
ResponseMessage,
StartupMessage,
kResponseCountField,
kRequestCountField,
kFieldCount,
Transferable,
Task,
TaskQueue,
kQueueOptions,
isTransferable,
markMovable,
isMovable,
kTransferable,
kValue,
TinypoolData,
} from './common'
declare global {
namespace NodeJS {
interface Process {
__tinypool_state__: {
isWorkerThread: boolean
workerData: any
workerId: number
}
}
}
}
const cpuCount: number = physicalCpuCount
interface AbortSignalEventTargetAddOptions {
once: boolean
}
interface AbortSignalEventTarget {
addEventListener: (
name: 'abort',
listener: () => void,
options?: AbortSignalEventTargetAddOptions
) => void
removeEventListener: (name: 'abort', listener: () => void) => void
aborted?: boolean
}
interface AbortSignalEventEmitter {
off: (name: 'abort', listener: () => void) => void
once: (name: 'abort', listener: () => void) => void
}
type AbortSignalAny = AbortSignalEventTarget | AbortSignalEventEmitter
function onabort(abortSignal: AbortSignalAny, listener: () => void) {
if ('addEventListener' in abortSignal) {
abortSignal.addEventListener('abort', listener, { once: true })
} else {
abortSignal.once('abort', listener)
}
}
class AbortError extends Error {
constructor() {
super('The task has been aborted')
}
get name() {
return 'AbortError'
}
}
type ResourceLimits = Worker extends {
resourceLimits?: infer T
}
? T
: {}
type EnvSpecifier = typeof Worker extends {
new (filename: never, options?: { env: infer T }): Worker
}
? T
: never
class ArrayTaskQueue implements TaskQueue {
tasks: Task[] = []
get size() {
return this.tasks.length
}
shift(): Task | null {
return this.tasks.shift() as Task
}
push(task: Task): void {
this.tasks.push(task)
}
remove(task: Task): void {
const index = this.tasks.indexOf(task)
assert.notStrictEqual(index, -1)
this.tasks.splice(index, 1)
}
}
interface Options {
filename?: string | null
name?: string
minThreads?: number
maxThreads?: number
idleTimeout?: number
maxQueue?: number | 'auto'
concurrentTasksPerWorker?: number
useAtomics?: boolean
resourceLimits?: ResourceLimits
argv?: string[]
execArgv?: string[]
env?: EnvSpecifier
workerData?: any
taskQueue?: TaskQueue
trackUnmanagedFds?: boolean
isolateWorkers?: boolean
}
interface FilledOptions extends Options {
filename: string | null
name: string
minThreads: number
maxThreads: number
idleTimeout: number
maxQueue: number
concurrentTasksPerWorker: number
useAtomics: boolean
taskQueue: TaskQueue
}
const kDefaultOptions: FilledOptions = {
filename: null,
name: 'default',
minThreads: Math.max(cpuCount / 2, 1),
maxThreads: cpuCount,
idleTimeout: 0,
maxQueue: Infinity,
concurrentTasksPerWorker: 1,
useAtomics: true,
taskQueue: new ArrayTaskQueue(),
trackUnmanagedFds: true,
}
interface RunOptions {
transferList?: TransferList
filename?: string | null
signal?: AbortSignalAny | null
name?: string | null
}
interface FilledRunOptions extends RunOptions {
transferList: TransferList | never
filename: string | null
signal: AbortSignalAny | null
name: string | null
}
const kDefaultRunOptions: FilledRunOptions = {
transferList: undefined,
filename: null,
signal: null,
name: null,
}
class DirectlyTransferable implements Transferable {
#value: object
constructor(value: object) {
this.#value = value
}
get [kTransferable](): object {
return this.#value
}
get [kValue](): object {
return this.#value
}
}
class ArrayBufferViewTransferable implements Transferable {
#view: ArrayBufferView
constructor(view: ArrayBufferView) {
this.#view = view
}
get [kTransferable](): object {
return this.#view.buffer
}
get [kValue](): object {
return this.#view
}
}
let taskIdCounter = 0
type TaskCallback = (err: Error, result: any) => void
// Grab the type of `transferList` off `MessagePort`. At the time of writing,
// only ArrayBuffer and MessagePort are valid, but let's avoid having to update
// our types here every time Node.js adds support for more objects.
type TransferList = MessagePort extends {
postMessage(value: any, transferList: infer T): any
}
? T
: never
type TransferListItem = TransferList extends (infer T)[] ? T : never
function maybeFileURLToPath(filename: string): string {
return filename.startsWith('file:')
? fileURLToPath(new URL(filename))
: filename
}
// Extend AsyncResource so that async relations between posting a task and
// receiving its result are visible to diagnostic tools.
class TaskInfo extends AsyncResource implements Task {
callback: TaskCallback
task: any
transferList: TransferList
filename: string
name: string
taskId: number
abortSignal: AbortSignalAny | null
abortListener: (() => void) | null = null
workerInfo: WorkerInfo | null = null
created: number
started: number
constructor(
task: any,
transferList: TransferList,
filename: string,
name: string,
callback: TaskCallback,
abortSignal: AbortSignalAny | null,
triggerAsyncId: number
) {
super('Tinypool.Task', { requireManualDestroy: true, triggerAsyncId })
this.callback = callback
this.task = task
this.transferList = transferList
// If the task is a Transferable returned by
// Tinypool.move(), then add it to the transferList
// automatically
if (isMovable(task)) {
// This condition should never be hit but typescript
// complains if we dont do the check.
/* istanbul ignore if */
if (this.transferList == null) {
this.transferList = []
}
this.transferList = this.transferList.concat(task[kTransferable])
this.task = task[kValue]
}
this.filename = filename
this.name = name
this.taskId = taskIdCounter++
this.abortSignal = abortSignal
this.created = performance.now()
this.started = 0
}
releaseTask(): any {
const ret = this.task
this.task = null
return ret
}
done(err: unknown | null, result?: any): void {
this.emitDestroy() // `TaskInfo`s are used only once.
this.runInAsyncScope(this.callback, null, err, result)
// If an abort signal was used, remove the listener from it when
// done to make sure we do not accidentally leak.
if (this.abortSignal && this.abortListener) {
if ('removeEventListener' in this.abortSignal && this.abortListener) {
this.abortSignal.removeEventListener('abort', this.abortListener)
} else {
;(this.abortSignal as AbortSignalEventEmitter).off(
'abort',
this.abortListener
)
}
}
}
get [kQueueOptions](): object | null {
return kQueueOptions in this.task ? this.task[kQueueOptions] : null
}
}
abstract class AsynchronouslyCreatedResource {
onreadyListeners: (() => void)[] | null = []
markAsReady(): void {
const listeners = this.onreadyListeners
assert(listeners !== null)
this.onreadyListeners = null
for (const listener of listeners) {
listener()
}
}
isReady(): boolean {
return this.onreadyListeners === null
}
onReady(fn: () => void) {
if (this.onreadyListeners === null) {
fn() // Zalgo is okay here.
return
}
this.onreadyListeners.push(fn)
}
abstract currentUsage(): number
}
class AsynchronouslyCreatedResourcePool<
T extends AsynchronouslyCreatedResource
> {
pendingItems = new Set<T>()
readyItems = new Set<T>()
maximumUsage: number
onAvailableListeners: ((item: T) => void)[]
constructor(maximumUsage: number) {
this.maximumUsage = maximumUsage
this.onAvailableListeners = []
}
add(item: T) {
this.pendingItems.add(item)
item.onReady(() => {
/* istanbul ignore else */
if (this.pendingItems.has(item)) {
this.pendingItems.delete(item)
this.readyItems.add(item)
this.maybeAvailable(item)
}
})
}
delete(item: T) {
this.pendingItems.delete(item)
this.readyItems.delete(item)
}
findAvailable(): T | null {
let minUsage = this.maximumUsage
let candidate = null
for (const item of this.readyItems) {
const usage = item.currentUsage()
if (usage === 0) return item
if (usage < minUsage) {
candidate = item
minUsage = usage
}
}
return candidate
}
*[Symbol.iterator]() {
yield* this.pendingItems
yield* this.readyItems
}
get size() {
return this.pendingItems.size + this.readyItems.size
}
maybeAvailable(item: T) {
/* istanbul ignore else */
if (item.currentUsage() < this.maximumUsage) {
for (const listener of this.onAvailableListeners) {
listener(item)
}
}
}
onAvailable(fn: (item: T) => void) {
this.onAvailableListeners.push(fn)
}
}
type ResponseCallback = (response: ResponseMessage) => void
const Errors = {
ThreadTermination: () => new Error('Terminating worker thread'),
FilenameNotProvided: () =>
new Error('filename must be provided to run() or in options object'),
TaskQueueAtLimit: () => new Error('Task queue is at limit'),
NoTaskQueueAvailable: () =>
new Error('No task queue available and all Workers are busy'),
}
class WorkerInfo extends AsynchronouslyCreatedResource {
worker: Worker
workerId: number
freeWorkerId: () => void
taskInfos: Map<number, TaskInfo>
idleTimeout: NodeJS.Timeout | null = null // eslint-disable-line no-undef
port: MessagePort
sharedBuffer: Int32Array
lastSeenResponseCount: number = 0
onMessage: ResponseCallback
constructor(
worker: Worker,
port: MessagePort,
workerId: number,
freeWorkerId: () => void,
onMessage: ResponseCallback
) {
super()
this.worker = worker
this.workerId = workerId
this.freeWorkerId = freeWorkerId
this.port = port
this.port.on('message', (message: ResponseMessage) =>
this._handleResponse(message)
)
this.onMessage = onMessage
this.taskInfos = new Map()
this.sharedBuffer = new Int32Array(
new SharedArrayBuffer(kFieldCount * Int32Array.BYTES_PER_ELEMENT)
)
}
async destroy(): Promise<void> {
await this.worker.terminate()
this.port.close()
this.clearIdleTimeout()
for (const taskInfo of this.taskInfos.values()) {
taskInfo.done(Errors.ThreadTermination())
}
this.taskInfos.clear()
}
clearIdleTimeout(): void {
if (this.idleTimeout !== null) {
clearTimeout(this.idleTimeout)
this.idleTimeout = null
}
}
ref(): WorkerInfo {
this.port.ref()
return this
}
unref(): WorkerInfo {
// Note: Do not call ref()/unref() on the Worker itself since that may cause
// a hard crash, see https://github.com/nodejs/node/pull/33394.
this.port.unref()
return this
}
_handleResponse(message: ResponseMessage): void {
this.onMessage(message)
if (this.taskInfos.size === 0) {
// No more tasks running on this Worker means it should not keep the
// process running.
this.unref()
}
}
postTask(taskInfo: TaskInfo) {
assert(!this.taskInfos.has(taskInfo.taskId))
const message: RequestMessage = {
task: taskInfo.releaseTask(),
taskId: taskInfo.taskId,
filename: taskInfo.filename,
name: taskInfo.name,
}
try {
this.port.postMessage(message, taskInfo.transferList)
} catch (err) {
// This would mostly happen if e.g. message contains unserializable data
// or transferList is invalid.
taskInfo.done(err)
return
}
taskInfo.workerInfo = this
this.taskInfos.set(taskInfo.taskId, taskInfo)
this.ref()
this.clearIdleTimeout()
// Inform the worker that there are new messages posted, and wake it up
// if it is waiting for one.
Atomics.add(this.sharedBuffer, kRequestCountField, 1)
Atomics.notify(this.sharedBuffer, kRequestCountField, 1)
}
processPendingMessages() {
// If we *know* that there are more messages than we have received using
// 'message' events yet, then try to load and handle them synchronously,
// without the need to wait for more expensive events on the event loop.
// This would usually break async tracking, but in our case, we already have
// the extra TaskInfo/AsyncResource layer that rectifies that situation.
const actualResponseCount = Atomics.load(
this.sharedBuffer,
kResponseCountField
)
if (actualResponseCount !== this.lastSeenResponseCount) {
this.lastSeenResponseCount = actualResponseCount
let entry
while ((entry = receiveMessageOnPort(this.port)) !== undefined) {
this._handleResponse(entry.message)
}
}
}
isRunningAbortableTask(): boolean {
// If there are abortable tasks, we are running one at most per Worker.
if (this.taskInfos.size !== 1) return false
// @ts-ignore
const [[, task]] = this.taskInfos
return task.abortSignal !== null
}
currentUsage(): number {
if (this.isRunningAbortableTask()) return Infinity
return this.taskInfos.size
}
}
class ThreadPool {
publicInterface: Tinypool
workers: AsynchronouslyCreatedResourcePool<WorkerInfo>
workerIds: Map<number, boolean> // Map<workerId, isIdAvailable>
options: FilledOptions
taskQueue: TaskQueue
skipQueue: TaskInfo[] = []
completed: number = 0
start: number = performance.now()
inProcessPendingMessages: boolean = false
startingUp: boolean = false
workerFailsDuringBootstrap: boolean = false
constructor(publicInterface: Tinypool, options: Options) {
this.publicInterface = publicInterface
this.taskQueue = options.taskQueue || new ArrayTaskQueue()
const filename = options.filename
? maybeFileURLToPath(options.filename)
: null
this.options = { ...kDefaultOptions, ...options, filename, maxQueue: 0 }
// The >= and <= could be > and < but this way we get 100 % coverage 🙃
if (
options.maxThreads !== undefined &&
this.options.minThreads >= options.maxThreads
) {
this.options.minThreads = options.maxThreads
}
if (
options.minThreads !== undefined &&
this.options.maxThreads <= options.minThreads
) {
this.options.maxThreads = options.minThreads
}
if (options.maxQueue === 'auto') {
this.options.maxQueue = this.options.maxThreads ** 2
} else {
this.options.maxQueue = options.maxQueue ?? kDefaultOptions.maxQueue
}
this.workerIds = new Map(
new Array(this.options.maxThreads).fill(0).map((_, i) => [i + 1, true])
)
this.workers = new AsynchronouslyCreatedResourcePool<WorkerInfo>(
this.options.concurrentTasksPerWorker
)
this.workers.onAvailable((w: WorkerInfo) => this._onWorkerAvailable(w))
this.startingUp = true
this._ensureMinimumWorkers()
this.startingUp = false
}
_ensureEnoughWorkersForTaskQueue(): void {
while (
this.workers.size < this.taskQueue.size &&
this.workers.size < this.options.maxThreads
) {
this._addNewWorker()
}
}
_ensureMaximumWorkers(): void {
while (this.workers.size < this.options.maxThreads) {
this._addNewWorker()
}
}
_ensureMinimumWorkers(): void {
while (this.workers.size < this.options.minThreads) {
this._addNewWorker()
}
}
_addNewWorker(): void {
const pool = this
const workerIds = this.workerIds
const __dirname = dirname(fileURLToPath(import.meta.url))
let workerId: number
workerIds.forEach((isIdAvailable, _workerId) => {
if (isIdAvailable && !workerId) {
workerId = _workerId
workerIds.set(_workerId, false)
}
})
const tinypoolPrivateData = { workerId: workerId! }
const worker = new Worker(resolve(__dirname, './worker.js'), {
env: this.options.env,
argv: this.options.argv,
execArgv: this.options.execArgv,
resourceLimits: this.options.resourceLimits,
workerData: [
tinypoolPrivateData,
this.options.workerData,
] as TinypoolData,
trackUnmanagedFds: this.options.trackUnmanagedFds,
})
const onMessage = (message: ResponseMessage) => {
const { taskId, result } = message
// In case of success: Call the callback that was passed to `runTask`,
// remove the `TaskInfo` associated with the Worker, which marks it as
// free again.
const taskInfo = workerInfo.taskInfos.get(taskId)
workerInfo.taskInfos.delete(taskId)
if (!this.options.isolateWorkers) pool.workers.maybeAvailable(workerInfo)
/* istanbul ignore if */
if (taskInfo === undefined) {
const err = new Error(
`Unexpected message from Worker: ${inspect(message)}`
)
pool.publicInterface.emit('error', err)
} else {
taskInfo.done(message.error, result)
}
pool._processPendingMessages()
}
const { port1, port2 } = new MessageChannel()
const workerInfo = new WorkerInfo(
worker,
port1,
workerId!,
() => workerIds.set(workerId, true),
onMessage
)
if (this.startingUp) {
// There is no point in waiting for the initial set of Workers to indicate
// that they are ready, we just mark them as such from the start.
workerInfo.markAsReady()
}
const message: StartupMessage = {
filename: this.options.filename,
name: this.options.name,
port: port2,
sharedBuffer: workerInfo.sharedBuffer,
useAtomics: this.options.useAtomics,
}
worker.postMessage(message, [port2])
worker.on('message', (message: ReadyMessage) => {
if (message.ready === true) {
if (workerInfo.currentUsage() === 0) {
workerInfo.unref()
}
if (!workerInfo.isReady()) {
workerInfo.markAsReady()
}
return
}
worker.emit(
'error',
new Error(`Unexpected message on Worker: ${inspect(message)}`)
)
})
worker.on('error', (err: Error) => {
// Work around the bug in https://github.com/nodejs/node/pull/33394
worker.ref = () => {}
// In case of an uncaught exception: Call the callback that was passed to
// `postTask` with the error, or emit an 'error' event if there is none.
const taskInfos = [...workerInfo.taskInfos.values()]
workerInfo.taskInfos.clear()
// Remove the worker from the list and potentially start a new Worker to
// replace the current one.
this._removeWorker(workerInfo)
if (workerInfo.isReady() && !this.workerFailsDuringBootstrap) {
this._ensureMinimumWorkers()
} else {
// Do not start new workers over and over if they already fail during
// bootstrap, there's no point.
this.workerFailsDuringBootstrap = true
}
if (taskInfos.length > 0) {
for (const taskInfo of taskInfos) {
taskInfo.done(err, null)
}
} else {
this.publicInterface.emit('error', err)
}
})
worker.unref()
port1.on('close', () => {
// The port is only closed if the Worker stops for some reason, but we
// always .unref() the Worker itself. We want to receive e.g. 'error'
// events on it, so we ref it once we know it's going to exit anyway.
worker.ref()
})
this.workers.add(workerInfo)
}
_processPendingMessages() {
if (this.inProcessPendingMessages || !this.options.useAtomics) {
return
}
this.inProcessPendingMessages = true
try {
for (const workerInfo of this.workers) {
workerInfo.processPendingMessages()
}
} finally {
this.inProcessPendingMessages = false
}
}
_removeWorker(workerInfo: WorkerInfo): void {
workerInfo.freeWorkerId()
workerInfo.destroy()
this.workers.delete(workerInfo)
}
_onWorkerAvailable(workerInfo: WorkerInfo): void {
while (
(this.taskQueue.size > 0 || this.skipQueue.length > 0) &&
workerInfo.currentUsage() < this.options.concurrentTasksPerWorker
) {
// The skipQueue will have tasks that we previously shifted off
// the task queue but had to skip over... we have to make sure
// we drain that before we drain the taskQueue.
const taskInfo =
this.skipQueue.shift() || (this.taskQueue.shift() as TaskInfo)
// If the task has an abortSignal and the worker has any other
// tasks, we cannot distribute the task to it. Skip for now.
if (taskInfo.abortSignal && workerInfo.taskInfos.size > 0) {
this.skipQueue.push(taskInfo)
break
}
const now = performance.now()
taskInfo.started = now
workerInfo.postTask(taskInfo)
this._maybeDrain()
return
}
if (
workerInfo.taskInfos.size === 0 &&
this.workers.size > this.options.minThreads
) {
workerInfo.idleTimeout = setTimeout(() => {
assert.strictEqual(workerInfo.taskInfos.size, 0)
if (this.workers.size > this.options.minThreads) {
this._removeWorker(workerInfo)
}
}, this.options.idleTimeout).unref()
}
}
runTask(task: any, options: RunOptions): Promise<any> {
let { filename, name } = options
const { transferList = [], signal = null } = options
if (filename == null) {
filename = this.options.filename
}
if (name == null) {
name = this.options.name
}
if (typeof filename !== 'string') {
return Promise.reject(Errors.FilenameNotProvided())
}
filename = maybeFileURLToPath(filename)
let resolve: (result: any) => void
let reject: (err: Error) => void
// eslint-disable-next-line
const ret = new Promise((res, rej) => {
resolve = res
reject = rej
})
const taskInfo = new TaskInfo(
task,
transferList,
filename,
name,
(err: Error | null, result: any) => {
this.completed++
if (err !== null) {
reject(err)
} else {
resolve(result)
}
// When `isolateWorkers` is enabled, remove the worker after task is finished
if (this.options.isolateWorkers && taskInfo.workerInfo) {
this._removeWorker(taskInfo.workerInfo)
this._ensureEnoughWorkersForTaskQueue()
}
},
signal,
this.publicInterface.asyncResource.asyncId()
)
if (signal !== null) {
// If the AbortSignal has an aborted property and it's truthy,
// reject immediately.
if ((signal as AbortSignalEventTarget).aborted) {
return Promise.reject(new AbortError())
}
taskInfo.abortListener = () => {
// Call reject() first to make sure we always reject with the AbortError
// if the task is aborted, not with an Error from the possible
// thread termination below.
reject(new AbortError())
if (taskInfo.workerInfo !== null) {
// Already running: We cancel the Worker this is running on.
this._removeWorker(taskInfo.workerInfo)
this._ensureMinimumWorkers()
} else {
// Not yet running: Remove it from the queue.
this.taskQueue.remove(taskInfo)
}
}
onabort(signal, taskInfo.abortListener)
}
// If there is a task queue, there's no point in looking for an available
// Worker thread. Add this task to the queue, if possible.
if (this.taskQueue.size > 0) {
const totalCapacity = this.options.maxQueue + this.pendingCapacity()
if (this.taskQueue.size >= totalCapacity) {
if (this.options.maxQueue === 0) {
return Promise.reject(Errors.NoTaskQueueAvailable())
} else {
return Promise.reject(Errors.TaskQueueAtLimit())
}
} else {
if (this.workers.size < this.options.maxThreads) {
this._addNewWorker()
}
this.taskQueue.push(taskInfo)
}
return ret
}
// Look for a Worker with a minimum number of tasks it is currently running.
let workerInfo: WorkerInfo | null = this.workers.findAvailable()
// If we want the ability to abort this task, use only workers that have
// no running tasks.
if (workerInfo !== null && workerInfo.currentUsage() > 0 && signal) {
workerInfo = null
}
// If no Worker was found, or that Worker was handling another task in some
// way, and we still have the ability to spawn new threads, do so.
let waitingForNewWorker = false
if (
(workerInfo === null || workerInfo.currentUsage() > 0) &&
this.workers.size < this.options.maxThreads
) {
this._addNewWorker()
waitingForNewWorker = true
}
// If no Worker is found, try to put the task into the queue.
if (workerInfo === null) {
if (this.options.maxQueue <= 0 && !waitingForNewWorker) {
return Promise.reject(Errors.NoTaskQueueAvailable())
} else {
this.taskQueue.push(taskInfo)
}
return ret
}
const now = performance.now()
taskInfo.started = now
workerInfo.postTask(taskInfo)
this._maybeDrain()
return ret
}
pendingCapacity(): number {
return (
this.workers.pendingItems.size * this.options.concurrentTasksPerWorker
)
}
_maybeDrain() {
if (this.taskQueue.size === 0 && this.skipQueue.length === 0) {
this.publicInterface.emit('drain')
}
}
async destroy() {
while (this.skipQueue.length > 0) {
const taskInfo: TaskInfo = this.skipQueue.shift() as TaskInfo
taskInfo.done(new Error('Terminating worker thread'))
}
while (this.taskQueue.size > 0) {
const taskInfo: TaskInfo = this.taskQueue.shift() as TaskInfo
taskInfo.done(new Error('Terminating worker thread'))
}
const exitEvents: Promise<any[]>[] = []
while (this.workers.size > 0) {
const [workerInfo] = this.workers
// @ts-ignore
exitEvents.push(once(workerInfo.worker, 'exit'))
// @ts-ignore
this._removeWorker(workerInfo)
}
await Promise.all(exitEvents)
}
}
class Tinypool extends EventEmitterAsyncResource {
#pool: ThreadPool
constructor(options: Options = {}) {
// convert fractional option values to int
if (
options.minThreads !== undefined &&
options.minThreads > 0 &&
options.minThreads < 1
) {
options.minThreads = Math.max(
1,
Math.floor(options.minThreads * cpuCount)
)
}
if (
options.maxThreads !== undefined &&
options.maxThreads > 0 &&
options.maxThreads < 1
) {
options.maxThreads = Math.max(