Persistent channels, and channel pools for gRPC Elixir.
Add conn_grpc to your list of dependencies:
defdepsdo[{:conn_grpc,"~> 0.1"},# You also need to have gRPC Elixir installed{:grpc,"~> 0.5"}]endYou can use ConnGRPC with a pool of persistent channels, or with a single persistent channel.
Define a module that uses ConnGRPC.Pool:
defmoduleDemoPooldouseConnGRPC.Pool,pool_size: 5,channel: [address: "localhost:50051",opts: []]endThen add DemoPool to your supervision tree, and call get_channel/0 from anywhere in your application to get a channel connection:
{:ok,channel}=DemoPool.get_channel()Each time get_channel is called, a different channel from your pool will be returned using round-robin distribution.
For more info, see ConnGRPC.Pool on Hexdocs.
For a single persistent channel, define a module that uses ConnGRPC.Channel.
defmoduleDemoChanneldouseConnGRPC.Channel,address: "localhost:50051",opts: []endThen add DemoChannel to your supervision tree, and call get/0 from anywhere in your application to get your channel connection:
{:ok,channel}=DemoChannel.get()Depending on the load, using a single channel for the entire application may become a bottleneck. In that case, use the ConnGRPC.Pool module, that creates a pool of channels.
For more info, see ConnGRPC.Channel on Hexdocs.
This project uses Contributor Covenant version 2.1. Check CODE_OF_CONDUCT.md file for more information.
ConnGRPC source code is released under Apache License 2.0.