pub struct Bridge<U: BridgePeer, C> { /* private fields */ }Expand description
Carries envelopes to upstream under one agreement and brings the answers
back.
The second parameter is the codec the reply path translates through, injected like the peer. It is not defaulted: a default would have to name the crate’s own adapter, and application code must not know which adapter it is wired to.
§Why there is no downstream peer
A bridge used to take one, and the type said “carries envelopes from
downstream to upstream”. It never did: forward takes
the envelope as an argument and returns the answer, which is what a call
from a loop that already reads the client wants. The peer was reachable only
through client — which nothing called — and close, so
every caller passed a second handle on the client’s connection that was
never used to talk to the client and closed at the end on a guess.
The client channel belongs to whoever owns the client’s connection, which is the loop: it reads frames from a source and writes replies to a sink. This type is the part that does not care.
Implementations§
Source§impl<U: BridgePeer, C: ToolResultCodec> Bridge<U, C>
impl<U: BridgePeer, C: ToolResultCodec> Bridge<U, C>
Sourcepub const fn new(
agreement: Agreement,
policy: DegradePolicy,
upstream: U,
codec: C,
) -> Self
pub const fn new( agreement: Agreement, policy: DegradePolicy, upstream: U, codec: C, ) -> Self
A bridge over a peer that has already agreed.
Sourcepub const fn upstream_version(&self) -> ProtocolVersion
pub const fn upstream_version(&self) -> ProtocolVersion
The version the upstream peer speaks.
Sourcepub const fn upstream(&self) -> &U
pub const fn upstream(&self) -> &U
The peer this bridge forwards through.
The owner of the upstream connection, not a handle opened for
convenience: a bridge that could talk to the server by a second path
would have two owners of one session. Exposed because the frames a peer
receives unasked for belong to it and nobody else — see
BridgePeer::take_pending.
Sourcepub async fn forward(
&self,
envelope: &Map<String, Value>,
) -> Result<Forwarded, BridgeError>
pub async fn forward( &self, envelope: &Map<String, Value>, ) -> Result<Forwarded, BridgeError>
Forwards one envelope and brings the answer back.
A tools/call reply is translated into the client’s revision on the way
back, and only if the client’s revision cannot read what the server
said. Every other reply, and every reply that needs no translation, comes
back as the upstream wrote it — see
content for why rebuilding a
reply that was not asking to be rebuilt is a bug.
§Errors
BridgeError — the envelope was unreadable, the two peers share no
feature for the method asked, the upstream could not be reached, or the
reply could not be expressed at the client’s revision. The method is
checked against the agreement before anything is sent, so a doomed
call never reaches the wire.
Sourcepub async fn close(&self) -> Result<(), BridgeError>
pub async fn close(&self) -> Result<(), BridgeError>
Ends the conversation with the upstream peer.
The client’s connection is not this type’s to close: the loop that read the frames opened it, and it ends when the client’s stream does.
§Errors
BridgeError when the upstream refused to close.