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 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
17 changes: 16 additions & 1 deletion lib/grpc/server/adapters/cowboy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,22 @@ 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/
allowed_ranch_opts = %{
ip: {:ip, opts[:ip]},
ipv6_v6only: {:ipv6_v6only, opts[:ipv6_v6only]},
net: opts[:net]
}

socket_opts =
Enum.reduce(allowed_ranch_opts, socket_opts, fn {key, value}, acc ->
if opts[key] != nil do
[value | acc]
else
acc
end
end)
polvalente marked this conversation as resolved.
Show resolved Hide resolved

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