Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Penumbra worker init reliability #233

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ const onReady = async (
},
)),
);

writer.write(
...(await penumbra.get(
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@transcend-io/penumbra",
"version": "5.3.2",
"version": "5.3.3",
"description": "Crypto streams for the browser.",
"main": "dist/main.penumbra.js",
"types": "ts-build/src/index.d.ts",
Expand Down
19 changes: 6 additions & 13 deletions src/workers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ const onWorkerInitQueue: (() => void)[] = [];
* @returns Worker
*/
function getFreeWorker(): PenumbraWorker {
// Poll for any available free workers
if (workers.length === 0) {
throw new Error('No Penumbra workers available.');
}

// Poll for any available free workers or create a new one
eligrey marked this conversation as resolved.
Show resolved Hide resolved
const freeWorker = workers.find(({ busy }) => !busy);

// return any free worker or a random one if all are busy
Expand Down Expand Up @@ -233,21 +237,11 @@ export async function getWorker(): Promise<PenumbraWorker> {
}
return getFreeWorker();
}

/**
* Returns all active Penumbra Workers
*
* @returns Workers
*/
function getActiveWorkers(): PenumbraWorker[] {
return workers;
}

/**
* Terminate Penumbra worker and de-allocate their resources
*/
function cleanup(): void {
getActiveWorkers().forEach((thread) => {
workers.forEach((thread) => {
thread.worker.terminate();
});
workers.length = 0;
Expand Down Expand Up @@ -285,7 +279,6 @@ export async function setWorkerLocation(
if (initialized) {
logger.warn('Penumbra Workers are already active. Reinitializing...');
await cleanup();
return;
}
settings.workers = JSON.stringify(
typeof options === 'string'
Expand Down