Skip to content

Commit 6bbc6c7

Browse files
authored
Merge pull request #370 from caike/allow-configurable-mint-adapter
Allow configurable Mint adapter
2 parents 4526427 + 59d5ec4 commit 6bbc6c7

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,26 @@ iex> {:ok, channel} = GRPC.Stub.connect("localhost:50051", interceptors: [GRPC.C
205205

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

208+
## Client Adapter and Configuration
209+
210+
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:
211+
212+
```
213+
GRPC.Stub.connect("localhost:50051",
214+
# Use Mint adapter instead of default Gun
215+
adapter: GRPC.Client.Adapters.Mint
216+
)
217+
```
218+
219+
The `GRPC.Client.Adapters.Mint` adapter accepts custom configuration. To do so, you can configure it from your mix application via:
220+
221+
```
222+
// File: your application's config file.
223+
config :grpc, GRPC.Client.Adapters.Mint, custom_opts
224+
```
225+
226+
The accepted options for configuration are the ones listed on [Mint.HTTP.connect/4](https://hexdocs.pm/mint/Mint.HTTP.html#connect/4-options)
227+
208228
## Features
209229

210230
- Various kinds of RPC:

lib/grpc/client/adapters/mint.ex

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ defmodule GRPC.Client.Adapters.Mint do
1515

1616
@impl true
1717
def connect(%{host: host, port: port} = channel, opts \\ []) do
18-
opts = Keyword.merge(@default_connect_opts, connect_opts(channel, opts))
18+
# Added :config_options to facilitate testing.
19+
{config_opts, opts} = Keyword.pop(opts, :config_options, [])
20+
module_opts = Application.get_env(:grpc, __MODULE__, config_opts)
21+
22+
opts = connect_opts(channel, opts) |> merge_opts(module_opts)
23+
1924
Process.flag(:trap_exit, true)
2025

2126
channel
@@ -122,6 +127,11 @@ defmodule GRPC.Client.Adapters.Mint do
122127
[transport_opts: Keyword.merge(@default_transport_opts, transport_opts)]
123128
end
124129

130+
defp merge_opts(opts, module_opts) do
131+
opts = Keyword.merge(opts, module_opts)
132+
Keyword.merge(@default_connect_opts, opts)
133+
end
134+
125135
defp mint_scheme(%Channel{scheme: "https"} = _channel), do: :https
126136
defp mint_scheme(_channel), do: :http
127137

test/grpc/client/adapters/mint_test.exs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ defmodule GRPC.Client.Adapters.MintTest do
1515
end
1616

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

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

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

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

3333
assert message == "Error while opening connection: {:error, :badarg}"
3434
end
35+
36+
test "accepts config_options for application specific configuration", %{port: port} do
37+
channel = build(:channel, adapter: Mint, port: port, host: "localhost")
38+
39+
assert {:ok, result} =
40+
Mint.connect(channel, config_options: [transport_opts: [ip: :loopback]])
41+
42+
assert %{channel | adapter_payload: %{conn_pid: result.adapter_payload.conn_pid}} == result
43+
44+
# Ensure that changing one of the options via config_options also breaks things
45+
assert {:error, message} =
46+
Mint.connect(channel, config_options: [transport_opts: [ip: "256.0.0.0"]])
47+
48+
assert message == "Error while opening connection: {:error, :badarg}"
49+
end
3550
end
3651
end

0 commit comments

Comments
 (0)