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 1 commit
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
22 changes: 19 additions & 3 deletions lib/grpc/client/adapters/mint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ 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]

@impl true
Expand Down Expand Up @@ -119,12 +125,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
]
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