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

fix: do not consume limiter token if failed to accept #192

Closed
Show file tree
Hide file tree
Changes from all 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
17 changes: 10 additions & 7 deletions src/esockd_acceptor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ accepting(info, {inet_async, LSock, Ref, {ok, Sock}},
%% Inc accepted stats.
esockd_server:inc_stats({Proto, ListenOn}, accepted, 1),

case eval_tune_socket_fun(TuneFun, Sock) of
{ok, Sock} ->
Result = case eval_tune_socket_fun(TuneFun, Sock) of
{ok, _} ->
case esockd_connection_sup:start_connection(ConnSup, Sock, UpgradeFuns) of
{ok, _Pid} -> ok;
{ok, _Pid} ->
consume_limiter;
{error, enotconn} ->
close(Sock); %% quiet...issue #10
{error, einval} ->
Expand All @@ -140,8 +141,7 @@ accepting(info, {inet_async, LSock, Ref, {ok, Sock}},
[esockd:format(Sockname), Reason]),
close(Sock)
end,
rate_limit(State);

rate_limit(State, Result);
accepting(info, {inet_async, LSock, Ref, {error, closed}},
State = #state{lsock = LSock, accept_ref = Ref}) ->
{stop, normal, State};
Expand Down Expand Up @@ -184,13 +184,16 @@ code_change(_OldVsn, StateName, State, _Extra) ->

close(Sock) -> catch port_close(Sock).

rate_limit(State = #state{conn_limiter = Limiter}) ->
rate_limit(State = #state{conn_limiter = Limiter}, consume_limiter) ->
case esockd_limiter:consume(Limiter, 1) of
{I, Pause} when I =< 0 ->
{next_state, suspending, State, Pause};
_ ->
{keep_state, State, {next_event, internal, accept}}
end.
end;
rate_limit(State, _NotAccepted) ->
%% Socket closed or error by the time when accepting it
{keep_state, State, {next_event, internal, accept}}.

eval_tune_socket_fun({Fun, Args1}, Sock) ->
apply(Fun, [Sock|Args1]).
14 changes: 9 additions & 5 deletions src/esockd_dtls_acceptor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ accepting(internal, accept,
{ok, Sock} ->
%% Inc accepted stats.
esockd_server:inc_stats({Proto, ListenOn}, accepted, 1),
_ = case eval_tune_socket_fun(TuneFun, Sock) of
Result = case eval_tune_socket_fun(TuneFun, Sock) of
{ok, Sock} ->
case esockd_connection_sup:start_connection(ConnSup, Sock, UpgradeFuns) of
{ok, _Pid} -> ok;
{ok, _Pid} ->
consume_limiter;
{error, enotconn} ->
close(Sock); %% quiet...issue #10
{error, einval} ->
Expand All @@ -117,7 +118,7 @@ accepting(internal, accept,
[esockd:format(Sockname), Reason]),
close(Sock)
end,
rate_limit(State);
rate_limit(State, Result);
{error, Reason} when Reason =:= emfile;
Reason =:= enfile ->
{next_state, suspending, State, 1000};
Expand Down Expand Up @@ -148,13 +149,16 @@ code_change(_OldVsn, StateName, State, _Extra) ->

close(Sock) -> ssl:close(Sock).

rate_limit(State = #state{conn_limiter = Limiter}) ->
rate_limit(State = #state{conn_limiter = Limiter}, consume_limiter) ->
case esockd_limiter:consume(Limiter, 1) of
{I, Pause} when I =< 0 ->
{next_state, suspending, State, Pause};
_ ->
{keep_state, State, {next_event, internal, accept}}
end.
end;
rate_limit(State, _NotAccepted) ->
%% Socket closed or error by the time when accepting it
{keep_state, State, {next_event, internal, accept}}.

eval_tune_socket_fun({Fun, Args1}, Sock) ->
apply(Fun, [Sock|Args1]).
Expand Down
Loading