NAME

Google::gRPC::Stream - Active HTTP/2 gRPC RPC stream state machine

SYNOPSIS

use Google::gRPC::Stream;

# Typically created via Google::gRPC::Channel->create_stream(...)
my $stream = $channel->create_stream(
    method         => '/google.pubsub.v1.Publisher/Publish',
    response_class => 'Google::Pubsub::V1::PublishResponse',
    deadline       => time() + 30,
    on_message     => sub {
        my ($stream, $msg) = @_;
        ...
    },
    on_trailers    => sub {
        my ($stream, $info) = @_;
        warn "Status: $info->{status} ($info->{message})\n";
    },
    on_close       => sub {
        my ($stream, $status, $message) = @_;
        ...
    },
);

# Send a message (pass 1 as second arg to half-close the send side)
$stream->write_message($req_msg, 1);

# Or pull buffered responses synchronously
while (my $msg = $stream->recv_message()) {
    ...
}

DESCRIPTION

Google::gRPC::Stream represents a single active gRPC RPC stream multiplexed over an HTTP/2 connection managed by Google::gRPC::Channel. It handles length-prefixed gRPC message framing via Google::gRPC::Framing, optional automatic Protobuf deserialization into response_class, deadline enforcement, and lifecycle callbacks (on_message, on_trailers, on_close).

ATTRIBUTES

stream_id (required, read-only)

HTTP/2 stream identifier assigned by the underlying transport engine.

channel (required, read-only)

Parent Google::gRPC::Channel instance.

type (read-only, default 'unary')

RPC streaming mode (e.g. unary, client_streaming, server_streaming, bidi_streaming).

response_class (optional, read-only)

Protobuf Perl class name (supporting parse($bytes)) used to deserialize incoming message payloads automatically.

on_message / on_trailers / on_close (read-write)

Optional coderef callbacks invoked as messages arrive, trailers are parsed, or the stream closes.

deadline (read-write)

Optional absolute epoch timestamp (from "time" in Time::HiRes). When exceeded, "check_deadline" aborts the stream with status 4 (DEADLINE_EXCEEDED).

state (read-write, default 'open')

Current stream state: open, half_closed_local, half_closed_remote, or closed.

status / status_message / status_details (read-write)

Final gRPC status code (default 0), status message, and optional decoded grpc-status-details-bin payload.

pool / ip_key (read-write)

Optional Google::gRPC::ChannelPool reference and endpoint IP key for byte accounting.

METHODS

write_message($msg, $end_stream)

$stream->write_message($protobuf_msg, 0);
$stream->write_message($protobuf_msg, 1); # half-close write side

Serializes $msg (calling $msg->serialize() if it is an object), wraps it in a 5-byte gRPC frame, records pool metrics if attached, and sends it via the parent channel. If $end_stream is true, transitions state to half_closed_local (or closed if already half_closed_remote).

close_write()

$stream->close_write();

Convenience wrapper for $stream->write_message(undef, 1) to signal end-of-stream on the client send side.

push_incoming_data($chunk)

$stream->push_incoming_data($bytes);

Appends raw HTTP/2 DATA bytes to read_buffer, unpacks any complete gRPC frames, deserializes them via response_class when configured, enqueues them into response_queue, and invokes on_message.

recv_message()

my $msg = $stream->recv_message();

Checks the stream deadline and shifts the next buffered response message off response_queue, or returns undef if none is available.

handle_trailers($trailers)

$stream->handle_trailers(\%trailers);

Parses gRPC trailing headers via "parse_trailers" in Google::gRPC::Framing, populates status, status_message, and status_details, and invokes on_trailers.

handle_close($code)

$stream->handle_close($code);

Transitions the stream to closed and invokes on_close.

check_deadline()

$stream->check_deadline();

Checks whether time() > $self->deadline and, if so, calls "abort_stream" with status 4 (Deadline Exceeded).

abort_stream($status_code, $status_msg)

$stream->abort_stream(4, 'Deadline Exceeded');

Terminates the stream with the given gRPC status code and message, firing on_trailers and "handle_close".

SEE ALSO

Google::gRPC, Google::gRPC::Channel, Google::gRPC::ChannelPool, Google::gRPC::Framing, Google::gRPC::Deadline

AUTHOR

C.J. Collier <cjac@google.com>

LICENSE

Apache License 2.0