Skip to content

Commit

Permalink
use map syntax to pass ranch transport options
Browse files Browse the repository at this point in the history
  • Loading branch information
michalwski committed Jan 9, 2019
1 parent e8617e0 commit e5b007d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions doc/advanced-configuration/Listener-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ Unlike `ejabberd_c2s`, it doesn't use `ejabberd_receiver` or `ejabberd_listener`
* `ip` (IP tuple, default: `{0,0,0,0}`) - IP address to bind to.
* `num_acceptors` (positive integer, default: 100) - Number of acceptors.
* `transport_options` (proplist, default: []) - Ranch-specific transport options.
See [ranch:opt()](https://ninenines.eu/docs/en/ranch/1.5/manual/ranch/#_opt).
See [ranch:opt()](https://ninenines.eu/docs/en/ranch/1.7/manual/ranch/#_opt).
* `protocol_options` (proplist, default: []) - Protocol configuration options for Cowboy.
See [Cowboy HTTP module docs](https://ninenines.eu/docs/en/cowboy/2.4/manual/cowboy_http/).
See [Cowboy HTTP module docs](https://ninenines.eu/docs/en/cowboy/2.6/manual/cowboy_http/).
* `ssl` (list of ssl options, required for https, no default value) - If specified, https will be used.
Accepts all ranch_ssl options that don't take fun() parameters.
Only `certfile` and `keyfile` are mandatory.
Expand Down
35 changes: 17 additions & 18 deletions src/ejabberd_cowboy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ start_listener({Port, IP, tcp}=Listener, Opts) ->
transient, infinity, worker, [?MODULE]},
{ok, Pid} = supervisor:start_child(ejabberd_listeners, ChildSpec),
TransportOpts = gen_mod:get_opt(transport_options, Opts, []),
TransportOpts2 = [{port, Port}, {ip, IP} | TransportOpts],
TransportOpts3 = maybe_insert_max_connections(TransportOpts2, Opts),
Opts2 = gen_mod:set_opt(transport_options, Opts, TransportOpts3),
TransportOptsMap = maps:from_list(TransportOpts),
TransportOptsMap2 = TransportOptsMap#{socket_opts => [{port, Port}, {ip, IP}]},
TransportOptsMap3 = maybe_insert_max_connections(TransportOptsMap2, Opts),
Opts2 = gen_mod:set_opt(transport_options, Opts, TransportOptsMap3),
{ok, _} = start_cowboy(Ref, Opts2),
{ok, Pid}.

Expand Down Expand Up @@ -127,12 +128,13 @@ do_start_cowboy(Ref, Opts, Retries, SleepTime) ->
do_start_cowboy(Ref, Opts) ->
SSLOpts = gen_mod:get_opt(ssl, Opts, undefined),
NumAcceptors = gen_mod:get_opt(num_acceptors, Opts, 100),
TransportOpts = gen_mod:get_opt(transport_options, Opts, []),
TransportOpts0 = gen_mod:get_opt(transport_options, Opts, #{}),
TransportOpts = TransportOpts0#{numb_acceptros => NumAcceptors},
Modules = gen_mod:get_opt(modules, Opts),
Dispatch = cowboy_router:compile(get_routes(Modules)),
ProtocolOpts = [{env, [{dispatch, Dispatch}]} |
gen_mod:get_opt(protocol_options, Opts, [])],
case catch start_http_or_https(SSLOpts, Ref, NumAcceptors, TransportOpts, ProtocolOpts) of
case catch start_http_or_https(SSLOpts, Ref, TransportOpts, ProtocolOpts) of
{error, {{shutdown,
{failed_to_start_child, ranch_acceptors_sup,
{{badmatch, {error, eaddrinuse}}, _ }}}, _}} ->
Expand All @@ -141,23 +143,21 @@ do_start_cowboy(Ref, Opts) ->
Result
end.

start_http_or_https(undefined, Ref, NumAcceptors, TransportOpts, ProtocolOpts) ->
cowboy_start_http(Ref, NumAcceptors, TransportOpts, ProtocolOpts);
start_http_or_https(SSLOpts, Ref, NumAcceptors, TransportOpts, ProtocolOpts) ->
start_http_or_https(undefined, Ref, TransportOpts, ProtocolOpts) ->
cowboy_start_http(Ref, TransportOpts, ProtocolOpts);
start_http_or_https(SSLOpts, Ref, TransportOpts, ProtocolOpts) ->
SSLOptsWithVerifyFun = maybe_set_verify_fun(SSLOpts),
FilteredSSLOptions = filter_options(ignored_ssl_options(), SSLOptsWithVerifyFun),
TransportOptsWithSSL = TransportOpts ++ FilteredSSLOptions,
cowboy_start_https(Ref, NumAcceptors, TransportOptsWithSSL, ProtocolOpts).
SocketOptsWithSSL = maps:get(socket_opts, TransportOpts) ++ FilteredSSLOptions,
cowboy_start_https(Ref, TransportOpts#{socket_opts => SocketOptsWithSSL}, ProtocolOpts).

cowboy_start_http(Ref, NumAcceptors, TransportOpts, ProtocolOpts) ->
cowboy_start_http(Ref, TransportOpts, ProtocolOpts) ->
ProtoOpts = make_env_map(maps:from_list(ProtocolOpts)),
TransOpts = [{num_acceptors, NumAcceptors}|TransportOpts],
cowboy:start_clear(Ref, TransOpts, ProtoOpts).
cowboy:start_clear(Ref, TransportOpts, ProtoOpts).

cowboy_start_https(Ref, NumAcceptors, TransportOpts, ProtocolOpts) ->
cowboy_start_https(Ref, TransportOpts, ProtocolOpts) ->
ProtoOpts = make_env_map(maps:from_list(ProtocolOpts)),
TransOpts = [{num_acceptors, NumAcceptors}|TransportOpts],
cowboy:start_tls(Ref, TransOpts, ProtoOpts).
cowboy:start_tls(Ref, TransportOpts, ProtoOpts).

make_env_map(Map = #{env := Env}) ->
Map#{env => maps:from_list(Env)}.
Expand Down Expand Up @@ -253,6 +253,5 @@ maybe_insert_max_connections(TransportOpts, Opts) ->
undefined ->
TransportOpts;
Value ->
NewTuple = {Key, Value},
lists:keystore(Key, 1, TransportOpts, NewTuple)
TransportOpts#{Key => Value}
end.
7 changes: 4 additions & 3 deletions test/cowboy_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ mixed_requests(_Config) ->
Responses = lists:duplicate(50, {TextPong, true, TextPong, true}).

start_cowboy_returns_error_eaddrinuse(_C) ->
Opts = [{transport_options, [{port, 8088},
{ip, {127, 0, 0, 1}}]},
Opts = [{transport_options, #{socket_opts => [{port, 8088},
{ip, {127, 0, 0, 1}}]}},
{modules, []},
{retries, {2, 10}}],
{ok, _Pid} = ejabberd_cowboy:start_cowboy(a_ref, Opts),
Expand Down Expand Up @@ -274,7 +274,8 @@ start_cowboy() ->
]}]
}]),
{ok, _Pid} = cowboy:start_clear(http_listener,
[{num_acceptors, 20}, {port, 8080}],
#{num_acceptors => 20,
socket_opts => [{port, 8080}]},
#{env => #{dispatch => Dispatch}}).

stop_cowboy() ->
Expand Down

0 comments on commit e5b007d

Please sign in to comment.