Skip to content

Migrate worker server behaviour to libp2p-stream NET-924#211

Merged
kalabukdima merged 2 commits into
mainfrom
net-stream-server-worker
Jul 16, 2026
Merged

Migrate worker server behaviour to libp2p-stream NET-924#211
kalabukdima merged 2 commits into
mainfrom
net-stream-server-worker

Conversation

@kalabukdima

Copy link
Copy Markdown
Collaborator

Replace the worker's request_response server with a new stream_server behaviour built on the forked libp2p-stream.
Responses can now be written directly from the per-stream handler task instead of syncing on the swarm.

  • Add stream_server behaviour.
  • Keep the wire protocol unchanged so old portals interoperate with the new worker.
  • Decode requests in the worker actor; drop the response queues and make send_* async, writing straight to the stream.
  • Bump the libp2p fork rev for Control::accept_with_capacity, which buffers a burst of inbound streams instead of dropping them.
  • The portal logs collector keeps request_server for now.

@kalabukdima
kalabukdima requested a review from mo4islona July 6, 2026 12:30
@kalabukdima kalabukdima self-assigned this Jul 6, 2026
@kalabukdima kalabukdima changed the title Migrate worker server behaviour to libp2p-stream Migrate worker server behaviour to libp2p-stream NET-924 Jul 6, 2026
Replace the worker's request_response server with a new stream_server
behaviour built on raw libp2p-stream, mirroring the already-migrated
stream_client. Responses are now written directly from the per-stream
handler task instead of round-tripping through the swarm loop, so the
response queues, ResponseChannel plumbing, and per-result select arms are
gone. The wire protocol (EOF-delimited protobuf, one request per stream)
is unchanged, so old portals interoperate with the new worker.

- Add stream_server behaviour (bytes-level Request/ResponseSender API)
- Decode requests in the worker actor; drop the response queues and make
  send_* async, writing straight to the stream
- Bump the libp2p fork rev for Control::accept_with_capacity, which
  buffers a burst of inbound streams instead of dropping them
- Patch crates-io libp2p-identity to the fork: the fork now builds
  against its in-tree identity crate, so consumers must use the same
  source or two incompatible libp2p_identity copies get linked
- Migrate only the worker; the portal logs collector keeps request_server
@mo4islona

mo4islona commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

I've opened a follow-up draft #212 (based on this branch): it makes ServerBehaviour generic over the request message type and moves decoding from the worker actor into the per-stream read tasks.

  • Undecodable requests are dropped at the source, before taking slots in the requests/events queues.
  • Request validation ends up in one place: the size check and decoding now sit next to each other in the read task, so whatever reaches the worker actor is already a well-formed, typed message.
  • worker.rs largely returns to its pre-migration shape: the typed QueryBehaviour/LogsBehaviour/StatusBehaviour aliases are restored (now with just the request type instead of the codec pair), and the handlers are one-liners again — the three repeated decode-and-log blocks are gone.
  • Decoding stays off the single-threaded swarm event loop (with request_response it effectively ran in the connection tasks).

The cost is one type parameter on ServerBehaviour/Request — request side only; responses stay raw bytes via ResponseSender.

WDYT?

Decode ran in on_query/on_sql_query/on_logs_request, which Wrapped calls
from the swarm poll thread — the single task that also drives Kademlia,
ping, and identify — and before the lossy enqueue, so every request was
decoded even when it was about to be dropped.

WorkerEvent now carries raw request bytes and the consumer decodes them
off the swarm loop. Responses go straight through ResponseSender, so the
send_* handle methods are gone and WorkerTransportHandle is a pure
lifetime guard. on_query/on_sql_query keep a byte-level is_empty drop;
empty logs requests are valid and forwarded as-is.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@kalabukdima
kalabukdima marked this pull request as draft July 8, 2026 07:38
@kalabukdima

kalabukdima commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator Author

Sorry for the confusion. This PR shouldn't have been marked as ready for review yet

@kalabukdima
kalabukdima marked this pull request as ready for review July 15, 2026 09:05
@kalabukdima

Copy link
Copy Markdown
Collaborator Author

This one now has a bit different approach but solves the same problems. Protobuf decoding is left for the caller site.

@kalabukdima
kalabukdima merged commit c4b85c1 into main Jul 16, 2026
3 checks passed
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.

2 participants