Skip to content
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

fix: write peer stream messages atomically #484

Merged
merged 5 commits into from
Jan 30, 2024
Merged

Conversation

achingbrain
Copy link
Collaborator

@achingbrain achingbrain commented Jan 18, 2024

The sink passed to a libp2p stream will have chunks pulled from it when the previous chunk has read by the underlying transport. This allows the transport to apply backpressure all the way up to the application level without libp2p needing to buffer application data which can be very memory-intensive.

The batch-message writing in #344 introduces a second pushable queue to peer streams - messages from both queues are written to the underlying stream in parallel, removing any guarantees of order or consistency.

Notably if we are writing a Uint8ArrayList made of multiple Uint8Arrays, the underlying transport will pull the arrays one at a time to avoid a memory copy, if the stream is written to in parallel all bets are off in regard to the order in which the arrays will be sent over the wire.

This manifests itself as the remote failing to read a message because the message length is too long - it's read some random garbage from the middle of another message as a varint representing the length of the current message.

The change here is to combine the prefixed and non-prefixed pushable streams with it-merge, allowing the messages to be pulled in a predictable way and preserving the ability of the underlying transport to apply backpressure.

Also updates all deps to reduce duplication in web bundles.

Notably multiformats@13 is the TypeScript port and @libp2p/crypto@4 has dropped the node-forge dependency.

This drops the bundle size from around 132KB to 85KB

Refs:

@achingbrain achingbrain requested a review from a team as a code owner January 18, 2024 15:08
@achingbrain achingbrain changed the title deps: update @libp2p/crypto, multiformats, uint8arrays, etc fix: update @libp2p/crypto, multiformats, uint8arrays, etc Jan 18, 2024
wemeetagain
wemeetagain previously approved these changes Jan 19, 2024
@wemeetagain
Copy link
Member

The e2e tests are consistently failing 🤔

@codecov-commenter
Copy link

codecov-commenter commented Jan 25, 2024

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (b77e6ca) 81.36% compared to head (ca2ab09) 81.40%.
Report is 1 commits behind head on master.

Files Patch % Lines
test/utils/msgId.ts 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #484      +/-   ##
==========================================
+ Coverage   81.36%   81.40%   +0.03%     
==========================================
  Files          48       48              
  Lines       12321    12325       +4     
  Branches     1301     1301              
==========================================
+ Hits        10025    10033       +8     
+ Misses       2296     2292       -4     

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

Updates all deps to reduce duplication in web bundles.
@achingbrain achingbrain changed the title fix: update @libp2p/crypto, multiformats, uint8arrays, etc fix: write peer stream messages atomically Jan 30, 2024
@wemeetagain wemeetagain merged commit cc4ff3b into master Jan 30, 2024
9 checks passed
@wemeetagain wemeetagain deleted the deps/update-all-deps branch January 30, 2024 16:13
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