Skip to content

ours-h2 ​

libs/h2 — HTTP/2 (RFC 9113) for the ours transports: only what they need, without copies on the data path.

Why our own implementation ​

The transports need two things the h2 crate (hyperium) does not give:

  • received DATA as slices of pooled read buffers (128 KiB, from ours-bufpool, whose per-shard caches the kernel trims), and sent DATA taken as is;
  • DATA frames written straight into the record slots of our TLS (Frames, below).

The h2 crate stays a dev dependency, the reference in compatibility tests.

Design ​

  • One task drives the connection's I/O: on the client — the client::Connection future, on the server — server::Connection::poll_accept/accept. Streams queue frames and take what was received; the driver writes the queue in batches with vectored writes.
  • client::handshake → SendRequest (send_request, poll_ready, max_send_streams, extended_connect_enabled, goaway_reason, poll_ping) + Connection.
  • server::handshake → Connection (accept, graceful_shutdown, has_streams).
  • Stream — one stream: poll_chunk, start_send/poll_flush_send, poll_finish, send_response, reset; ResponseFuture.
  • Frames (Stream::poll_frames/send_frames) — writing without a copy through a TLS that seals records in place (Io::record_room/poll_write_records):
    • Layout: a buffer from huge_stream_pool is laid out for TLS records (room for the record header and the tag). It holds the same bytes a copying writer would put there: the caller's messages of at most message bytes, each from the start of a record with front bytes for its prefix; DATA frames of the peer's SETTINGS_MAX_FRAME_SIZE go back to back through full records, a frame header wherever it falls. Data is read straight into the frames.
    • Windows: the stream and connection windows are taken in advance in whole messages, and the writer waits for a window of at least one message (at most a quarter of the window). Otherwise every small window update would give a small message, and Go returns the window in equally small pieces, so the exchange would stick on small messages. The shared connection window is awaited this way only while no other stream waits for it: streams each holding a share would wait for each other forever. An unused window is returned.
    • Fallback: the driver hands such a buffer to the Io whole; an Io that does not take buffers gets the same frames with an ordinary vectored write.
  • Fields — a list of headers in one buffer with the RFC 9113 §8 malformedness checks.
  • frame — the frame header, types, flags, error codes (Reason), the payload of control frames.
  • hpack — the decoder is complete (the peer may use everything); the encoder uses only the static table and literals without indexing, which the RFC allows, so the encoder has no state.
  • Config — connection settings; the Io trait — the stream the connection runs over.
  • The connection state is an Arc<Shared> with a Mutex; Stream, SendRequest and ResponseFuture are Send, and a stream can be served on a different executor thread than the driver (this is how shards separate a stream's relay and the session, see Runtime). The driver and a stream wake each other with Wakers; the lock is held only while parsing one read chunk or queuing one write.

Who uses it ​

ours-http-client, ours-http, ours-quinn-ext (the HPACK Huffman decoder for QPACK in h3.rs), ours-proto (the h2 feature, the tunnel of transport-http, transport-grpc, transport-xhttp; decoy), the modules transport-grpc, transport-xhttp and mux (h2mux).

Specifics ​

  • #![forbid(unsafe_code)] in lib.rs in addition to the workspace lint.
  • examples/h2spec_server.rs — a server for running the external h2spec.

Tests and fuzzing ​

  • Unit tests in src/; libs/h2/tests/connection.rs — connections against the h2 crate in both directions; libs/h2/tests/frames.rs — Frames against the h2 crate with small and large windows, frame and message sizes, full records, small records, a waiting and a refusing write; libs/h2/tests/cross_thread.rs — streams served on a different executor thread than the driver: echo, window back pressure, RST in both directions.
  • libs/h2/fuzz — the cargo-fuzz targets hpack, frame, conn (a separate workspace).

GPL-3.0-or-later