feat: allow selecting outbound protocols in request_response behaviour#6262
feat: allow selecting outbound protocols in request_response behaviour#6262MegaRedHand wants to merge 2 commits intolibp2p:masterfrom
request_response behaviour#6262Conversation
|
rust-libp2p/protocols/request-response/src/lib.rs Lines 50 to 55 in b2a9000 So as long as the type for request and response are the same, it's OK to support multiple |
| let request = OutboundMessage { | ||
| request_id, | ||
| request, | ||
| protocols: SmallVec::from_iter([protocol]), |
There was a problem hiding this comment.
Maybe we should check against outbound_protocol on the behaviour first? protocols outside of outbound_protocol may be negotiated successfully, but I don't think it is a correct behaviour.
There was a problem hiding this comment.
I agree. Allowing that will probably just lead to confusion
| request_id | ||
| } | ||
|
|
||
| /// Like [`Behaviour::send_request`], but using a specific protocol for negotiation. |
There was a problem hiding this comment.
What if a user want to send a request of a specific protocol, and a specific address, given that we also have send_request_with_addresses?
There was a problem hiding this comment.
That request isn't supported by this interface.
I was thinking about adding a new function receiving an options argument (similar interface as DialOpts). That'd replace this function and simplify/deprecate the addresses one.
drHuangMHT
left a comment
There was a problem hiding this comment.
request_response::Message does not carry information about the protocol through which the request is sent, in other words, requests seen on the receiving end are outbound-protocol-agnostic.
I don't think that's a problem. The In our case, we have an enum for requests and another for responses, which implicitly define the protocol we're using. The Codec has access to the negotiated protocol, so it knows which enum variant to read from the wire on the receiving side, and then wraps it in the enum, sending it to the application. Relevant code |
I think implementing serializing and deserializing on the enum instead of its variants works better, which makes
I read your code. I've been assuming the simplest implementation, but you are implementing rust-libp2p/protocols/request-response/src/handler.rs Lines 450 to 452 in b2a9000 So it is a missing feature to be able to select which protocol to use. I think to simplify the interface we should work on send_request itself to make it accept which protocols to use. thoughts @elenaf9 ?
|
Description
The current
request_responsemodule handles incoming requests for any of the configured protocols, negotiating the protocol on each request, but can only send requests for all of the configured protocols. This makes it impossible to handle multiple request types with a single codec.Problem example
My req-resp server supports two protocols A and B.
If a peer sends a request for protocol A, my server sends a response with protocol A (i.e. the codec receives A as parameter). If a peer sends a request for protocol B, my server sends a response with protocol B.
If I send a request through
send_request, however, the Behaviour automatically fills the protocols as [A, B], which seems to resolve to A always during negotiation. Using the newsend_request_with_protocolmethod, however, let's me specify B as the protocol to request for.Notes & open questions
send_request_with_protocolreceives a single protocol. This could be extended to receive an iterator of protocols.send_request_with_addresses. The repeated code could be moved to a new function receiving both addresses and protocols.Change checklist