-
Notifications
You must be signed in to change notification settings - Fork 97
feat: introduce WORKERS_POOL_ENABLED config to optionally disable worker thread pool #5070
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
Merged
quiet-node
merged 1 commit into
main
from
4987-enhance-resource-efficiency-optionally-disable-worker-thread-pool-for-low-memory-profiles
Mar 12, 2026
+637
−160
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
103 changes: 103 additions & 0 deletions
103
packages/relay/src/lib/services/workersService/WorkersErrorUtils.ts
This file contains hidden or 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,103 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { parentPort } from 'worker_threads'; | ||
|
|
||
| import { JsonRpcError, predefined } from '../../errors/JsonRpcError'; | ||
| import { MirrorNodeClientError } from '../../errors/MirrorNodeClientError'; | ||
|
|
||
| /** | ||
| * Plain JSON representation of a serializable error that can be safely transported | ||
| * across worker thread boundaries using the Structured Clone algorithm. | ||
| * | ||
| * Piscina communicates task results and errors between threads via `postMessage`, which | ||
| * uses Structured Clone. Only plain data survives this boundary — class instances, prototype | ||
| * chains, and private fields are stripped. This envelope captures the fields needed to | ||
| * reconstruct supported error types on the receiving thread. | ||
| */ | ||
| interface ErrorEnvelope { | ||
| name: string; | ||
| message: string; | ||
| code?: number; | ||
| statusCode?: number; | ||
| data?: string; | ||
| detail?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Conditionally serializes an error for cross-thread transport. | ||
| * | ||
| * When invoked inside a Piscina worker thread (`parentPort` is non-null), serializes the | ||
| * error into a standard {@link Error} whose `message` contains the JSON-encoded payload. | ||
| * This ensures that rich error types (e.g. {@link JsonRpcError}, {@link MirrorNodeClientError}) | ||
| * survive the Structured Clone boundary used by `postMessage`, which otherwise strips | ||
| * prototype chains and class-specific fields. | ||
| * | ||
| * When invoked on the main thread (`parentPort` is null), returns the original error | ||
| * unchanged. This avoids the CPU/memory overhead of unnecessary serialization-deserialization | ||
| * cycles during local execution on the main thread (e.g. when WORKERS_POOL_ENABLED is false) | ||
| * and preserves the original object's identity. | ||
| * | ||
| * @param err - The error-like value to wrap if in a worker context. | ||
| * @returns The original error when called outside a worker, or a JSON-encoded `Error` inside one. | ||
| */ | ||
| export function wrapError(err: unknown): unknown { | ||
| if (!parentPort) { | ||
| return err; | ||
| } | ||
| return new Error(JSON.stringify(err)); | ||
| } | ||
|
|
||
| /** | ||
| * Reconstructs the original typed error from an error previously produced by {@link wrapError}. | ||
| * | ||
| * Parses the JSON payload embedded in `err.message` and attempts to reconstruct one of the | ||
| * supported rich error types. Returns a predefined internal error if the input is not an | ||
| * `Error`, the message is not valid JSON, or the error name is unrecognised. | ||
| * | ||
| * Supported error types: | ||
| * - {@link JsonRpcError} | ||
| * - {@link MirrorNodeClientError} | ||
| * | ||
| * @param err - An error whose `message` is expected to contain a JSON-encoded {@link ErrorEnvelope}. | ||
| * @returns The reconstructed typed error, or a {@link predefined.INTERNAL_ERROR} if the | ||
| * envelope cannot be parsed or the type is unsupported. | ||
| */ | ||
| export function unwrapError(err: unknown): Error { | ||
| if (!(err instanceof Error)) { | ||
| return predefined.INTERNAL_ERROR('Failed unwrapping piscina error: value is not an Error instance.'); | ||
| } | ||
|
|
||
| let parsedErr: ErrorEnvelope; | ||
| try { | ||
| parsedErr = JSON.parse(err.message) as ErrorEnvelope; | ||
| } catch { | ||
| return predefined.INTERNAL_ERROR('Failed parsing wrapped piscina error while unwrapping.'); | ||
| } | ||
|
|
||
| switch (parsedErr?.name) { | ||
| case JsonRpcError.name: { | ||
| if (typeof parsedErr.code !== 'number') { | ||
| return predefined.INTERNAL_ERROR( | ||
| 'Failed unwrapping piscina error: missing numeric code in JsonRpcError envelope.', | ||
| ); | ||
| } | ||
| return new JsonRpcError({ | ||
| code: parsedErr.code, | ||
| data: parsedErr.data, | ||
| message: parsedErr.message, | ||
| }); | ||
| } | ||
|
|
||
| case MirrorNodeClientError.name: { | ||
| if (typeof parsedErr.statusCode !== 'number') { | ||
| return predefined.INTERNAL_ERROR( | ||
| 'Failed unwrapping piscina error: missing numeric statusCode in MirrorNodeClientError envelope.', | ||
| ); | ||
| } | ||
| return MirrorNodeClientError.fromJSON(parsedErr.statusCode, parsedErr.message, parsedErr.data, parsedErr.detail); | ||
| } | ||
|
|
||
| default: | ||
| return predefined.INTERNAL_ERROR('Failed unwrapping piscina error.'); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.