@nxtedition/scheduler 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +24 -25
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -52,7 +52,7 @@ export function parsePriority(p ) {
|
|
|
52
52
|
p = Number(p)
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
if (typeof p !== 'number') {
|
|
55
|
+
if (typeof p !== 'number' || Number.isNaN(p)) {
|
|
56
56
|
return 0
|
|
57
57
|
} else if (p < -3) {
|
|
58
58
|
return -3
|
|
@@ -156,7 +156,7 @@ export class Scheduler {
|
|
|
156
156
|
fn(opaque)
|
|
157
157
|
return
|
|
158
158
|
}
|
|
159
|
-
} else if (this.#running < 1 || this.#running < this.#concurrency) {
|
|
159
|
+
} else if ((this.#running < 1 && this.#concurrency > 0) || this.#running < this.#concurrency) {
|
|
160
160
|
this.#running += 1
|
|
161
161
|
fn(opaque)
|
|
162
162
|
return
|
|
@@ -185,31 +185,36 @@ export class Scheduler {
|
|
|
185
185
|
|
|
186
186
|
try {
|
|
187
187
|
this.#releasing = true
|
|
188
|
-
while (this.#pending > 0) {
|
|
188
|
+
while (this.#pending > 0 && (this.#running === 0 || running < this.#concurrency)) {
|
|
189
189
|
let queue = null
|
|
190
190
|
|
|
191
|
-
|
|
191
|
+
let idx = this.#queues.length - 1
|
|
192
192
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
193
|
+
// Avoid starvation by randomizing the starting queue.
|
|
194
|
+
{
|
|
195
|
+
this.#counter = (this.#counter + 1) & maxInt
|
|
196
|
+
if (this.#counter & 0b0000001) {
|
|
197
|
+
idx = 6 // highest: 50%
|
|
198
|
+
} else if (this.#counter & 0b0000010) {
|
|
199
|
+
idx = 5 // higher: 25%
|
|
200
|
+
} else if (this.#counter & 0b0000100) {
|
|
201
|
+
idx = 4 // high: 12.5%
|
|
202
|
+
} else if (this.#counter & 0b0001000) {
|
|
203
|
+
idx = 3 // normal: 6.25%
|
|
204
|
+
} else if (this.#counter & 0b0010000) {
|
|
205
|
+
idx = 2 // low: 3.125%
|
|
206
|
+
} else if (this.#counter & 0b0100000) {
|
|
207
|
+
idx = 1 // lower: 1.5625%
|
|
208
|
+
} else if (this.#counter & 0b1000000) {
|
|
209
|
+
idx = 0 // lowest: 0.78%
|
|
210
|
+
}
|
|
206
211
|
}
|
|
207
212
|
|
|
208
213
|
for (let n = idx; n >= 0 && (queue == null || queue.cnt === 0); n--) {
|
|
209
214
|
queue = this.#queues[n]
|
|
210
215
|
}
|
|
211
216
|
|
|
212
|
-
for (let n =
|
|
217
|
+
for (let n = this.#queues.length - 1; n > idx && (queue == null || queue.cnt === 0); n--) {
|
|
213
218
|
queue = this.#queues[n]
|
|
214
219
|
}
|
|
215
220
|
|
|
@@ -217,12 +222,6 @@ export class Scheduler {
|
|
|
217
222
|
throw new Error('Invariant violation: pending > 0 but no tasks in queues')
|
|
218
223
|
}
|
|
219
224
|
|
|
220
|
-
if (this.#running > 0 && running >= this.#concurrency) {
|
|
221
|
-
break
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
this.#counter = (this.#counter + 1) & maxInt
|
|
225
|
-
|
|
226
225
|
const fn = queue.arr[queue.idx++]
|
|
227
226
|
const opaque = queue.arr[queue.idx++]
|
|
228
227
|
queue.cnt -= 1
|
|
@@ -232,8 +231,8 @@ export class Scheduler {
|
|
|
232
231
|
queue.idx = 0
|
|
233
232
|
queue.arr.length = 0
|
|
234
233
|
} else if (queue.idx > 1024) {
|
|
235
|
-
queue.arr.splice(0, queue.idx)
|
|
236
234
|
queue.idx = 0
|
|
235
|
+
queue.arr.splice(0, queue.idx)
|
|
237
236
|
}
|
|
238
237
|
|
|
239
238
|
running = this.#stateView
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/scheduler",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -20,5 +20,5 @@
|
|
|
20
20
|
"rimraf": "^6.1.2",
|
|
21
21
|
"typescript": "^5.9.3"
|
|
22
22
|
},
|
|
23
|
-
"gitHead": "
|
|
23
|
+
"gitHead": "c88bafd0c4a7e250d9e4080d9f50a0a0a1a562ac"
|
|
24
24
|
}
|