MSPF-622: add get_addr/2 for woody_server (#148)

This commit is contained in:
Alexey 2020-11-23 16:50:50 +03:00 committed by GitHub
parent 3b76622054
commit 58f56b4624
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 2 deletions

View File

@ -5,6 +5,7 @@
%% API
-export([child_spec/2]).
-export([get_addr/2]).
%% Types
-type options() :: #{
@ -21,6 +22,7 @@
%% Behaviour definition
-callback child_spec(_Id, options()) -> supervisor:child_spec().
-callback get_addr(_Id) -> {inet:ip_address(), inet:port_number()}.
%%
%% API
@ -30,3 +32,9 @@
child_spec(Id, Options) ->
ProtocolHandler = woody_util:get_protocol_handler(server, Options),
ProtocolHandler:child_spec(Id, Options).
-spec get_addr(_Id, options()) ->
{inet:ip_address(), inet:port_number()}.
get_addr(Id, Options) ->
ProtocolHandler = woody_util:get_protocol_handler(server, Options),
ProtocolHandler:get_addr(Id).

View File

@ -9,6 +9,7 @@
%% woody_server callback
-export([child_spec/2]).
-export([get_addr/1]).
%% API
-export([get_routes/1]).
@ -112,11 +113,19 @@
child_spec(Id, Opts) ->
{Transport, TransportOpts} = get_socket_transport(Opts),
CowboyOpts = get_cowboy_config(Opts),
RanchRef = {?MODULE, Id},
RanchRef = create_ranch_ref(Id),
DrainSpec = make_drain_childspec(RanchRef, Opts),
RanchSpec = ranch:child_spec(RanchRef, Transport, TransportOpts, cowboy_clear, CowboyOpts),
make_server_childspec(Id, [RanchSpec, DrainSpec]).
-spec get_addr(atom()) ->
{inet:ip_address(), inet:port_number()}.
get_addr(Id) ->
ranch:get_addr(create_ranch_ref(Id)).
create_ranch_ref(Id) ->
{?MODULE, Id}.
make_drain_childspec(Ref, Opts) ->
ShutdownTimeout = maps:get(shutdown_timeout, Opts, ?DEFAULT_SHUTDOWN_TIMEOUT),
DrainOpts = #{shutdown => ShutdownTimeout, ranch_ref => Ref},

View File

@ -8,6 +8,7 @@
%% woody_server callback
-export([child_spec/2]).
-export([get_addr/1]).
%% API
-export([get_routes/1]).
@ -120,11 +121,19 @@
child_spec(Id, Opts) ->
{Transport, TransportOpts} = get_socket_transport(Opts),
CowboyOpts = get_cowboy_config(Opts),
RanchRef = {?MODULE, Id},
RanchRef = create_ranch_ref(Id),
DrainSpec = make_drain_childspec(RanchRef, Opts),
RanchSpec = ranch:child_spec(RanchRef, Transport, TransportOpts, cowboy_clear, CowboyOpts),
make_server_childspec(Id, [RanchSpec, DrainSpec]).
-spec get_addr(atom()) ->
{inet:ip_address(), inet:port_number()}.
get_addr(Id) ->
ranch:get_addr(create_ranch_ref(Id)).
create_ranch_ref(Id) ->
{?MODULE, Id}.
make_drain_childspec(Ref, Opts) ->
ShutdownTimeout = maps:get(shutdown_timeout, Opts, ?DEFAULT_SHUTDOWN_TIMEOUT),
DrainOpts = #{shutdown => ShutdownTimeout, ranch_ref => Ref},