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

clean up usage of string vs. protocol.ID #1886

Closed
marten-seemann opened this issue Nov 16, 2022 · 6 comments · Fixed by #2004
Closed

clean up usage of string vs. protocol.ID #1886

marten-seemann opened this issue Nov 16, 2022 · 6 comments · Fixed by #2004
Labels
effort/hours Estimated to take one or several hours exp/beginner Can be confidently tackled by newcomers kind/enhancement A net-new feature or improvement to an existing feature

Comments

@marten-seemann
Copy link
Contributor

It's all over the place, and we're converting between the two all the time.

@marten-seemann marten-seemann added kind/enhancement A net-new feature or improvement to an existing feature exp/beginner Can be confidently tackled by newcomers effort/hours Estimated to take one or several hours labels Nov 16, 2022
@sukunrt
Copy link
Member

sukunrt commented Jan 17, 2023

can i pick this up?

@sukunrt
Copy link
Member

sukunrt commented Jan 18, 2023

I'm looking into this. Discussed the issue with @marten-seemann
Here a simple generic type constraint of ~string won't suffice since multistream.MultistreamMuxer is a struct and method parameters cannot be generic. To make a method like
func (msm *MultistreamMuxer) AddHandlerWithFunc(protocol string, match func(string) bool, handler HandlerFunc)
generic we'd need MultistreamMuxer to be generic with a signature like
func (msm *MultistreamMuxer[T ~string]) AddHandlerWithFunc(protocol T, match func(T) bool, handler HandlerFunc)
that seems tricky to do without breaking the api since this constructor NewMultistreamMuxer() expects to create a muxer without any type being specified. I'll come back on the possible options here.

@marten-seemann
Copy link
Contributor Author

generic we'd need MultistreamMuxer to be generic with a signature like
func (msm *MultistreamMuxer[T ~string]) AddHandlerWithFunc(protocol T, match func(T) bool, handler HandlerFunc)

Yes, that would be great!

that seems tricky to do without breaking the api since this constructor NewMultistreamMuxer() expects to create a muxer without any type being specified. I'll come back on the possible options here.

I wouldn’t worry about that. Breaking the API is ok here, we can release this as a new major version.

If you really, really want to avoid breaking API changes you could do this

func NewGenericMultistreamMuxer[T ~string]() MultistreamMuxer[T] {
    …
}

and then have NewMultistreamMuxer return the concrete string type:

func NewMultistreamMuxer() MultistreamMuxer[string] {
    return NewGenericMultistreamMuxer[string]()
}

Not sure if that’s worth it here though, we don’t use it at so many places that avoiding the API change would actually pay off.

@sukunrt
Copy link
Member

sukunrt commented Jan 20, 2023

For go-libp2p the changes are here. I have changed PeerStore. There are a couple of other places where we can remove the converstions before integrating the changes go-multistream repo. I'll update those changes soon.

#2004

@sukunrt
Copy link
Member

sukunrt commented Jan 20, 2023

There is one source of conversion that remains, flow.MeterRegistry. However this only impacts bandwidth.go. @marten-seemann Should I change that to a generic implementation as well?

@marten-seemann
Copy link
Contributor Author

There is one source of conversion that remains, flow.MeterRegistry. However this only impacts bandwidth.go. @marten-seemann Should I change that to a generic implementation as well?

I'm undecided on that one. I think for now it's ok to leave it as strings, since it's a fairly contained code path, and since we're not using the FindIdle function, which is the only function that returns a []string (and would need to be converted to []protocol.ID).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort/hours Estimated to take one or several hours exp/beginner Can be confidently tackled by newcomers kind/enhancement A net-new feature or improvement to an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants