mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
fixed
This commit is contained in:
1 parent
d53cfd88d6
commit
ba225c914a
1 file changed
+24
-8
+24
-8
@@ -93,10 +93,11 @@ function isIgnorableMediaStateError(err) {
|
|||||||
// ═══════════════════════════════════════════════════════════════════════════════
|
// ═══════════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
class PlayerWorkerPool {
|
class PlayerWorkerPool {
|
||||||
constructor(size, workerPath) {
|
constructor(size, workerPath, nlConfig = {}) {
|
||||||
this._size = size;
|
this._size = size;
|
||||||
this._workerPath = workerPath;
|
this._workerPath = workerPath;
|
||||||
this._workers = []; // { worker, busy }
|
this._nlConfig = nlConfig;
|
||||||
|
this._workers = []; // { worker, busy, _currentJobKey }
|
||||||
this._queue = []; // { jobKey, msg, resolve, reject, onMessage }
|
this._queue = []; // { jobKey, msg, resolve, reject, onMessage }
|
||||||
this._pending = new Map(); // jobKey → { resolve, reject, onMessage }
|
this._pending = new Map(); // jobKey → { resolve, reject, onMessage }
|
||||||
this._jobCounter = 0;
|
this._jobCounter = 0;
|
||||||
@@ -106,9 +107,14 @@ class PlayerWorkerPool {
|
|||||||
|
|
||||||
_spawn() {
|
_spawn() {
|
||||||
const worker = new Worker(this._workerPath, {
|
const worker = new Worker(this._workerPath, {
|
||||||
workerData: { poolMode: true }
|
workerData: {
|
||||||
|
poolMode: true,
|
||||||
|
// Pass NodeLink config so the module-level nlGet fallback
|
||||||
|
// has the correct host/port/password if _nlGet is ever null.
|
||||||
|
data: { nodelink: this._nlConfig ?? {} }
|
||||||
|
}
|
||||||
});
|
});
|
||||||
const entry = { worker, busy: false };
|
const entry = { worker, busy: false, _currentJobKey: null };
|
||||||
|
|
||||||
worker.on("message", (raw) => {
|
worker.on("message", (raw) => {
|
||||||
try {
|
try {
|
||||||
@@ -121,11 +127,13 @@ class PlayerWorkerPool {
|
|||||||
} else if (event === "finished") {
|
} else if (event === "finished") {
|
||||||
this._pending.delete(jobKey);
|
this._pending.delete(jobKey);
|
||||||
entry.busy = false;
|
entry.busy = false;
|
||||||
|
entry._currentJobKey = null;
|
||||||
cb.resolve(data);
|
cb.resolve(data);
|
||||||
this._drain();
|
this._drain();
|
||||||
} else if (event === "error") {
|
} else if (event === "error") {
|
||||||
this._pending.delete(jobKey);
|
this._pending.delete(jobKey);
|
||||||
entry.busy = false;
|
entry.busy = false;
|
||||||
|
entry._currentJobKey = null;
|
||||||
cb.reject(new Error(String(data)));
|
cb.reject(new Error(String(data)));
|
||||||
this._drain();
|
this._drain();
|
||||||
}
|
}
|
||||||
@@ -133,15 +141,22 @@ class PlayerWorkerPool {
|
|||||||
});
|
});
|
||||||
|
|
||||||
worker.on("error", (err) => {
|
worker.on("error", (err) => {
|
||||||
// Release any job currently assigned to this worker
|
// Release only the job currently assigned to THIS worker.
|
||||||
for (const [key, cb] of this._pending) {
|
// Iterating all _pending would incorrectly reject in-flight jobs
|
||||||
|
// from other workers, leaving those workers stuck busy=true forever.
|
||||||
|
if (entry._currentJobKey != null) {
|
||||||
|
const cb = this._pending.get(entry._currentJobKey);
|
||||||
|
if (cb) {
|
||||||
|
this._pending.delete(entry._currentJobKey);
|
||||||
cb.reject(err);
|
cb.reject(err);
|
||||||
this._pending.delete(key);
|
}
|
||||||
|
entry._currentJobKey = null;
|
||||||
}
|
}
|
||||||
entry.busy = false;
|
entry.busy = false;
|
||||||
// Replace dead worker
|
// Replace dead worker
|
||||||
this._workers.splice(this._workers.indexOf(entry), 1);
|
this._workers.splice(this._workers.indexOf(entry), 1);
|
||||||
this._spawn();
|
this._spawn();
|
||||||
|
this._drain();
|
||||||
});
|
});
|
||||||
|
|
||||||
worker.on("exit", (code) => {
|
worker.on("exit", (code) => {
|
||||||
@@ -162,6 +177,7 @@ class PlayerWorkerPool {
|
|||||||
|
|
||||||
_dispatch(entry, { jobKey, msg, resolve, reject, onMessage }) {
|
_dispatch(entry, { jobKey, msg, resolve, reject, onMessage }) {
|
||||||
entry.busy = true;
|
entry.busy = true;
|
||||||
|
entry._currentJobKey = jobKey;
|
||||||
this._pending.set(jobKey, { resolve, reject, onMessage });
|
this._pending.set(jobKey, { resolve, reject, onMessage });
|
||||||
entry.worker.postMessage(JSON.stringify(msg));
|
entry.worker.postMessage(JSON.stringify(msg));
|
||||||
}
|
}
|
||||||
@@ -1868,7 +1884,7 @@ export default class Player extends EventEmitter {
|
|||||||
_getWorkerPool() {
|
_getWorkerPool() {
|
||||||
if (!this._workerPool) {
|
if (!this._workerPool) {
|
||||||
const workerPath = new URL("./worker.mjs", import.meta.url);
|
const workerPath = new URL("./worker.mjs", import.meta.url);
|
||||||
this._workerPool = new PlayerWorkerPool(2, workerPath);
|
this._workerPool = new PlayerWorkerPool(2, workerPath, this._nl);
|
||||||
}
|
}
|
||||||
return this._workerPool;
|
return this._workerPool;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user