Skip to content

feat: Introduce MultiIsolateRelicServer - #216

Merged
nielsenko merged 14 commits into
serverpod:mainfrom
nielsenko:multi-isolate-relic-server
Oct 28, 2025
Merged

nielsenko merged 14 commits into
serverpod:mainfrom
nielsenko:multi-isolate-relic-server

Conversation

@nielsenko

@nielsenko nielsenko commented Oct 13, 2025 •

Copy link
Copy Markdown
Collaborator

Description

  • Implements multi-isolate support for RelicServer to enable concurrent request handling across multiple CPU cores.
  • Refactors RelicServer into a sealed interface with the existing implementation moved to _RelicServer.
  • Adds IsolatedObject<T>, a generic wrapper that spawns an object in a separate isolate and communicates via message passing.
  • Introduces _IsolatedRelicServer and _MultiIsolateRelicServer implementations of RelicServer.
  • Adds noOfIsolates optional named parameter to RelicServer constructor (which is now a factory, delegating to either _RelicServer or _MultiIsolateRelicServer), and RelicApp.run.

Related Issues

  • Fixes: #?

Pre-Launch Checklist

Please ensure that your PR meets the following requirements before submitting:

  • This update focuses on a single feature or bug fix. (For multiple fixes, please submit separate PRs.)
  • I have read and followed the Dart Style Guide and formatted the code using dart format.
  • I have referenced at least one issue this PR fixes or is related to.
  • I have updated/added relevant documentation (doc comments with ///), ensuring consistency with existing project documentation.
  • I have added new tests to verify the changes.
  • All existing and new tests pass successfully.
  • I have documented any breaking changes below.

Breaking Changes

  • Includes breaking changes.
  • No breaking changes.

Additional Notes

The IsolatedObject implementation handles error propagation via RemoteError and proper cleanup of ports and subscriptions.

Summary by CodeRabbit

  • New Features

    • Multi-isolate server support (configurable count), server exposes port plus mount/start and close lifecycle methods.
    • New IsolatedObject utility to run/evaluate objects in separate isolates.
    • Stream asyncListen helper added.
  • Breaking Changes

    • Server constructor now accepts a lazy adapter factory (adapter created on demand) instead of a pre-created adapter instance.
    • Adapter now exposes a public port getter.
  • Tests

    • Added extensive tests for IsolatedObject behavior and lifecycle.
  • Docs

    • Minor wording correction in history.

@coderabbitai

coderabbitai Bot commented Oct 13, 2025 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Walkthrough

Introduces lazy IOAdapter initialization via an adapter factory, adds Adapter.port, adds IsolatedObject for cross‑isolate RPC and multi‑isolate RelicServer implementations, and updates examples and tests to use the new factory and multi‑isolate patterns.

Changes

Cohort / File(s) Summary
Adapter interface enhancement
lib/src/adapter/adapter.dart, lib/src/adapter/io/io_adapter.dart
Added int get port to the Adapter interface; IOAdapter implements the getter (now annotated with @override).
Isolated object RPC primitive
lib/src/isolated_object.dart
New Factory<T> typedef and IsolatedObject<T> providing spawn/connect, id‑based request/response RPC, evaluate(), evaluateVoid(), and close().
RelicServer refactor & multi‑isolate support
lib/src/relic_server.dart, lib/src/router/relic_app.dart
Reworked RelicServer into a sealed API with factory constructor taking adapterFactory and noOfIsolates; added mountAndStart(Handler), close(), and port getter; private implementations _RelicServer, _IsolatedRelicServer, _MultiIsolateRelicServer.
Stream utility
lib/src/util/util.dart
Added StreamEx<T>.asyncListen extension to serialize async onData handlers via asyncMap(...).listen(...).
Examples updated for lazy adapter
example/as_handle.dart, example/pipeline.dart
Replaced eager IOAdapter creation with zero‑argument factory usage: RelicServer(() => IOAdapter.bind(...)).
Tests & test utilities updated
test/...
test/headers/headers_test_utils.dart, test/relic_server_test.dart, test/router/relic_app_test.dart, test/util/test_util.dart, test/isolated_object/*
Tests updated to use adapter factory pattern; header test moves header capture to inter‑isolate ReceivePort/SendPort and sets noOfIsolates: 2; _FakeAdapter gains a port field; new isolated_object tests added.
Docs / minor
doc/history.md
Typo fix: "Shelf adaptor" -> "Shelf adapter".
Imports
lib/src/router/router.dart
Added import '../util/util.dart'; (for StreamEx).

Sequence Diagram(s)

sequenceDiagram
    participant Caller as Caller/Test
    participant Server as RelicServer
    participant Factory as AdapterFactory
    participant Adapter as IOAdapter
    participant Isolated as IsolatedObject
    participant Child as ChildIsolate

    rect #DCEFFD
    Note over Caller,Server: Old (Eager)
    Caller->>Adapter: IOAdapter.bind()
    Adapter-->>Server: adapter instance
    Caller->>Server: RelicServer(adapter)
    end

    rect #DFFFD9
    Note over Caller,Server: New (Lazy)
    Caller->>Server: RelicServer(() => IOAdapter.bind())
    Server->>Server: store adapterFactory
    Caller->>Server: mountAndStart(handler)
    Server->>Factory: call adapterFactory()
    Factory-->>Adapter: bind -> adapter + port
    Adapter-->>Server: adapter ready
    Server->>Server: start serving
    end

    rect #FFEFD9
    Note over Server,Isolated: Multi‑isolate (noOfIsolates > 1)
    Server->>Isolated: IsolatedObject(adapterFactory)
    Isolated->>Child: spawn isolate & handshake
    Child->>Factory: call adapterFactory()
    Factory-->>Adapter: bind -> adapter + port
    Child-->>Isolated: send port & ready
    Isolated-->>Server: propagate port
    Server->>Isolated: evaluate(fn)
    Isolated->>Child: dispatch {id, fn}
    Child->>Child: execute fn(isolatedObject)
    Child-->>Isolated: respond {id, result}
    Isolated-->>Server: deliver result
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The PR description provides a solid overview of the changes and most template sections are addressed, including a detailed Description section, completed Pre-Launch Checklist items, and Additional Notes. However, the Related Issues section is incomplete—it shows "Fixes: #?" as a placeholder with no actual issue number provided. Additionally, while the Breaking Changes section correctly indicates that breaking changes exist (checkbox is marked), it does not outline what those breaking changes are, which the template explicitly requires ("clearly outline them here to ensure they are included in the release notes"). The raw_summary confirms significant breaking changes (e.g., RelicServer constructor signature, RelicApp.run signature), but these are not documented in the PR description. This represents incomplete coverage of two required template sections. Please complete the Related Issues section by providing an actual issue number (e.g., "Fixes: #123") rather than leaving the placeholder "Fixes: #?". Additionally, expand the Breaking Changes section to explicitly list and describe each breaking change introduced by this PR, such as the RelicServer constructor signature change (from accepting IOAdapter to accepting a factory function) and the RelicApp.run signature change (addition of noOfIsolates parameter), to ensure these are properly communicated in release notes.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The PR title "feat: Introduce MultiIsolateRelicServer" clearly relates to a significant aspect of the changeset. The raw_summary confirms that _MultiIsolateRelicServer is a core new class introduced as part of multi-isolate support for RelicServer. While the title focuses on one implementation detail rather than the broader feature (multi-isolate support), it meaningfully communicates what's being added and is specific enough for a teammate scanning history to understand a primary architectural change. The reference to "MultiIsolateRelicServer" (the public-facing concept, with the actual class being _MultiIsolateRelicServer prefixed for privacy) is reasonable shorthand and aligns with common PR naming conventions.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Oct 13, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.56522% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.33%. Comparing base (75c4d55) to head (237d20d).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/src/isolated_object.dart 94.82% 3 Missing ⚠️
lib/src/relic_server.dart 96.55% 1 Missing ⚠️
lib/src/router/relic_app.dart 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #216      +/-   ##
==========================================
+ Coverage   91.23%   91.33%   +0.10%     
==========================================
  Files          88       89       +1     
  Lines        3343     3428      +85     
  Branches     1720     1763      +43     
==========================================
+ Hits         3050     3131      +81     
- Misses        293      297       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nielsenko
nielsenko force-pushed the multi-isolate-relic-server branch 15 times, most recently from 5949364 to d5f5a42 Compare October 15, 2025 19:49
@nielsenko nielsenko self-assigned this Oct 16, 2025
@nielsenko
nielsenko force-pushed the multi-isolate-relic-server branch 12 times, most recently from 41036e3 to e1038a8 Compare October 16, 2025 11:22
@nielsenko
nielsenko force-pushed the multi-isolate-relic-server branch 2 times, most recently from 2ac3ae2 to ced8324 Compare October 24, 2025 07:46
@nielsenko

Copy link
Copy Markdown
Collaborator Author

@SandPod Separated upgrade to Dart 3.7 into separate PR #239. This will likely introduce the flaky coverage gathering issue again, but keep this PR clean.

@nielsenko
nielsenko force-pushed the multi-isolate-relic-server branch from ced8324 to 88b228e Compare October 27, 2025 10:51
@nielsenko

nielsenko commented Oct 27, 2025 •

Copy link
Copy Markdown
Collaborator Author

Rebased on main after #235, #238, and #241 landed

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@nielsenko
nielsenko force-pushed the multi-isolate-relic-server branch 2 times, most recently from 5d4d3b0 to 229f794 Compare October 28, 2025 07:44
@nielsenko nielsenko mentioned this pull request Oct 28, 2025
6 of 9 tasks
@nielsenko
nielsenko force-pushed the multi-isolate-relic-server branch 2 times, most recently from 08dd9d0 to 62e0521 Compare October 28, 2025 11:05
@nielsenko

Copy link
Copy Markdown
Collaborator Author

Rebased

@nielsenko
nielsenko force-pushed the multi-isolate-relic-server branch from 62e0521 to 237d20d Compare October 28, 2025 11:13
@nielsenko
nielsenko merged commit 3ab7119 into serverpod:main Oct 28, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Dec 1, 2025
8 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants