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

Provide an option to specify base context for RPC connection #595

Merged
merged 2 commits into from
Dec 27, 2024

Conversation

homier
Copy link
Contributor

@homier homier commented Nov 28, 2024

Rationale

Providing a function to build base context is really useful in situations, when we need to propagate some, let's say, identity data to server handlers. Let's take the following issue as an example:

  • "I need to pass the remote peer ID to my underlying handlers, so I could do some basic identification stuff"

The problem is that now the only way to do this is to set it in rpc.Conn and pass the connection to handlers, but this breaks abstraction between transport layer and application layer.
But we could do a simple change to have a context factory, so all the necessary connection information could be set in context, that will be passed to handlers later. And it's similar to how the native http.Server interface is built.

Real-life example (using libp2p as network stack):

package handler

import 	(
       "capnproto.org/go/capnp/v3/rpc"
       "github.com/libp2p/go-libp2p/core"
)

type Handler struct {
    ...
}

func (h *Handler) Serve(stream core.Stream) *rpc.Conn {
        remotePeerID := stream.Conn().RemotePeer()

	conn := rpc.NewConn(rpc.NewPackedStreamTransport(stream), &rpc.Options{
		BootstrapClient: h.resolverHandler.Client(),
		BaseContext: func() context.Context {
		       return context.WithValue(context.Background(), "peerID", remotePeerID)
		},
	})

	return conn
}

Signed-off-by: Dzmitry Mikhalapau <[email protected]>
@homier
Copy link
Contributor Author

homier commented Dec 25, 2024

@lthibault Hi! Don't know whether it's ok to ping you, but could you review the PR please, if/when you have some free time? :) Thanks in advance!

@lthibault
Copy link
Collaborator

hey @homier, yes, totally okay to ping me! Reviewing now :)

Copy link
Collaborator

@lthibault lthibault left a comment

Choose a reason for hiding this comment

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

Firstly, thank you for the contribution! Just a quick question before I approve: have you tested that the behavior is the one you expect when the context is canceled? I'd expect it to close the connection, and judging from the code, that should happen.

Can we add a quick test for this behavior?

P.S.: as a libp2p developer myself, I'm very curious to know what you're building :)

@homier
Copy link
Contributor Author

homier commented Dec 27, 2024

@lthibault Thanks for the review! I'll add a test as soon as possible when I have some free time :) Expect a new ping from me 🙈

Just a quick question before I approve: have you tested that the behavior is the one you expect when the context is canceled? I'd expect it to close the connection, and judging from the code, that should happen.

Yeah, everything works fine, I'm using my fork with this patch at the moment, works as expected. But anyway, I'll add some tests to ensure that expected behaviour is not broken.

I'm very curious to know what you're building :)

Nothing special, I'm just doing my pet project with p2p file sharing in a form of mobile application at the moment. The code is closed now, though I'm planning to open it when I won't be ashamed of its quality :D

@lthibault
Copy link
Collaborator

Yeah, everything works fine, I'm using my fork with this patch at the moment, works as expected. But anyway, I'll add some tests to ensure that expected behaviour is not broken.

I'll merge in the meantime, just for the sake of expediency. Just go ahead and add the test in a separate PR when you get a chance.

Thanks again for your contribution!

@lthibault lthibault merged commit e9d7785 into capnproto:main Dec 27, 2024
4 checks passed
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