NAME

Google::gRPC::ChannelPool - Round-robin gRPC subchannel pool with per-endpoint metrics

SYNOPSIS

use Google::gRPC::ChannelPool;

my $pool = Google::gRPC::ChannelPool->new(
    target     => 'pubsub.googleapis.com:443',
    auth_token => $bearer_token,
);

# Round-robin subchannel selection
my $channel = $pool->get_channel();

# Or create a stream directly with automatic request/response byte accounting
my $stream = $pool->create_stream(
    method         => '/google.pubsub.v1.Publisher/Publish',
    request        => $req_msg,
    response_class => 'Google::Pubsub::V1::PublishResponse',
    on_message     => sub {
        my ($stream, $resp) = @_;
        ...
    },
);

# Inspect per-endpoint traffic metrics
my $all_metrics = $pool->get_metrics();
$pool->reset_metrics();

DESCRIPTION

Google::gRPC::ChannelPool resolves a gRPC target hostname into one or more IP addresses (using Socket::getaddrinfo, or a caller-supplied resolved_ips list), creates a dedicated Google::gRPC::Channel subchannel for each endpoint, and distributes RPCs across them in round-robin order.

It also tracks per-endpoint request counts and framed byte totals (requests, bytes_sent, bytes_received, total_bytes) whenever streams are created via "create_stream".

ATTRIBUTES

target (required, read-only)

Target host and optional port (e.g. pubsub.googleapis.com:443, [::1]:50051, or localhost). Defaults to port 443 when omitted.

auth_token (optional, read-only)

Bearer token forwarded to each underlying Google::gRPC::Channel.

engine_type (optional, read-only)

Transport engine type (e.g. xs, pureperl, mock) forwarded to each underlying Google::gRPC::Channel.

resolved_ips (optional, read-only)

Optional arrayref of pre-resolved IP addresses (or host:port targets) to use instead of performing DNS resolution via getaddrinfo.

subchannels (read-write)

Arrayref of Google::gRPC::Channel objects managed by the pool.

rr_index (read-write)

Current round-robin cursor index.

metrics (read-write)

Hashref keyed by endpoint IP address containing per-endpoint counters (requests, bytes_sent, bytes_received, total_bytes, target).

METHODS

get_channel()

my $channel = $pool->get_channel();

Returns the next Google::gRPC::Channel in round-robin order, or undef if the pool has no subchannels.

create_stream(%opts)

my $stream = $pool->create_stream(
    method         => '/pkg.Service/Method',
    request        => $req,
    response_class => 'My::Response',
    on_message     => sub { my ($stream, $msg) = @_; ... },
);

Selects the next subchannel via "get_channel", records outgoing request metrics, wraps on_message to record incoming response byte metrics, and delegates to $channel->create_stream(%opts). Returns the resulting Google::gRPC::Stream with its pool and ip_key attributes populated.

record_request($ip_key, $bytes)

$pool->record_request('127.0.0.1', 128);

Increments requests, bytes_sent, and total_bytes for $ip_key.

record_response($ip_key, $bytes)

$pool->record_response('127.0.0.1', 256);

Increments bytes_received and total_bytes for $ip_key.

get_metrics($key)

my $all = $pool->get_metrics();
my $one = $pool->get_metrics('127.0.0.1');

Returns the metrics hashref for $key (accepting either an IP key or a host:port target string), or the full metrics hashref when $key is omitted.

reset_metrics()

$pool->reset_metrics();

Resets requests, bytes_sent, bytes_received, and total_bytes to 0 for all tracked endpoints.

SEE ALSO

Google::gRPC, Google::gRPC::Channel, Google::gRPC::Stream

AUTHOR

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

LICENSE

Apache License 2.0