Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Allow client settings opt on mint adapter #378

Merged
merged 4 commits into from
Jul 10, 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
34 changes: 30 additions & 4 deletions lib/grpc/client/adapters/mint.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule GRPC.Client.Adapters.Mint do
@moduledoc """
A client adapter using mint
A client adapter using Mint.
"""

alias GRPC.Channel
Expand All @@ -10,9 +10,25 @@ defmodule GRPC.Client.Adapters.Mint do

@behaviour GRPC.Client.Adapter

@default_connect_opts [protocols: [:http2]]
@default_connect_opts [
protocols: [:http2]
]
@default_client_settings [
initial_window_size: 8_000_000,
max_frame_size: 8_000_000
]
@default_transport_opts [timeout: :infinity]

@doc """
Connects using Mint based on the provided configs. Options
* `:transport_opts`: Defaults to `[timeout: :infinity]`, given the nature of H2 connections (with support to
long-lived streams) this default is set to avoid timeouts while waiting for server streams to complete. The other
options may vary based on the transport used for this connection (tcp or ssl). Check [Mint.HTTP.connect/4](https://hexdocs.pm/mint/Mint.HTTP.html#connect/4)
* `:client_settings`: Defaults to `[initial_window_size: 8_000_000, max_frame_size: 8_000_000]`, a larger default
window size ensures that the number of packages exchanges is smaller, thus speeding up the requests by reducing the
amount of networks round trip, with the cost of having larger packages reaching the server per connection.
Check [Mint.HTTP2.setting() type](https://hexdocs.pm/mint/Mint.HTTP2.html#t:setting/0) for additional configs.
"""
@impl true
def connect(%{host: host, port: port} = channel, opts \\ []) do
# Added :config_options to facilitate testing.
Expand Down Expand Up @@ -119,12 +135,22 @@ defmodule GRPC.Client.Adapters.Mint do
|> Keyword.get(:transport_opts, [])
|> Keyword.merge(ssl)

[transport_opts: Keyword.merge(@default_transport_opts, transport_opts)]
client_settings = Keyword.get(opts, :client_settings, @default_client_settings)

[
transport_opts: Keyword.merge(@default_transport_opts, transport_opts),
client_settings: client_settings
sleipnir marked this conversation as resolved.
Show resolved Hide resolved
]
end

defp connect_opts(_channel, opts) do
transport_opts = Keyword.get(opts, :transport_opts, [])
[transport_opts: Keyword.merge(@default_transport_opts, transport_opts)]
client_settings = Keyword.get(opts, :client_settings, @default_client_settings)

[
transport_opts: Keyword.merge(@default_transport_opts, transport_opts),
client_settings: client_settings
]
end

defp merge_opts(opts, module_opts) do
Expand Down
31 changes: 31 additions & 0 deletions test/grpc/client/adapters/mint_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,36 @@ defmodule GRPC.Client.Adapters.MintTest do

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

test "defaults client settings when none is passed", %{port: port} do
channel = build(:channel, adapter: Mint, port: port, host: "localhost")

assert {:ok, result} = Mint.connect(channel, [])
# wait for settings to be pushed
Process.sleep(50)
state = :sys.get_state(result.adapter_payload.conn_pid)

assert %{initial_window_size: 8_000_000, max_frame_size: 8_000_000} =
Map.get(state.conn, :client_settings)
end

test "allow client settings to be passed", %{port: port} do
channel = build(:channel, adapter: Mint, port: port, host: "localhost")

assert {:ok, result} =
Mint.connect(channel,
client_settings: [
initial_window_size: 50_000,
max_frame_size: 50_000
]
)

# wait for settings to be pushed
Process.sleep(50)
state = :sys.get_state(result.adapter_payload.conn_pid)

assert %{initial_window_size: 50_000, max_frame_size: 50_000} =
Map.get(state.conn, :client_settings)
end
end
end
Loading