From 80c8f69e5d020bed19303c2957634ec9a6546d6d Mon Sep 17 00:00:00 2001 From: Noah Betzen Date: Mon, 13 Mar 2023 10:56:29 -0700 Subject: [PATCH 1/6] feat: add support for ranch_tcp socket opts --- lib/grpc/server/adapters/cowboy.ex | 17 +++++++- test/grpc/server/adapters/cowboy_test.exs | 49 +++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/grpc/server/adapters/cowboy_test.exs diff --git a/lib/grpc/server/adapters/cowboy.ex b/lib/grpc/server/adapters/cowboy.ex index 51caa7f0..f1834b4f 100644 --- a/lib/grpc/server/adapters/cowboy.ex +++ b/lib/grpc/server/adapters/cowboy.ex @@ -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) if opts[:cred] do opts[:cred].ssl ++ diff --git a/test/grpc/server/adapters/cowboy_test.exs b/test/grpc/server/adapters/cowboy_test.exs new file mode 100644 index 00000000..2345fce8 --- /dev/null +++ b/test/grpc/server/adapters/cowboy_test.exs @@ -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 From f340b67c67270f9d90f417f74cb6cee197fe7007 Mon Sep 17 00:00:00 2001 From: Paulo Valente <16843419+polvalente@users.noreply.github.com> Date: Tue, 14 Mar 2023 03:20:46 -0300 Subject: [PATCH 2/6] Update lib/grpc/server/adapters/cowboy.ex --- lib/grpc/server/adapters/cowboy.ex | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/lib/grpc/server/adapters/cowboy.ex b/lib/grpc/server/adapters/cowboy.ex index f1834b4f..d7eee3ad 100644 --- a/lib/grpc/server/adapters/cowboy.ex +++ b/lib/grpc/server/adapters/cowboy.ex @@ -223,19 +223,11 @@ defmodule GRPC.Server.Adapters.Cowboy do socket_opts = [port: port] # 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 + 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] + _, acc -> acc end) if opts[:cred] do From ba8c1658db11e6056a2e96d9853bcca1369e734e Mon Sep 17 00:00:00 2001 From: Paulo Valente <16843419+polvalente@users.noreply.github.com> Date: Tue, 14 Mar 2023 03:34:24 -0300 Subject: [PATCH 3/6] Update lib/grpc/server/adapters/cowboy.ex --- lib/grpc/server/adapters/cowboy.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/grpc/server/adapters/cowboy.ex b/lib/grpc/server/adapters/cowboy.ex index d7eee3ad..1993fee1 100644 --- a/lib/grpc/server/adapters/cowboy.ex +++ b/lib/grpc/server/adapters/cowboy.ex @@ -223,7 +223,7 @@ defmodule GRPC.Server.Adapters.Cowboy do socket_opts = [port: port] # https://ninenines.eu/docs/en/ranch/1.7/manual/ranch_tcp/ - socket_opts = + 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] From efe0674d9189c2df81ae4963f1b7f9629fa1fe86 Mon Sep 17 00:00:00 2001 From: Noah Betzen Date: Tue, 14 Mar 2023 02:12:50 -0700 Subject: [PATCH 4/6] Update Elixir versions for asdf and mix project --- .tool-versions | 4 ++-- mix.exs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.tool-versions b/.tool-versions index d905c6c8..e4f68f14 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,2 +1,2 @@ -elixir 1.13.3-otp-25 -erlang 25.0.3 +elixir 1.14.1-otp-25 +erlang 25.1.1 diff --git a/mix.exs b/mix.exs index 8d8fbe74..366405b1 100644 --- a/mix.exs +++ b/mix.exs @@ -7,7 +7,7 @@ defmodule GRPC.Mixfile do [ app: :grpc, version: @version, - elixir: "~> 1.11", + elixir: "~> 1.14", elixirc_paths: elixirc_paths(Mix.env()), build_embedded: Mix.env() == :prod, start_permanent: Mix.env() == :prod, From 341f9289d829eaaff5e2357388f8db12656d69c1 Mon Sep 17 00:00:00 2001 From: Noah Betzen Date: Tue, 14 Mar 2023 02:13:32 -0700 Subject: [PATCH 5/6] Format --- lib/grpc/server/adapters/cowboy.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/grpc/server/adapters/cowboy.ex b/lib/grpc/server/adapters/cowboy.ex index 1993fee1..61f79f9c 100644 --- a/lib/grpc/server/adapters/cowboy.ex +++ b/lib/grpc/server/adapters/cowboy.ex @@ -224,7 +224,7 @@ defmodule GRPC.Server.Adapters.Cowboy do # https://ninenines.eu/docs/en/ranch/1.7/manual/ranch_tcp/ socket_opts = - Enum.reduce(opts, socket_opts, fn + 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] _, acc -> acc From 82afb6fc42ee556d4e15fd09aeecb98328696999 Mon Sep 17 00:00:00 2001 From: Paulo Valente <16843419+polvalente@users.noreply.github.com> Date: Tue, 14 Mar 2023 08:51:19 -0300 Subject: [PATCH 6/6] Update mix.exs --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index 366405b1..8d8fbe74 100644 --- a/mix.exs +++ b/mix.exs @@ -7,7 +7,7 @@ defmodule GRPC.Mixfile do [ app: :grpc, version: @version, - elixir: "~> 1.14", + elixir: "~> 1.11", elixirc_paths: elixirc_paths(Mix.env()), build_embedded: Mix.env() == :prod, start_permanent: Mix.env() == :prod,