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

Add support for ranch_tcp opts: ip, ipv6_v6only, inet, inet6 #308

Merged
merged 6 commits into from
Mar 14, 2023
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
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.13.3-otp-25
erlang 25.0.3
elixir 1.14.1-otp-25
erlang 25.1.1
9 changes: 8 additions & 1 deletion lib/grpc/server/adapters/cowboy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,14 @@ defmodule GRPC.Server.Adapters.Cowboy do

defp socket_opts(port, opts) do
socket_opts = [port: port]
socket_opts = if opts[:ip], do: [{:ip, opts[:ip]} | socket_opts], else: socket_opts

# https://ninenines.eu/docs/en/ranch/1.7/manual/ranch_tcp/
socket_opts =
Enum.reduce(opts, socket_opts, fn
{k, v}, acc when k in [:ip, :ipv6_v6only] and not is_nil(v) -> [{k, v} | acc]
{:net, v}, acc when not is_nil(v) -> [v | acc]
Nezteb marked this conversation as resolved.
Show resolved Hide resolved
_, acc -> acc
end)

if opts[:cred] do
opts[:cred].ssl ++
Expand Down
49 changes: 49 additions & 0 deletions test/grpc/server/adapters/cowboy_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
defmodule GRPC.Server.Adapters.CowboyTest do
use ExUnit.Case, async: false

alias GRPC.Server.Adapters.Cowboy

describe "child_spec/4" do
test "produces the correct socket opts for ranch_tcp for inet" do
spec =
Cowboy.child_spec(:endpoint, [], 8080, [
{:foo, :bar},
{:ip, {127, 0, 0, 1}},
{:ipv6_v6only, false},
{:net, :inet},
{:baz, :foo}
])

socket_opts = get_socket_opts_from_child_spec(spec)
assert socket_opts == [:inet, {:ipv6_v6only, false}, {:ip, {127, 0, 0, 1}}, {:port, 8080}]
end

test "produces the correct socket opts for ranch_tcp for inet6" do
spec =
Cowboy.child_spec(:endpoint, [], 8081, [
{:foo, :bar},
{:ip, {0, 0, 0, 0, 0, 0, 0, 1}},
{:ipv6_v6only, true},
{:net, :inet6},
{:baz, :foo}
])

socket_opts = get_socket_opts_from_child_spec(spec)

assert socket_opts == [
:inet6,
{:ipv6_v6only, true},
{:ip, {0, 0, 0, 0, 0, 0, 0, 1}},
{:port, 8081}
]
end
end

defp get_socket_opts_from_child_spec(spec) do
{_Cowboy, _start_link, start_opts} = spec.start
[_http, _endpoint, _empty_list, ranch_listener_call] = start_opts
{_ranch_listener_sup, _start_link, ranch_listener_opts} = ranch_listener_call
[_endpoint, _ranch_tcp, transport_opts, _cowboy_clear, _opts_map] = ranch_listener_opts
transport_opts.socket_opts
end
end