View Source ConnGRPC.Pool (ConnGRPC v0.2.0)
A process that manages a pool of persistent gRPC channels.
When ConnGRPC.Pool
is started, it will start a pool of pre-connected channels. You can
then fetch an individual channel from it by calling ConnGRPC.Pool.get_channel/1
, which
uses round-robin to determine the channel returned.
ConnGRPC.Pool
doesn't implement any checkout mechanism and acts as a routing pool, or a simple
load balancer. The reason checkout is not implemented is because gRPC allows making multiple
requests in parallel in a single channel, so we don't need to lock the channel to a specific process
while it's being used.
module-based-pool
Module-based pool
To implement a module-based gRPC pool, define a module that uses ConnGRPC.Pool
.
defmodule DemoPool do
use ConnGRPC.Pool,
pool_size: 5,
channel: [address: "localhost:50051", opts: []]
end
The format of address
and opts
is the same used by
GRPC.Stub.connect/2
Then, you can add the module to your application supervision tree.
defmodule Demo.Application do
use Application
@impl true
def start(_type, _args) do
children = [
DemoPool
]
Supervisor.start_link(children, strategy: :one_for_one, name: Demo.Supervisor)
end
end
To get a connection from the pool in your application, call:
DemoPool.get_channel()
It'll return either {:ok, channel}
or {:error, :not_connected}
.
pool-without-module
Pool without module
If you don't want to define a module for your pool, you can add ConnGRPC.Pool
directly to your
supervision tree and pass the options on the child spec.
defmodule Demo.Application do
use Application
@impl true
def start(_type, _args) do
children = [
Supervisor.child_spec(
{ConnGRPC.Pool, name: :demo_pool, pool_size: 5, channel: [address: "localhost:50051", opts: []]},
id: :demo_pool
)
]
Supervisor.start_link(children, strategy: :one_for_one, name: Demo.Supervisor)
end
end
The format of address
and opts
is the same used by
GRPC.Stub.connect/2
To get a connection from the pool in your application, call:
ConnGRPC.Pool.get_channel(:demo_pool)
options-available
Options available
For all options available, see start_link/1
.
telemetry
Telemetry
ConnGRPC sends telemetry events. See telemetry.md.
Link to this section Summary
Functions
Returns a specification to start this module under a supervisor.
Returns all pids on the pool
Returns a gRPC channel from the pool
Starts and links supervisor that keeps a pool of gRPC channels.
Link to this section Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
Returns all pids on the pool
@spec get_channel(module() | atom()) :: {:ok, GRPC.Channel.t()} | {:error, :not_connected}
Returns a gRPC channel from the pool
Starts and links supervisor that keeps a pool of gRPC channels.
options
Options
:name
- A name to register the started process (see the:name
option inGenServer.start_link/3
):pool_size
- The size of the channel pool:channel
- Channel configuration, such as address, connection options, backoff, and callbacks. For all options, seeConnGRPC.Channel.start_link/1