hackney_h2_stream (hackney v4.4.2)

View Source

gen_statem process for a single full-duplex HTTP/2 stream (gRPC-style bidirectional streaming).

This module mirrors hackney_wt / hackney_ws: an application gets a pid and uses send/recv (passive) or active-mode messages. It owns one dedicated HTTP/2 connection (negotiated via ALPN) and opens one stream on it whose events are routed here directly by the h2 library's per-stream handler mechanism, so the request body and the response can be sent and read interleaved on the same stream.

Send / receive

send/2,3 writes a DATA frame (optionally with END_STREAM via fin). send_trailers/2 closes the send side with trailing HEADERS (gRPC carries grpc-status there). recv/1,2 returns the next inbound message: {response, Status, Headers}, {data, Data}, {trailers, Trailers} or done (the peer ended the stream). After done, recv returns {error, closed}.

Delivery modes

Passive (default): messages are buffered and read with recv. Active ({active, true|once}): every message is forwarded to the owner as {hackney_h2, Pid, Msg}, and errors as {hackney_h2_error, Pid, Reason}.

Flow control

With {flow_control, manual} the receive window is only replenished by consume/2, so a slow reader applies backpressure to the peer. The default auto replenishes automatically.

States: idle (not yet connected), connected (ready), closed (stream/conn ended, buffered data still drainable).

Summary

Functions

Cancel the stream and tear down the connection.

Establish the connection and open the stream. Blocks until ready.

Acknowledge N consumed bytes (manual flow control only).

Assign a new controlling process.

Receive the next inbound message (passive mode only).

Send a DATA frame on the stream (no END_STREAM).

Send a DATA frame, optionally half-closing the send side (fin).

Send trailing HEADERS, half-closing the send side (gRPC trailers).

Set options. Supported: [{active, true|false|once}].

Start a stream process. See hackney:h2_open/3,4 for option docs.

Types

h2_msg/0

-type h2_msg() :: {response, non_neg_integer(), list()} | {data, binary()} | {trailers, list()} | done.

Functions

close(Pid)

-spec close(pid()) -> ok.

Cancel the stream and tear down the connection.

closed(_, OldState, Data)

connect(Pid)

-spec connect(pid()) -> ok | {error, term()}.

Establish the connection and open the stream. Blocks until ready.

connect(Pid, Timeout)

-spec connect(pid(), timeout()) -> ok | {error, term()}.

connected(_, OldState, Data)

consume(Pid, NBytes)

-spec consume(pid(), non_neg_integer()) -> ok | {error, term()}.

Acknowledge N consumed bytes (manual flow control only).

controlling_process(Pid, NewOwner)

-spec controlling_process(pid(), pid()) -> ok | {error, term()}.

Assign a new controlling process.

idle(_, OldState, Data)

recv(Pid)

-spec recv(pid()) -> {ok, h2_msg()} | {error, term()}.

Receive the next inbound message (passive mode only).

recv(Pid, Timeout)

-spec recv(pid(), timeout()) -> {ok, h2_msg()} | {error, term()}.

send(Pid, Data)

-spec send(pid(), iodata()) -> ok | {error, term()}.

Send a DATA frame on the stream (no END_STREAM).

send(Pid, Data, Fin)

-spec send(pid(), iodata(), fin | nofin) -> ok | {error, term()}.

Send a DATA frame, optionally half-closing the send side (fin).

send_trailers(Pid, Trailers)

-spec send_trailers(pid(), list()) -> ok | {error, term()}.

Send trailing HEADERS, half-closing the send side (gRPC trailers).

setopts(Pid, Opts)

-spec setopts(pid(), list()) -> ok | {error, term()}.

Set options. Supported: [{active, true|false|once}].

start_link(Opts)

-spec start_link(map()) -> {ok, pid()} | {error, term()}.

Start a stream process. See hackney:h2_open/3,4 for option docs.