-
-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reusable thread pool to decrypt keystores
- Loading branch information
Showing
11 changed files
with
175 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
packages/cli/src/cmds/validator/keymanager/decryptKeystoreDefinitions/types.ts
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
packages/cli/src/cmds/validator/keymanager/decryptKeystores/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {DecryptKeystoresThreadPool} from "./threadPool.js"; |
File renamed without changes.
67 changes: 67 additions & 0 deletions
67
packages/cli/src/cmds/validator/keymanager/decryptKeystores/threadPool.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {spawn, Pool, Worker, ModuleThread, QueuedTask} from "@chainsafe/threads"; | ||
import {DecryptKeystoreArgs, DecryptKeystoreWorkerAPI} from "./types.js"; | ||
import {maxPoolSize} from "./poolSize.js"; | ||
|
||
/** | ||
* Thread pool to decrypt keystores | ||
*/ | ||
export class DecryptKeystoresThreadPool { | ||
private pool: Pool<ModuleThread<DecryptKeystoreWorkerAPI>>; | ||
private tasks: QueuedTask<ModuleThread<DecryptKeystoreWorkerAPI>, Uint8Array>[] = []; | ||
private terminatePoolHandler: () => void; | ||
|
||
constructor(keystoreCount: number, private readonly signal: AbortSignal) { | ||
this.pool = Pool( | ||
() => | ||
spawn<DecryptKeystoreWorkerAPI>(new Worker("./worker.js"), { | ||
// The number below is big enough to almost disable the timeout | ||
// which helps during tests run on unpredictably slow hosts | ||
timeout: 5 * 60 * 1000, | ||
}), | ||
{ | ||
// Adjust worker pool size based on keystore count | ||
size: Math.min(keystoreCount, maxPoolSize), | ||
// Decrypt keystores in sequence, increasing concurrency does not improve performance | ||
concurrency: 1, | ||
} | ||
); | ||
// Terminate worker threads when process receives exit signal | ||
this.terminatePoolHandler = () => { | ||
void this.pool.terminate(true); | ||
}; | ||
signal.addEventListener("abort", this.terminatePoolHandler); | ||
} | ||
|
||
/** | ||
* Add keystore to the task queue to be decrypted | ||
*/ | ||
queue( | ||
args: DecryptKeystoreArgs, | ||
onDecrypted: (secretKeyBytes: Uint8Array) => void, | ||
onError: (e: Error) => void | ||
): void { | ||
const task = this.pool.queue((thread) => thread.decryptKeystore(args)); | ||
this.tasks.push(task); | ||
task.then(onDecrypted).catch(onError); | ||
} | ||
|
||
/** | ||
* Resolves once all queued tasks are completed and terminates worker threads. | ||
* Errors during executing can be captured in `onError` handler for each task. | ||
*/ | ||
async completed(): Promise<void> { | ||
await this.pool.settled(true); | ||
await this.pool.terminate(); | ||
this.signal.removeEventListener("abort", this.terminatePoolHandler); | ||
} | ||
|
||
/** | ||
* Cancel all pending tasks | ||
*/ | ||
cancel(): void { | ||
for (const task of this.tasks) { | ||
task.cancel(); | ||
} | ||
this.tasks = []; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/cli/src/cmds/validator/keymanager/decryptKeystores/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {KeystoreStr} from "@lodestar/api/keymanager"; | ||
import {LocalKeystoreDefinition} from "../interface.js"; | ||
|
||
export type DecryptKeystoreWorkerAPI = { | ||
decryptKeystore(args: DecryptKeystoreArgs): Promise<Uint8Array>; | ||
}; | ||
|
||
export type DecryptKeystoreArgs = LocalKeystoreDefinition | {keystoreStr: KeystoreStr; password: string}; | ||
|
||
export function isLocalKeystoreDefinition(args: DecryptKeystoreArgs): args is LocalKeystoreDefinition { | ||
return (args as LocalKeystoreDefinition).keystorePath !== undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters