SQC-871 Implements socket serialization for transferring socket over RPC
- Adds TransferredSocketStream class that handles implementing underlying AsyncIoStream IO operations for deserializing socket
- Socket deserialization unwraps raw kj streams and builds non-deferred stream to pass into TransferredSocketStream
- Adds js-rpc-socket-test tests
- Add outbound interceptor test to showcase full end to end use case of loopback binding
A few questions...
- How does socket upgrade (startTLS) work with a serialized socket?
- It looks like
isDefaultFetchPortis not set? - There doesn't seem to be any handling of opened state. A socket uses the
openedPromiseto signal when the socket is actually opened/established. From the looks of it here, it would be possible to serialize a still not-yet-open socket but the receiving side would appear to be opened? It's not clear. - What if the socket is already being closed? Or is already closed?
A few questions...
- How does socket upgrade (startTLS) work with a serialized socket?
- It looks like
isDefaultFetchPortis not set?- There doesn't seem to be any handling of opened state. A socket uses the
openedPromiseto signal when the socket is actually opened/established. From the looks of it here, it would be possible to serialize a still not-yet-open socket but the receiving side would appear to be opened? It's not clear.- What if the socket is already being closed? Or is already closed?
@jasnell
- In the case of Hyperdrive needs, socket upgrade isn't needed as Hyperdrive will handle TLS to the origin. In the generalized socket case I am not sure. I imagine that we would need to somehow allow the transferred socket to upgrade tls and start tls handshake but I would probably prefer if the socket has already been upgraded before transfer instead?
- Updated to add
isDefaultFetchPortto worker-interface.capnp and serialize/deserialize ✅ - I thought about this for a while and I think throwing an error is fair if the socket is still pending/closed. I can't think of a good case where receiving a socket and needing to await on socket.opened would be a fair user experience. I would prefer that the socket is assumed to be opened and connected. I've gone ahead and implemented some checks on serialize to prevent serializing the socket when that state is pending/closed but fully open to other implementation ideas here
- Throwing error on serialize feels fair to me. There is still the case of where on serialize the socket is opened, but during transit the connection closes and we assume the serialize socket info is still opened on deserialize. This should naturally resolve to closed but might show some disconnect errors? Let me know if this behavior is not preferred
Yeah, like James mentioned, I'd repeat that currently as things stand there is a need for more synchronisation with the various socket states (closed/open). But perhaps that's planned since this is still a draft :)
In general though I think this is a good start. I left a few comments about various things I'd improve as well, but nothing major.
@dom96
Thank you for the initial review, I have spent some extra time addressing the feedback and have added a second commit to this merge request to handle the various states I initially put on TODO: halfOpen, opened, closed
I'll re-review my second commit today again but this seems to be functional now
Merging this PR will improve performance by 9.77%
:warning: Different runtime environments detected
Some benchmarks with significant performance changes were compared across different runtime environments, which may affect the accuracy of the results.
⚡ 1 improved benchmark
✅ 71 untouched benchmarks
⏩ 129 skipped benchmarks[^skipped]
Performance Changes
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | bodyWithHeaders[Response] |
34.2 µs | 31.2 µs | +9.77% |
[!TIP] Curious why this is faster? Comment
@codspeedbot explain why this is fasteron this PR, or directly use the CodSpeed MCP with your agent.
Comparing Ltadrian:dlapid/jsrpc_socket (a44dee4) with main (a77b7aa)
[^skipped]: 129 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.
Addressed most comments by James/Dominik here. Moving entire changeset and discussions to internal MR. Will close for now