feat(exhook): expose tcp some options for grpc client

This commit is contained in:
firest 2022-06-24 14:45:15 +08:00
parent 7a3941ce5d
commit abe2a9cb45
4 changed files with 59 additions and 5 deletions

View File

@ -47,3 +47,20 @@ exhook.server.default.url = http://127.0.0.1:9000
#exhook.server.default.ssl.cacertfile = {{ platform_etc_dir }}/certs/cacert.pem
#exhook.server.default.ssl.certfile = {{ platform_etc_dir }}/certs/cert.pem
#exhook.server.default.ssl.keyfile = {{ platform_etc_dir }}/certs/key.pem
## Enables/disables periodic transmission on a connected socket when no other data is exchanged.
## If the other end does not respond, the connection is considered broken and an error message is sent to the controlling process.
##
## Default: true
#exhook.server.default.socket_options.keepalive = true
## If true, option TCP_NODELAY is turned on for the socket, which means that also small amounts of data are sent immediately.
##
## Default: true
#exhook.server.default.socket_options.nodelay = true
## The minimum size of the receive buffer to use for the socket.
#exhook.server.default.socket_options.recbuf = 64KB
## The minimum size of the send buffer to use for the socket.
#exhook.server.default.socket_options.sndbuf = 16KB

View File

@ -47,12 +47,42 @@ end}.
{datatype, string}
]}.
{mapping, "exhook.server.$name.socket_options.keepalive", "emqx_exhook.servers", [
{default, true},
{datatype, {enum, [true, false]}}
]}.
{mapping, "exhook.server.$name.socket_options.nodelay", "emqx_exhook.servers", [
{default, true},
{datatype, {enum, [true, false]}}
]}.
{mapping, "exhook.server.$name.socket_options.recbuf", "emqx_exhook.servers", [
{datatype, bytesize}
]}.
{mapping, "exhook.server.$name.socket_options.sndbuf", "emqx_exhook.servers", [
{datatype, bytesize}
]}.
{translation, "emqx_exhook.servers", fun(Conf) ->
Filter = fun(Opts) -> [{K, V} || {K, V} <- Opts, V =/= undefined] end,
MkSockOpts = fun(Prefix, Conf) ->
{socket_options,
Filter([{Opt,
cuttlefish:conf_get(Prefix ++ ".socket_options." ++ atom_to_list(Opt),
Conf,
undefined)}
|| Opt <- [keepalive, nodelay, recbuf, sndbuf]])}
end,
ServerOptions = fun(Prefix) ->
case http_uri:parse(cuttlefish:conf_get(Prefix ++ ".url", Conf)) of
{ok, {http, _, Host, Port, _, _}} ->
[{scheme, http}, {host, Host}, {port, Port}];
[{scheme, http}, {host, Host}, {port, Port},
MkSockOpts(Prefix, Conf)
];
{ok, {https, _, Host, Port, _, _}} ->
[{scheme, https}, {host, Host}, {port, Port},
{ssl_options,
@ -60,10 +90,13 @@ end}.
{certfile, cuttlefish:conf_get(Prefix ++ ".ssl.certfile", Conf, undefined)},
{keyfile, cuttlefish:conf_get(Prefix ++ ".ssl.keyfile", Conf, undefined)},
{cacertfile, cuttlefish:conf_get(Prefix ++ ".ssl.cacertfile", Conf, undefined)}
])}];
])},
MkSockOpts(Prefix, Conf)
];
_ -> error(invalid_server_options)
end
end,
[{list_to_atom(Name), ServerOptions("exhook.server." ++ Name)}
|| {["exhook", "server", Name, "url"], _} <- cuttlefish_variable:filter_by_prefix("exhook.server", Conf)]
end}.

View File

@ -124,13 +124,16 @@ channel_opts(Opts) ->
Host = proplists:get_value(host, Opts),
Port = proplists:get_value(port, Opts),
SvrAddr = format_http_uri(Scheme, Host, Port),
SockOpts = proplists:get_value(socket_options, Opts),
ClientOpts = case Scheme of
https ->
SslOpts = lists:keydelete(ssl, 1, proplists:get_value(ssl_options, Opts, [])),
#{gun_opts =>
#{transport => ssl,
transport_opts => SslOpts}};
_ -> #{}
transport_opts => SockOpts ++ SslOpts}};
_ ->
#{gun_opts =>
#{transport_opts => SockOpts}}
end,
{SvrAddr, ClientOpts}.

View File

@ -46,7 +46,8 @@ set_special_cfgs(emqx) ->
application:set_env(emqx, allow_anonymous, false),
application:set_env(emqx, enable_acl_cache, false),
application:set_env(emqx, plugins_loaded_file, undefined),
application:set_env(emqx, modules_loaded_file, undefined);
application:set_env(emqx, modules_loaded_file, undefined),
application:set_env(ekka, cluster_name, ?OTHER_CLUSTER_NAME_ATOM);
set_special_cfgs(emqx_exhook) ->
ok.