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

feat(connection): configurable TCP keepalive #10933

Merged

Conversation

paulozulato
Copy link
Contributor

@paulozulato paulozulato commented Jun 2, 2023

Fixes https://emqx.atlassian.net/browse/EMQX-9852

Summary

🤖 Generated by Copilot at 7f73c08

This pull request adds support for TCP keepalive on Mac OS and configurable TCP keepalive options for listeners. It modifies the emqx_connection, emqx_schema, emqx_gateway_api_listeners, and emqx_listeners modules, as well as the emqx_mqtt_SUITE test case and the emqx_schema.hocon file. It also fixes some socket errors and refactors some code for readability and consistency.

PR Checklist

Please convert it to a draft if any of the following conditions are not met. Reviewers may skip over until all the items are checked:

  • Added tests for the changes
  • Changed lines covered in coverage report
  • Change log has been added to changes/{ce,ee}/(feat|perf|fix)-<PR-id>.en.md files
  • For internal contributor: there is a jira ticket to track this change
  • If there should be document changes, a PR to emqx-docs.git is sent, or a jira ticket is created to follow up
  • Schema changes are backward compatible

Checklist for CI (.github/workflows) changes

  • If changed package build workflow, pass this action (manual trigger)
  • Change log has been added to changes/ dir for user-facing artifacts update

@paulozulato paulozulato force-pushed the feat-configurable-tcp-keepalive branch from 7f73c08 to e5c7669 Compare June 2, 2023 21:23
@paulozulato paulozulato marked this pull request as ready for review June 2, 2023 22:33
@paulozulato paulozulato requested review from a team and lafirest as code owners June 2, 2023 22:33
sc(
string(),
#{
default => "none",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default => "none",
default => <<"none">>,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

@@ -353,6 +367,20 @@ init_state(
false -> disabled
end,
IdleTimeout = emqx_channel:get_mqtt_conf(Zone, idle_timeout),

{ListenerType, ListenerId} = Listener,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe move this hunk to a small function (take get_active_n function as an example).

also, we should not try to set tcp-ka for QUIC connections.

ok = maybe_set_tcp_keepalive(Listener),
maybe_set_tcp_keepalive({quic, _}) -> ok;
maybe_set_tcp_keepalive({Type, Id}) ->
    Conf = mqx_config:get_listener_conf(ListenerType, ListenerId, [tcp_options, keepalive], <<"none">>),
    case iolist_to_binary(Conf) of
        <<"none">> ->
            ok;
        Value ->
            %% the value is already validated by schema, so we do not validate it again.
            {Idle, Interval, Probes} = emqx_schema:parse_tcp_keepalive(Value),
            async_set_keepalive(Idle, Interfval, Probe)
    end.
-module(emqx_schema).
-export([parse_tcp_keepalive/1]).

%% @doc This function is used as value validator and also run-time parser.
parse_tcp_keepalive(Str) ->
    try
        [Idle, Interval, Probes]  = binary:split(iolist_to_binary(Str), <<",">>, [global]),
        %% use 10 times the Linux defaults as range limit
        IdleInt = parse_ka_int(Idle, "Idle", 1, 7200_0),
        IntervalInt = parse_ka_int(Interval, "Interva",  75_0),
        ProbesInt = parse_ka_int(Probe, "Probes", 1, 9_0),
        {IdleInt, IntervalInt, ProbesInt}
    catch
        error:_ ->
           throw(#{reason => “Not comma separated positive integers of 'Idle,Interval,Probes' format", value => Str})
    end.

parse_ka_int(Bin, Name, Min, Max) ->
    I = binary_to_integer(Bin),
    case I >= Min andalso I =< Max of
        true ->
            I;
        false ->
            Msg = io_lib:format("TCP-Keepalive '~s' value must be in the rage of [~p, ~p].", [Name, Min, Max]),
            throw(#{reason => lists:flatten(Msg), value => I}
    end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

Comment on lines 2791 to 2892
case Value of
"none" ->
ok;
_ ->
try
[IdleStr, IntervalStr, ProbesStr] = string:tokens(Value, ", "),
_ = list_to_integer(IdleStr),
_ = list_to_integer(IntervalStr),
_ = list_to_integer(ProbesStr),
ok
catch
_:_ ->
{error, "value does not contain 3 comma-separate integer values"}
end
end.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case Value of
"none" ->
ok;
_ ->
try
[IdleStr, IntervalStr, ProbesStr] = string:tokens(Value, ", "),
_ = list_to_integer(IdleStr),
_ = list_to_integer(IntervalStr),
_ = list_to_integer(ProbesStr),
ok
catch
_:_ ->
{error, "value does not contain 3 comma-separate integer values"}
end
end.
case iolist_to_binary(Value) of
<<"none">> ->
ok;
_ ->
_ = parse_tcp_keealive(Value),
ok
end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

@paulozulato paulozulato force-pushed the feat-configurable-tcp-keepalive branch from e5c7669 to 18aefb5 Compare June 5, 2023 18:28
@coveralls
Copy link
Collaborator

coveralls commented Jun 5, 2023

Pull Request Test Coverage Report for Build 5194657467

  • 7 of 18 (38.89%) changed or added relevant lines in 1 file are covered.
  • 42 unchanged lines in 18 files lost coverage.
  • Overall coverage decreased (-0.04%) to 81.675%

Changes Missing Coverage Covered Lines Changed/Added Lines %
apps/emqx/src/emqx_connection.erl 7 18 38.89%
Files with Coverage Reduction New Missed Lines %
apps/emqx_authn/src/enhanced_authn/emqx_enhanced_authn_scram_mnesia.erl 1 85.29%
apps/emqx_gateway_exproto/src/emqx_exproto_gcli.erl 1 64.15%
apps/emqx_gateway/src/emqx_gateway_schema.erl 1 88.89%
apps/emqx/src/emqx_broker.erl 1 88.2%
apps/emqx/src/emqx_channel.erl 1 87.69%
apps/emqx/src/emqx_cm.erl 1 92.47%
apps/emqx/src/emqx_config_handler.erl 1 95.57%
apps/emqx/src/emqx_flapping.erl 1 85.45%
apps/emqx/src/emqx_limiter/src/emqx_limiter_schema.erl 1 81.48%
apps/emqx/src/emqx_router_helper.erl 1 87.04%
Totals Coverage Status
Change from base Build 5189817718: -0.04%
Covered Lines: 29105
Relevant Lines: 35635

💛 - Coveralls

@paulozulato paulozulato force-pushed the feat-configurable-tcp-keepalive branch from 18aefb5 to 00ce9b2 Compare June 6, 2023 00:02
@paulozulato paulozulato changed the base branch from master to release-51 June 6, 2023 00:02
@paulozulato paulozulato changed the base branch from release-51 to master June 7, 2023 00:24
@paulozulato paulozulato force-pushed the feat-configurable-tcp-keepalive branch 2 times, most recently from 985b602 to fce4f1b Compare June 7, 2023 12:42
@paulozulato paulozulato force-pushed the feat-configurable-tcp-keepalive branch from fce4f1b to a440776 Compare June 7, 2023 13:24
@paulozulato paulozulato merged commit 63d4209 into emqx:master Jun 7, 2023
@paulozulato paulozulato deleted the feat-configurable-tcp-keepalive branch June 7, 2023 14:30
@yanzhiemq
Copy link
Collaborator

yanzhiemq commented Jun 13, 2023

Enhancements

  • Added support for configuring TCP keep-alive in MQTT/TCP and MQTT/SSL listeners.

@yanzhiemq
Copy link
Collaborator

yanzhiemq commented Jun 13, 2023

增强

  • 支持在 MQTT/TCP 和 MQTT/SSL 监听器中配置 TCP keep-alive。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants