NAME

Google::gRPC::Server - HTTP/2 gRPC server for Perl

SYNOPSIS

use Google::gRPC::Server;

my $server = Google::gRPC::Server->new(
    host => '127.0.0.1',
    port => 50051,
);

$server->register_method(
    'helloworld.Greeter',
    'SayHello',
    sub {
        my ($req, $ctx) = @_;
        return 'Hello, ' . $req;
    },
);

$server->run();

DESCRIPTION

Google::gRPC::Server implements a gRPC-over-HTTP/2 server in Perl using Protocol::HTTP2::Server, AnyEvent::Socket, Google::gRPC::Framing, and Google::gRPC::Status.

ATTRIBUTES

host

Bind address for "listen" (default: 127.0.0.1).

port

TCP port for "listen" (default: 0, which binds an ephemeral port and updates port to the assigned port number).

METHODS

register_method( $service, $method, \&handler, \%opts )

Registers a unary RPC handler for /$service/$method. Optional $opts->{request_class} automatically decodes incoming request payloads via $request_class->decode($bytes).

register_service( $service, \%methods )

Registers multiple methods for $service. Each value in %methods may be a CODE reference or a hashref with handler and optional request_class.

dispatch_path( $path )

Returns the registered handler specification hashref for $path, or undef if no handler is registered.

handle_unary( $path, $payload, \%metadata )

Invokes the registered handler for $path and returns ($grpc_status_code, $grpc_message, $response_bytes).

new_session()

Returns a Protocol::HTTP2::Server instance wired to this server's gRPC dispatcher and framing layer, suitable for both live sockets and in-memory testing.

listen( %opts )

Binds a TCP listener via AnyEvent::Socket::tcp_server and returns the bound port number.

run( %opts )

Calls "listen" if not already listening and blocks in the AnyEvent event loop until "stop" is called.

stop()

Closes the TCP listener and unblocks "run".

SEE ALSO

Google::gRPC, Google::gRPC::Client, Google::gRPC::Framing, Google::gRPC::Status

AUTHOR

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

LICENSE

Licensed under the Apache License, Version 2.0.