View Source ConnGRPC.Channel (ConnGRPC v0.2.0)

A process that manages a gRPC channel.

When ConnGRPC.Channel is started, it will create a gRPC connection, which can be fetched with ConnGRPC.Channel.get/1.

You can use this if you want to keep a persistent gRPC channel open to be reused in your application.

Depending on the load, using a single channel for the entire application may become a bottleneck. In that case, see the ConnGRPC.Pool module, that allows creating a pool of channels.

module-based-channel

Module-based channel

To implement a module-based gRPC channel, define a module that uses ConnGRPC.Channel.

defmodule DemoChannel do
  use ConnGRPC.Channel, address: "localhost:50051", opts: []
end

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 = [
      DemoChannel
    ]

    Supervisor.start_link(children, strategy: :one_for_one, name: Demo.Supervisor)
  end
end

To get the connection in your application, call:

DemoChannel.get()

It'll return either {:ok, channel} or {:error, :not_connected}.

channel-without-module

Channel without module

If you don't want to define for your channel, you can add ConnGRPC.Channel 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.Channel, name: :demo_channel, address: "localhost:50051", opts: []},
        id: :demo_channel
      )
    ]

    Supervisor.start_link(children, strategy: :one_for_one, name: Demo.Supervisor)
  end
end

To get the connection in your application, call:

ConnGRPC.Channel.get_channel(:demo_channel)

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 the gRPC channel

Starts and links process that keeps a persistent gRPC channel.

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

get(channel, opts \\ [])

View Source

Returns the gRPC channel

Starts and links process that keeps a persistent gRPC channel.

options

Options

  • :address - The gRPC server address. For more details, see GRPC.Stub.connect/2

  • :opts - Options for Elixir gRPC. For more details, see GRPC.Stub.connect/2

  • :name - A name to register the started process (see the :name option in GenServer.start_link/3)

  • :backoff - Minimum and maximum exponential backoff intervals (default: [min: 1000, max: 30_000])

  • :backoff_module - Backoff module to be used (default: ConnGRPC.Backoff.Exponential). If you'd like to implement your own backoff, see the ConnGRPC.Backoff behavior.

  • :debug - Write debug logs (default: false)

  • :on_connect - Function to run on connect (0-arity)

  • :on_disconnect - Function to run on disconnect (0-arity)

  • :grpc_stub - GRPC stub module that will receive the connect/2 call (default: GRPC.Stub)

  • :mock - A function that if provided, will override calls to get/1. It can be useful for mocking channel connection in parallel tests.