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

Improve peer connection handling #526

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

peterargue
Copy link

This PR was originaly submitted to ipfs/go-bitswap#590

Currently, the bitswap network only adds peers to its client/server peer lists that connected after the network has started. This means the network must be started before enabling the libp2p listeners, otherwise some peers may connect too early and never be added to the network.

To address this, this PR updates the bitswap network's Start() method to first, listen for new peer connection events, and then iterate through all peers and add any that support the bitswap protocols. The issue and solution were originally described by @Stebalien in #83.

@peterargue peterargue requested a review from a team as a code owner December 16, 2023 01:24
Copy link

welcome bot commented Dec 16, 2023

Thank you for submitting this PR!
A maintainer will be here shortly to review it.
We are super grateful, but we are also overloaded! Help us by making sure that:

  • The context for this PR is clear, with relevant discussion, decisions
    and stakeholders linked/mentioned.

  • Your contribution itself is clear (code comments, self-review for the
    rest) and in its best form. Follow the code contribution
    guidelines

    if they apply.

Getting other community members to do a review would be great help too on complex PRs (you can ask in the chats/forums). If you are unsure about something, just leave us a comment.
Next steps:

  • A maintainer will triage and assign priority to this PR, commenting on
    any missing things and potentially assigning a reviewer for high
    priority items.

  • The PR gets reviews, discussed and approvals as needed.

  • The PR is merged by maintainers when it has been approved and comments addressed.

We currently aim to provide initial feedback/triaging within two business days. Please keep an eye on any labelling actions, as these will indicate priorities and status of your contribution.
We are very grateful for your contribution!

@peterargue
Copy link
Author

Hey @Jorropo, I ported this change over.

bitswap/network/ipfs_impl.go Outdated Show resolved Hide resolved
bsnet.cancel()
}

func (bsnet *impl) peerUpdatedSubscription(ctx context.Context, sub event.Subscription) {
Copy link
Member

Choose a reason for hiding this comment

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

This will race with the "old" version of notifications. Instead, we need to:

  1. Drop the old version (the net notifiee).
  2. Subscribe to EvtPeerConnectednessChanged to get connectivity changes.
  3. We care about "disconnected" events, but ignore "connected" events. Instead, rely on "protocol changed" and "identify" notifications to tell you that the peer supports bitswap (but check this with @Jorropo).

Copy link
Member

Choose a reason for hiding this comment

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

Ah, but once you do this, you also have to move the existing connection walk to the top of this function. That is, you need to:

  1. Subscribe to connect/disconnect events.
  2. Process all existing connections.
  3. Start processing the events.

Failure modes:

  • If you do 2 before 1, you could miss new connections.
  • If you do 3 concurrent with 2, you could process a connect/disconnect event in step-3, then re-add the peer in the step-2 loop.

If you do them in order (1,2,3), you may end up processing a "connect" event for a disconnected peer in step 3, but then you'll process the disconnect event.

You could additionally check the peer's "connectedness" before updating a peer's status, but I don't think that is necessary.

Copy link
Member

Choose a reason for hiding this comment

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

Hm. Although, I think we need to ask libp2p for all connections to the peer to make sure that we have a non-transient one. I wonder if that'll cause any issues? I assume we'll re-identify when we create a new connection... That'll require some testing.

Ideally libp2p would let us filter protocols by connection type, but that's a larger change.

Thoughts @Jorropo? @aschmahmann?

bitswap/network/ipfs_impl.go Show resolved Hide resolved
@Stebalien
Copy link
Member

I've filed two related libp2p issues:

Copy link

codecov bot commented Jan 29, 2024

Codecov Report

Attention: Patch coverage is 73.33333% with 16 lines in your changes are missing coverage. Please review.

Project coverage is 65.48%. Comparing base (79cb4e2) to head (decd8d9).
Report is 23 commits behind head on main.

❗ Current head decd8d9 differs from pull request most recent head a8040f8. Consider uploading reports for the commit a8040f8 to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #526      +/-   ##
==========================================
- Coverage   65.56%   65.48%   -0.08%     
==========================================
  Files         207      204       -3     
  Lines       25597    25588       -9     
==========================================
- Hits        16782    16756      -26     
- Misses       7336     7351      +15     
- Partials     1479     1481       +2     
Files Coverage Δ
bitswap/network/ipfs_impl.go 74.70% <73.33%> (+0.23%) ⬆️

... and 18 files with indirect coverage changes

@Stebalien
Copy link
Member

I believe libp2p/go-libp2p#2696 should unblock this from the libp2p side.

@peterargue
Copy link
Author

@Jorropo any chance you can take a look and provide any remaining feedback?

@lidel lidel added the status/blocked Unable to be worked further until needs are met label Apr 16, 2024
@peterargue peterargue force-pushed the improve-peer-connection-management branch from a8040f8 to 3ef8940 Compare September 3, 2024 18:28
@lidel
Copy link
Member

lidel commented Sep 10, 2024

Triage notes:

@lidel lidel added the need/maintainers-input Needs input from the current maintainer(s) label Sep 10, 2024
@lidel lidel marked this pull request as draft October 15, 2024 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need/maintainers-input Needs input from the current maintainer(s) status/blocked Unable to be worked further until needs are met
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants