Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow configurable Mint adapter #370

Merged
merged 7 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,26 @@ iex> {:ok, channel} = GRPC.Stub.connect("localhost:50051", interceptors: [GRPC.C

Check the [examples](examples) and [interop](interop) directories in the project's source code for some examples.

## Client Adapter and Configuration

The default adapter used by `GRPC.Stub.connect/2` is `GRPC.Client.Adapter.Gun`. Another option is to use `GRPC.Client.Adapters.Mint` instead, like so:

```
GRPC.Stub.connect("localhost:50051",
# Use Mint adapter instead of default Gun
adapter: GRPC.Client.Adapters.Mint
)
```

The `GRPC.Client.Adapters.Mint` adapter accepts custom configuration. To do so, you can configure it from your mix application via:

```
// File: your application's config file.
config :grpc, GRPC.Client.Adapters.Mint, custom_opts
```

The accepted options for configuration are the ones listed on [Mint.HTTP.connect/4](https://hexdocs.pm/mint/Mint.HTTP.html#connect/4-options)

## Features

- Various kinds of RPC:
Expand Down
12 changes: 11 additions & 1 deletion lib/grpc/client/adapters/mint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ defmodule GRPC.Client.Adapters.Mint do

@impl true
def connect(%{host: host, port: port} = channel, opts \\ []) do
opts = Keyword.merge(@default_connect_opts, connect_opts(channel, opts))
# Added :config_options to facilitate testing.
{config_opts, opts} = Keyword.pop(opts, :config_options, [])
module_opts = Application.get_env(:grpc, __MODULE__, config_opts)

opts = connect_opts(channel, opts) |> merge_opts(module_opts)

Process.flag(:trap_exit, true)

channel
Expand Down Expand Up @@ -122,6 +127,11 @@ defmodule GRPC.Client.Adapters.Mint do
[transport_opts: Keyword.merge(@default_transport_opts, transport_opts)]
end

defp merge_opts(opts, module_opts) do
opts = Keyword.merge(opts, module_opts)
Keyword.merge(@default_connect_opts, opts)
end

defp mint_scheme(%Channel{scheme: "https"} = _channel), do: :https
defp mint_scheme(_channel), do: :http

Expand Down
19 changes: 17 additions & 2 deletions test/grpc/client/adapters/mint_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ defmodule GRPC.Client.Adapters.MintTest do
end

test "connects insecurely (default options)", %{port: port} do
channel = build(:channel, port: port, host: "localhost")
channel = build(:channel, adapter: Mint, port: port, host: "localhost")

assert {:ok, result} = Mint.connect(channel, [])
assert %{channel | adapter_payload: %{conn_pid: result.adapter_payload.conn_pid}} == result
end

test "connects insecurely (custom options)", %{port: port} do
channel = build(:channel, port: port, host: "localhost")
channel = build(:channel, adapter: Mint, port: port, host: "localhost")

assert {:ok, result} = Mint.connect(channel, transport_opts: [ip: :loopback])
assert %{channel | adapter_payload: %{conn_pid: result.adapter_payload.conn_pid}} == result
Expand All @@ -32,5 +32,20 @@ defmodule GRPC.Client.Adapters.MintTest do

assert message == "Error while opening connection: {:error, :badarg}"
end

test "accepts config_options for application specific configuration", %{port: port} do
channel = build(:channel, adapter: Mint, port: port, host: "localhost")

assert {:ok, result} =
Mint.connect(channel, config_options: [transport_opts: [ip: :loopback]])

assert %{channel | adapter_payload: %{conn_pid: result.adapter_payload.conn_pid}} == result

# Ensure that changing one of the options via config_options also breaks things
assert {:error, message} =
Mint.connect(channel, config_options: [transport_opts: [ip: "256.0.0.0"]])

assert message == "Error while opening connection: {:error, :badarg}"
end
end
end
Loading