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

feat(swarm): enforce creation of Swarm via SwarmBuilder #3588

Merged
merged 4 commits into from
Mar 13, 2023

Conversation

vnermolaev
Copy link
Contributor

@vnermolaev vnermolaev commented Mar 10, 2023

Description

Mark constructors Swarm::with_X_executor as deprecated.
Move the deprecated functionality to SwarmBuilder::with_X_executor
Use SwarmBuilder throughout.

Resolves #3186.
Resolves #3107.

Notes

The main file to audit is https://github.com/libp2p/rust-libp2p/pull/3588/files#diff-7d75430c06d5d5aeba4229b6535d2b56f4e67809ffdac99d2dec8eb8a81b3c5c.
The rest contain changes related to the API change.

Change checklist

Changelog is to be updated after the tech side is settled.

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • A changelog entry has been made in the appropriate crates

@vnermolaev vnermolaev changed the title feat(swarm): Deprecate creation methods of Swarm in favour of SwormBuilder. feat(swarm): Deprecate creation methods of Swarm in favour of SwarmBuilder. Mar 10, 2023
@vnermolaev vnermolaev changed the title feat(swarm): Deprecate creation methods of Swarm in favour of SwarmBuilder. feat(swarm): Enforce creation of Swarm via SwarmBuilder. Mar 10, 2023
@vnermolaev vnermolaev force-pushed the feature/swarm-builder branch 3 times, most recently from 537d608 to e170ac0 Compare March 10, 2023 20:05
@thomaseizinger thomaseizinger changed the title feat(swarm): Enforce creation of Swarm via SwarmBuilder. feat(swarm): Enforce creation of Swarm via SwarmBuilder Mar 11, 2023
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Thank you for this! Left some comments.

swarm/src/lib.rs Outdated Show resolved Hide resolved
swarm/src/lib.rs Outdated Show resolved Hide resolved
swarm/src/lib.rs Outdated Show resolved Hide resolved
swarm/src/lib.rs Outdated
impl<TBehaviour> Swarm<TBehaviour>
where
TBehaviour: NetworkBehaviour,
{
/// Builds a new `Swarm` with a provided executor.
#[deprecated(
note = "Use a builder chain `SwarmBuilder::with_executor(transport, behaviour, local_peer_id, executor).build()` instead."
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can shorten this to:

Suggested change
note = "Use a builder chain `SwarmBuilder::with_executor(transport, behaviour, local_peer_id, executor).build()` instead."
note = "Use `SwarmBuilder::with_executor` instead."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will replace it, however, the message I propose could be used as a direct replacement.

Copy link
Contributor

Choose a reason for hiding this comment

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

I will replace it, however, the message I propose could be used as a direct replacement.

That only works if users use these exact variable names. The only thing we can say for sure is the name of the function so that is what I'd like to point to.

The story would be different if Rust had support for codemods :)

@mergify
Copy link
Contributor

mergify bot commented Mar 12, 2023

This pull request has merge conflicts. Could you please resolve them @vnermolaev? 🙏

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Nice, thank you for that!

Can you add a changelog entry to libp2p-swarm please?

swarm/src/lib.rs Outdated
Comment on lines 1560 to 1576
/// Sets executor to a threadpool executor.
pub fn with_threadpool_executor(
transport: transport::Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
local_peer_id: PeerId,
) -> Self {
match ThreadPoolBuilder::new()
.name_prefix("libp2p-swarm-task-")
.create()
{
Ok(tp) => Self::with_executor(transport, behaviour, local_peer_id, tp),
Err(err) => {
log::warn!("Failed to create executor thread pool: {:?}", err);
Self::without_executor(transport, behaviour, local_peer_id)
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not introduce this one. See #3107.

Copy link
Contributor Author

@vnermolaev vnermolaev Mar 13, 2023

Choose a reason for hiding this comment

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

Actually, I cannot simply not introduce it.

error[E0599]: no function or associated item named `with_threadpool_executor` found for struct `SwarmBuilder` in the current scope
  --> examples/file-sharing/src/network.rs:50:31
   |
50 |     let swarm = SwarmBuilder::with_threadpool_executor(
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^
   |                               |
   |                               function or associated item not found in `SwarmBuilder<_>`
   |                               help: there is an associated function with a similar name: `with_executor`

So I keep it for now and then it will be removed as part of #3107?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to not introduce code only to deprecate it later again. Would you mind porting the file-sharing example to a different executor?

swarm/src/lib.rs Outdated
@@ -401,6 +405,7 @@ where
}

/// Builds a new `Swarm` with a threadpool executor.
#[deprecated(note = "Use `SwarmBuilder::with_threadpool_executor` instead.")]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#[deprecated(note = "Use `SwarmBuilder::with_threadpool_executor` instead.")]
#[deprecated(note = "The `futures::executor::ThreadPool` executor is deprecated. See https://github.com/libp2p/rust-libp2p/issues/3107.")]

@vnermolaev vnermolaev force-pushed the feature/swarm-builder branch 2 times, most recently from 2595802 to 3015e93 Compare March 13, 2023 16:13
swarm/CHANGELOG.md Outdated Show resolved Hide resolved
swarm/CHANGELOG.md Outdated Show resolved Hide resolved
@thomaseizinger thomaseizinger changed the title feat(swarm): Enforce creation of Swarm via SwarmBuilder feat(swarm): enforce creation of Swarm via SwarmBuilder Mar 13, 2023
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Excellent, thank you!

@mergify mergify bot merged commit 2ec5402 into libp2p:master Mar 13, 2023
umgefahren pushed a commit to umgefahren/rust-libp2p that referenced this pull request Mar 8, 2024
Mark constructors `Swarm::with_X_executor` as deprecated.
Move the deprecated functionality to `SwarmBuilder::with_X_executor`
Use `SwarmBuilder` throughout.

Resolves libp2p#3186.
Resolves libp2p#3107.

Pull-Request: libp2p#3588.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants