-
Notifications
You must be signed in to change notification settings - Fork 86
feat: add socket-based listener support #204
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ddf1040
feat: support `socket`-based transport in `esockd_proxy_protocol`
keynslug d34c286
feat: generalize acceptor fsm to support `socket`-based transport
keynslug 0b6a1a6
feat: add `socket`-based listener support
keynslug 940ddcb
fix(examples): fix async recv echo server
keynslug c3102dc
chore: fix dialyzer warnings
keynslug 072674c
chore: fix Makefile target definition
keynslug 4b087f5
fix(tcpsocket): handle supplied inet family correctly
keynslug d3c0e29
feat(tcpsocket): implement `esockd_socket:type/1`
keynslug f1604f5
chore: make `esockd_socket` success typed w/o dialyzer hacks
keynslug 16f5766
feat(tcpsocket): support `tune_fun` option
keynslug 8ce9dfb
fix(proxy): handle partial proxy protocol header for `esockd_socket`s
keynslug a1913cf
chore(acceptor): update state diagram
keynslug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
%%-------------------------------------------------------------------- | ||
%% Copyright (c) 2025 EMQ Technologies Co., Ltd. All Rights Reserved. | ||
%% | ||
%% Licensed under the Apache License, Version 2.0 (the "License"); | ||
%% you may not use this file except in compliance with the License. | ||
%% You may obtain a copy of the License at | ||
%% | ||
%% http://www.apache.org/licenses/LICENSE-2.0 | ||
%% | ||
%% Unless required by applicable law or agreed to in writing, software | ||
%% distributed under the License is distributed on an "AS IS" BASIS, | ||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
%% See the License for the specific language governing permissions and | ||
%% limitations under the License. | ||
%%-------------------------------------------------------------------- | ||
|
||
-module(esockd_accept_inet). | ||
|
||
-export([ | ||
init/2, | ||
async_accept/2, | ||
async_accept_result/3, | ||
post_accept/2, | ||
fast_close/1 | ||
]). | ||
|
||
-export([ | ||
mk_tune_socket_fun/1, | ||
tune_socket/2 | ||
]). | ||
|
||
-type ctx() :: {module(), tune_socket_fun()}. | ||
-type socket() :: inet:socket(). | ||
-type async_ref() :: reference(). | ||
|
||
-type tune_socket_fun() :: | ||
{fun((socket(), Opts) -> {ok, socket()} | {error, any()}), Opts}. | ||
|
||
%% | ||
|
||
-spec init(socket(), _Opts) -> ctx(). | ||
init(LSock, TuneFun) -> | ||
{ok, SockMod} = inet_db:lookup_socket(LSock), | ||
{SockMod, TuneFun}. | ||
|
||
-spec async_accept(socket(), ctx()) -> | ||
{async, async_ref()} | {error, atom()}. | ||
async_accept(LSock, _Ctx) -> | ||
case prim_inet:async_accept(LSock, -1) of | ||
{ok, Ref} -> | ||
{async, Ref}; | ||
{error, Reason} -> | ||
{error, Reason} | ||
end. | ||
|
||
-spec async_accept_result(Message, async_ref(), ctx()) -> | ||
{ok, socket()} | {error, atom()} | Message. | ||
async_accept_result({inet_async, _LSock, Ref, {ok, Sock}}, Ref, _Ctx) -> | ||
{ok, Sock}; | ||
async_accept_result({inet_async, _LSock, Ref, {error, Reason}}, Ref, _Ctx) -> | ||
{error, Reason}; | ||
async_accept_result(Info, _Ref, _Ctx) -> | ||
Info. | ||
|
||
-spec post_accept(socket(), ctx()) -> {ok, socket()} | {error, atom()}. | ||
post_accept(Sock, {SockMod, TuneFun}) -> | ||
%% make it look like gen_tcp:accept | ||
inet_db:register_socket(Sock, SockMod), | ||
eval_tune_socket_fun(TuneFun, Sock). | ||
|
||
eval_tune_socket_fun({Fun, Opts}, Sock) -> | ||
Fun(Sock, Opts). | ||
|
||
-spec mk_tune_socket_fun([esockd:option()]) -> tune_socket_fun(). | ||
mk_tune_socket_fun(Opts) -> | ||
TuneOpts = [{Name, Val} || {Name, Val} <- Opts, | ||
Name =:= tune_buffer orelse | ||
Name =:= tune_fun], | ||
{fun ?MODULE:tune_socket/2, TuneOpts}. | ||
|
||
tune_socket(Sock, []) -> | ||
{ok, Sock}; | ||
tune_socket(Sock, [{tune_buffer, true}|More]) -> | ||
case esockd_transport:getopts(Sock, [sndbuf, recbuf, buffer]) of | ||
{ok, BufSizes} -> | ||
BufSz = lists:max([Sz || {_Opt, Sz} <- BufSizes]), | ||
case esockd_transport:setopts(Sock, [{buffer, BufSz}]) of | ||
ok -> | ||
tune_socket(Sock, More); | ||
Error -> | ||
Error | ||
end; | ||
Error -> | ||
Error | ||
end; | ||
tune_socket(Sock, [{tune_fun, {M, F, A}} | More]) -> | ||
%% NOTE: Socket is not part of the argument list, backward compatibility. | ||
case apply(M, F, A) of | ||
ok -> | ||
tune_socket(Sock, More); | ||
Error -> | ||
Error | ||
end. | ||
|
||
-spec fast_close(socket()) -> ok. | ||
fast_close(Sock) -> | ||
try | ||
%% NOTE | ||
%% Port-close leads to a TCP reset which cuts out TCP graceful close overheads. | ||
_ = port_close(Sock), | ||
receive {'EXIT', Sock, _} -> ok after 1 -> ok end | ||
catch | ||
error:_ -> ok | ||
end. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
%%-------------------------------------------------------------------- | ||
%% Copyright (c) 2025 EMQ Technologies Co., Ltd. All Rights Reserved. | ||
%% | ||
%% Licensed under the Apache License, Version 2.0 (the "License"); | ||
%% you may not use this file except in compliance with the License. | ||
%% You may obtain a copy of the License at | ||
%% | ||
%% http://www.apache.org/licenses/LICENSE-2.0 | ||
%% | ||
%% Unless required by applicable law or agreed to in writing, software | ||
%% distributed under the License is distributed on an "AS IS" BASIS, | ||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
%% See the License for the specific language governing permissions and | ||
%% limitations under the License. | ||
%%-------------------------------------------------------------------- | ||
|
||
-module(esockd_accept_socket). | ||
|
||
-export([ | ||
init/2, | ||
async_accept/2, | ||
async_accept_result/3, | ||
post_accept/2, | ||
fast_close/1 | ||
]). | ||
|
||
-export([ | ||
mk_tune_socket_fun/1, | ||
tune_socket/2 | ||
]). | ||
|
||
-type ctx() :: tune_socket_fun(). | ||
-type socket() :: socket:socket(). | ||
-type async_ref() :: socket:select_info(). | ||
|
||
-type tune_socket_fun() :: | ||
{fun((socket(), Opts) -> {ok, socket()} | {error, any()}), Opts}. | ||
|
||
-define(DEFAULT_SOCK_OPTIONS, [{nodelay, true}]). | ||
|
||
%% | ||
|
||
-spec init(socket(), _Opts) -> ctx(). | ||
init(_LSock, TuneFun) -> | ||
TuneFun. | ||
|
||
-spec async_accept(socket(), _Opts) -> | ||
{ok, socket()} | {async, async_ref()} | {error, atom()}. | ||
async_accept(LSock, _Opts) -> | ||
case socket:accept(LSock, nowait) of | ||
{ok, Sock} -> | ||
{ok, Sock}; | ||
{error, Reason} -> | ||
{error, Reason}; | ||
{select, {_Info, _Tag, Handle}} -> | ||
qzhuyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{async, Handle} | ||
end. | ||
|
||
-spec async_accept_result(Info, async_ref(), _Opts) -> | ||
{ok, socket()} | {error, atom()} | {async, async_ref()} | Info. | ||
async_accept_result({'$socket', LSock, select, Handle}, Handle, _Opts) -> | ||
case socket:accept(LSock, Handle) of | ||
{ok, Sock} -> | ||
{ok, Sock}; | ||
{error, Reason} -> | ||
{error, Reason}; | ||
{select, {_Info, _Tag, NHandle}} -> | ||
{async, NHandle} | ||
end; | ||
async_accept_result({'$socket', _LSock, abort, {Handle, Reason}}, Handle, _Opts) -> | ||
{error, Reason}; | ||
async_accept_result(Info, _Handle, _Opts) -> | ||
Info. | ||
|
||
-spec post_accept(socket(), ctx()) -> {ok, socket()} | {error, atom()}. | ||
post_accept(Sock, TuneFun) -> | ||
eval_tune_socket_fun(Sock, TuneFun). | ||
|
||
eval_tune_socket_fun(Sock, {Fun, Opts}) -> | ||
Fun(Sock, Opts). | ||
|
||
-spec mk_tune_socket_fun([esockd:option()]) -> tune_socket_fun(). | ||
mk_tune_socket_fun(Opts) -> | ||
TcpOpts = proplists:get_value(tcp_options, Opts, []), | ||
SockOpts = lists:flatten([sock_opt(O) || O <- merge_sock_defaults(TcpOpts)]), | ||
TuneOpts = [{Name, Val} || {Name, Val} <- Opts, | ||
Name =:= tune_buffer orelse | ||
Name =:= tune_fun], | ||
{fun ?MODULE:tune_socket/2, [{setopts, SockOpts} | TuneOpts]}. | ||
|
||
tune_socket(Sock, [{setopts, SockOpts} | Rest]) -> | ||
case setopts(Sock, SockOpts) of | ||
ok -> | ||
tune_socket(Sock, Rest); | ||
Error -> | ||
Error | ||
end; | ||
tune_socket(Sock, [{tune_buffer, true} | Rest]) -> | ||
try | ||
BufRecv = ensure(socket:getopt(Sock, {socket, rcvbuf})), | ||
Buffer = ensure(socket:getopt(Sock, {otp, rcvbuf})), | ||
Max = max(Buffer, BufRecv), | ||
ok = ensure(socket:setopt(Sock, {otp, rcvbuf}, Max)), | ||
tune_socket(Sock, Rest) | ||
catch | ||
Error -> Error | ||
end; | ||
tune_socket(Sock, [{tune_fun, {M, F, A}} | Rest]) -> | ||
%% NOTE: Socket is not part of the argument list, backward compatibility. | ||
case apply(M, F, A) of | ||
ok -> | ||
tune_socket(Sock, Rest); | ||
Error -> | ||
Error | ||
end; | ||
tune_socket(Sock, _) -> | ||
{ok, Sock}. | ||
|
||
ensure(ok) -> ok; | ||
ensure({ok, Result}) -> Result; | ||
ensure(Error) -> throw(Error). | ||
|
||
merge_sock_defaults(Opts) -> | ||
esockd:merge_opts(?DEFAULT_SOCK_OPTIONS, Opts). | ||
|
||
sock_opt(binary) -> | ||
%% Meaningless. | ||
[]; | ||
sock_opt({nodelay, Flag}) -> | ||
{{tcp, nodelay}, Flag}; | ||
sock_opt({linger, {Flag, N}}) -> | ||
{{socket, linger}, #{onoff => Flag, linger => N}}; | ||
sock_opt({recbuf, Size}) -> | ||
{{socket, rcvbuf}, Size}; | ||
sock_opt({sndbuf, Size}) -> | ||
{{socket, sndbuf}, Size}; | ||
sock_opt({buffer, Size}) -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to my own notes, double check. |
||
{{otp, rcvbuf}, Size}; | ||
sock_opt({reuseaddr, _}) -> | ||
%% Listener option. | ||
[]; | ||
sock_opt({backlog, _}) -> | ||
%% Listener option. | ||
[]; | ||
sock_opt(_Opt) -> | ||
%% TODO: Ignored, need to notify user. | ||
[]. | ||
|
||
setopts(Sock, [{Opt, Value} | Rest]) -> | ||
case socket:setopt(Sock, Opt, Value) of | ||
ok -> setopts(Sock, Rest); | ||
Error -> Error | ||
end; | ||
setopts(_Sock, []) -> | ||
ok. | ||
|
||
-spec fast_close(socket()) -> ok. | ||
fast_close(Sock) -> | ||
esockd_socket:fast_close(Sock). |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, need a -type for
Proto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps, but this is unrelated to the PR in question I believe.