Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🌉 Bridges

Nearly every proxy in rama ends up doing the same thing: it holds two established sides and moves data between them. That pair is a Bridge, with an ingress and an egress field.

The ingress side faces the client, the egress side faces the server. In a peer to peer topology both sides are equal nodes and those names are only a direction convention.

Who builds a bridge

A proxy stack usually starts with one side only: the accepted client connection. Somewhere down the stack a leaf layer establishes the second side, by dialing the origin, binding a socket, or completing a handshake. That layer is the one that creates the bridge. Layers in between pass the bridge on unchanged, and the relay at the end of the stack is what actually moves the data.

Side kinds

A side is not always a byte stream. Rama proxies relay TCP and TLS streams, but also QUIC connections, UDP sockets and websocket messages. So each side carries a marker that names its kind:

MarkerWhat it carries
ByteIoa byte stream: tcp, tls, an upgraded http connection
QuicIoa QUIC connection, multiplexing streams and datagrams of its own
MessageIoa transport of whole messages, such as websocket messages
DatagramIoa datagram socket, keeping message boundaries and metadata

Each kind has its own constructor:

use rama::io::Bridge;

let bytes = Bridge::io(client_stream, origin_stream);
let quic = Bridge::quic(client_connection, origin_connection);
let messages = Bridge::messages(client_socket, origin_socket);
let datagrams = Bridge::datagrams(client_socket, origin_socket);

Byte bridges are by far the most common, so they have a shorthand:

use rama::io::BridgeIo;

// the same type as Bridge<ByteIo<I>, ByteIo<E>>
fn relay<I, E>(bridge: BridgeIo<I, E>) { /* ... */ }

Why the markers

If a bridge carried bare sides, a service that wants to relay both byte streams and QUIC connections could not be written. These two impls would overlap, because nothing stops an upstream crate from making a QUIC connection a byte stream too:

impl<I: Io, E: Io> Service<Bridge<I, E>> for MyRelay { /* ... */ }
impl<E> Service<Bridge<Connection, E>> for MyRelay { /* ... */ }  // E0119

With markers the two outer types differ, so they never overlap. One service can serve several side kinds, and mixes of them such as a byte ingress with a QUIC egress.

The markers are transparent

A marker is not a wall. It derefs to the value it wraps and forwards the traits of its kind, so a side is used where its inner value was: a ByteIo<T> reads and writes bytes, a MessageIo<T> is a stream and a sink, a DatagramIo<T> is a datagram socket. All of them forward the extensions of the value they wrap.

A bridge itself reads the extensions of its ingress side, as a flow keeps moving from ingress to egress until the relay starts to move data. Read the egress extensions explicitly where you need them:

let Bridge { ingress, egress } = bridge;
let server_name = egress.extensions().get_ref::<ServerName>();

Relays

Which relay you reach for follows the side kind:

  • byte bridges: IoForwardService in rama-net
  • message bridges: StreamForwardService in rama-core
  • datagram bridges: DatagramForwardService in rama-udp
  • QUIC bridges: QuicRelay in rama-quic

The byte and message relays report why they ended through a BridgeCloseReason. DatagramForwardService and QuicRelay return () on success instead.