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(example): showcase how to manually compose NetworkBehaviour #5884

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

drHuangMHT
Copy link
Contributor

Description

Showcase how to compose NetworkBehaviours manually without the derive macro or Either.

Notes & open questions

Although this is not the recommended approach(see #3902, huge shoutout to thomas by the way), it can show us how the protocols and substreams get negotiated. Suggestions welcome!

Change checklist

  • 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

@drHuangMHT drHuangMHT changed the title feat(example): showcase how to manually composing NetworkBehaviour feat(example): showcase how to manually compose NetworkBehaviour Feb 24, 2025
@elenaf9
Copy link
Contributor

elenaf9 commented Feb 24, 2025

Thanks for this effort @drHuangMHT!

But looking through the code of this PR, I am unsure if this will really help newcomers. It's 1200 lines of code that are mostly just the expanded NetworkBehavior macro, right? I think it will be difficult for users to extract the key aspects out of this.

I agree that it would be great to have more resources on how to manually compose NetworkBehaviors, but I think the format of a Tutorial would be more suited. That way the key aspects of delegating calls, polling order and composed event types could be explained with some code snippets, but we don't need all of this boilerplate code. Wdyt?

@drHuangMHT
Copy link
Contributor Author

drHuangMHT commented Feb 25, 2025

It's 1200 lines of code that are mostly just the expanded NetworkBehavior macro, right?

Yes indeed.

I think it will be difficult for users to extract the key aspects out of this.

Maybe for some, yes. But I'm expecting advanced users to read this. Plus I myself am not very familiar with how rust-libp2p works so I am also seeking for explanation for some things.

I agree that it would be great to have more resources on how to manually compose NetworkBehaviors, but I think the
format of a Tutorial would be more suited.

Yes, but there is also timeliness problem with tutorials right?

That way the key aspects of delegating calls, polling order and composed event types could be explained with some code snippets, but we don't need all of this boilerplate code. Wdyt?

Some people are more comfortable with a working example. But I have to admit that there is too much boilerplate code.

What I write above are not very strong argument for this PR though. Do you think some simple macros will make it better?

@elenaf9
Copy link
Contributor

elenaf9 commented Feb 25, 2025

Do you think some simple macros will make it better?

Well, we already have the NetworkBehavior macro that does everything 😄. I don't think adding more macros in the example will make it easier to understand.

Yes, but there is also timeliness problem with tutorials right?

You mean with them staying up-to-date? Generally I agree, but I would argue that central concepts when composing network behaviors will stay the same.


Generel question: when would one want to manually compose such a "parent" NetworkBehavior in the first place?.

@drHuangMHT
Copy link
Contributor Author

Generel question: when would one want to manually compose such a "parent" NetworkBehavior in the first place?.

I would like to see if it is possible to negotiate protocols dynamically(instead of Toggle) and it seems it is? That's not possible with the derive macro.

@elenaf9
Copy link
Contributor

elenaf9 commented Feb 25, 2025

I would like to see if it is possible to negotiate protocols dynamically(instead of Toggle) and it seems it is?

What do you mean with negotiate protocols dynamically?

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