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

Provide a mechanism to inject a custom status handler #171

Merged
Show file tree
Hide file tree
Changes from 5 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
19 changes: 15 additions & 4 deletions lib/grpc/adapter/cowboy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,22 @@ defmodule GRPC.Adapter.Cowboy do
end

defp cowboy_start_args(endpoint, servers, port, opts) do
dispatch =
:cowboy_router.compile([
{:_, [{:_, GRPC.Adapter.Cowboy.Handler, {endpoint, servers, Enum.into(opts, %{})}}]}
])
# Custom handler to be able to listen in the same port, more info:
# https://github.com/containous/traefik/issues/6211
{adapter_opts, opts} = Keyword.pop(opts, :adapter_opts, [])
status_handler = Keyword.get(adapter_opts, :status_handler)

handlers =
if status_handler do
[
status_handler,
{:_, GRPC.Adapter.Cowboy.Handler, {endpoint, servers, Enum.into(opts, %{})}}
]
else
[{:_, GRPC.Adapter.Cowboy.Handler, {endpoint, servers, Enum.into(opts, %{})}}]
end

dispatch = :cowboy_router.compile([{:_, handlers}])
idle_timeout = Keyword.get(opts, :idle_timeout, :infinity)
num_acceptors = Keyword.get(opts, :num_acceptors, @default_num_acceptors)
max_connections = Keyword.get(opts, :max_connections, @default_max_connections)
Expand Down
3 changes: 3 additions & 0 deletions lib/grpc/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ defmodule GRPC.Server do
# * `:cred` - a credential created by functions of `GRPC.Credential`,
# an insecure server will be created without this option
# * `:adapter` - use a custom server adapter instead of default `GRPC.Adapter.Cowboy`
# * `:adapter_opts` - configuration for the specified adapter.
# * `:status_handler` - adds a status handler that could be listening on HTTP/1, if necessary.
# It should follow the format defined by cowboy_router:compile/3
@doc false
@spec start(servers_list, non_neg_integer, Keyword.t()) :: {atom, any, non_neg_integer}
def start(servers, port, opts \\ []) do
Expand Down
27 changes: 27 additions & 0 deletions test/grpc/integration/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ defmodule GRPC.Integration.ServerTest do
end
end

defmodule HTTP1Server do
def init(req, state) do
{:ok, :cowboy_req.reply(200, %{}, "OK", req), state}
end
end

test "multiple servers works" do
run_server([FeatureServer, HelloServer], fn port ->
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
Expand All @@ -111,6 +117,27 @@ defmodule GRPC.Integration.ServerTest do
end)
end

test "HTTP/1 status handler can be started along a gRPC server" do
status_handler = {"/status", HTTP1Server, []}

run_server(
[HelloServer],
fn port ->
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
req = Helloworld.HelloRequest.new(name: "Elixir")
{:ok, reply} = channel |> Helloworld.Greeter.Stub.say_hello(req)
assert reply.message == "Hello, Elixir"

{:ok, conn_pid} = :gun.open('localhost', port)
stream_ref = :gun.get(conn_pid, "/status")

assert_receive {:gun_response, ^conn_pid, ^stream_ref, :nofin, 200, _headers}
end,
0,
adapter_opts: [status_handler: status_handler]
)
end

test "returns appropriate error for unary requests" do
run_server([HelloErrorServer], fn port ->
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
Expand Down
4 changes: 2 additions & 2 deletions test/support/integration_test_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ defmodule GRPC.Integration.TestCase do
end
end

def run_server(servers, func, port \\ 0) do
{:ok, _pid, port} = GRPC.Server.start(servers, port)
def run_server(servers, func, port \\ 0, opts \\ []) do
{:ok, _pid, port} = GRPC.Server.start(servers, port, opts)

try do
func.(port)
Expand Down