• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

emqx / esockd / 425

12 Jul 2024 05:08PM UTC coverage: 67.1%. First build
425

Pull #190

github

web-flow
Merge 1178d8cdf into 4ec038215
Pull Request #190: fix: fixed the proxy could not be released even if the RC was 0

0 of 29 new or added lines in 2 files covered. (0.0%)

877 of 1307 relevant lines covered (67.1%)

56.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/udp_proxy/esockd_udp_proxy.erl
1
%%--------------------------------------------------------------------
2
%% Copyright (c) 2020 EMQ Technologies Co., Ltd. All Rights Reserved.
3
%%
4
%% Licensed under the Apache License, Version 2.0 (the "License");
5
%% you may not use this file except in compliance with the License.
6
%% You may obtain a copy of the License at
7
%%
8
%%     http://www.apache.org/licenses/LICENSE-2.0
9
%%
10
%% Unless required by applicable law or agreed to in writing, software
11
%% distributed under the License is distributed on an "AS IS" BASIS,
12
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
%% See the License for the specific language governing permissions and
14
%% limitations under the License.
15
%%--------------------------------------------------------------------
16

17
-module(esockd_udp_proxy).
18

19
-behaviour(gen_server).
20

21
-include("include/esockd_proxy.hrl").
22

23
%% API
24
-export([start_link/3, send/2, close/1, takeover/2]).
25

26
%% gen_server callbacks
27
-export([
28
    init/1,
29
    handle_call/3,
30
    handle_cast/2,
31
    handle_info/2,
32
    terminate/2
33
]).
34

35
-export_type([connection_options/0]).
36

37
-define(NOW, erlang:system_time(second)).
38
-define(ERROR_MSG(Format, Args),
39
    error_logger:error_msg("[~s]: " ++ Format, [?MODULE | Args])
40
).
41
-define(DEF_HEARTBEAT, 60).
42

43
-type timespan() :: non_neg_integer().
44

45
%%--------------------------------------------------------------------
46
%%  Definitions
47
%%--------------------------------------------------------------------
48

49
-type state() :: #{
50
    connection_mod := connection_module(),
51
    connection_id := connection_id() | undefined,
52
    connection_state := connection_state(),
53
    connection_pid := pid() | undefined,
54
    connection_options := connection_options(),
55
    %% last source's connection active time
56
    last_time := pos_integer(),
57
    transport := proxy_transport(),
58
    peer := peer()
59
}.
60

61
%%--------------------------------------------------------------------
62
%%- API
63
%%--------------------------------------------------------------------
64

65
start_link(Transport, Peer, Opts) ->
66
    gen_server:start_link(?MODULE, [Transport, Peer, Opts], []).
×
67

68
-spec send(proxy_id(), binary()) -> ok.
69
send(ProxyId, Data) ->
70
    gen_server:cast(ProxyId, {send, Data}).
×
71

72
close(ProxyId) ->
73
    case erlang:is_process_alive(ProxyId) of
×
74
        true ->
75
            gen_server:call(ProxyId, close);
×
76
        _ ->
77
            ok
×
78
    end.
79

80
takeover(ProxyId, CId) ->
NEW
81
    _ = gen_server:cast(ProxyId, {?FUNCTION_NAME, CId}),
×
NEW
82
    ok.
×
83

84
%%--------------------------------------------------------------------
85
%%- gen_server callbacks
86
%%--------------------------------------------------------------------
87

88
init([Transport, Peer, #{esockd_proxy_opts := Opts} = COpts]) ->
89
    #{connection_mod := Mod} = Opts,
×
90
    heartbeat(maps:get(heartbeat, Opts, ?DEF_HEARTBEAT)),
×
91
    init_transport(Transport, Peer, #{
×
92
        last_time => ?NOW,
93
        connection_mod => Mod,
94
        connection_options => COpts,
95
        connection_state => esockd_udp_proxy_connection:initialize(Mod, COpts),
96
        connection_id => undefined,
97
        connection_pid => undefined
98
    }).
99

100
handle_call(close, _From, State) ->
101
    {stop, {shutdown, close_transport}, ok, State};
×
102
handle_call(Request, _From, State) ->
103
    ?ERROR_MSG("Unexpected call: ~p", [Request]),
×
104
    {reply, ok, State}.
×
105

106
handle_cast({send, Data}, #{transport := Transport, peer := Peer} = State) ->
107
    case send(Transport, Peer, Data) of
×
108
        ok ->
109
            {noreply, State};
×
110
        {error, Reason} ->
111
            ?ERROR_MSG("Send failed, Reason: ~0p", [Reason]),
×
112
            {stop, {sock_error, Reason}, State}
×
113
    end;
114
handle_cast({takeover, CId}, #{connection_id := CId} = State) ->
NEW
115
    {stop, {shutdown, takeover}, State};
×
116
handle_cast({takeover, _CId}, State) ->
NEW
117
    {noreply, State};
×
118
handle_cast(Request, State) ->
119
    ?ERROR_MSG("Unexpected cast: ~p", [Request]),
×
120
    {noreply, State}.
×
121

122
handle_info({datagram, _SockPid, Data}, State) ->
123
    {noreply, handle_incoming(Data, State)};
×
124
handle_info({ssl, _Socket, Data}, State) ->
125
    {noreply, handle_incoming(Data, State)};
×
126
handle_info({heartbeat, Span}, #{last_time := LastTime} = State) ->
127
    Now = ?NOW,
×
128
    case Now - LastTime > Span of
×
129
        true ->
130
            {stop, normal, State};
×
131
        _ ->
132
            heartbeat(Span),
×
133
            {noreply, State}
×
134
    end;
135
handle_info({ssl_error, _Sock, Reason}, State) ->
136
    {stop, Reason, socket_exit(State)};
×
137
handle_info({ssl_closed, _Sock}, State) ->
138
    {stop, ssl_closed, socket_exit(State)};
×
139
handle_info(
140
    {'DOWN', _, process, Pid, _Reason},
141
    State
142
) ->
NEW
143
    ct:print(">>> DOWN:~p, Self:~p, ~p~n", [Pid, self(), ets:tab2list(esockd_udp_proxy_db)]),
×
144
    {stop, {shutdown, connection_closed}, State};
×
145
handle_info(Info, State) ->
146
    ?ERROR_MSG("Unexpected info: ~p", [Info]),
×
147
    {noreply, State}.
×
148

149
terminate(Reason, #{transport := Transport} = State) ->
NEW
150
    ct:print(">>> esockd:~p termiante, reason:~p~n", [self(), Reason]),
×
151
    close_transport(Transport),
×
152
    Clear =
×
153
        case Reason of
154
            close_transport ->
155
                false;
×
156
            connection_closed ->
157
                false;
×
158
            takeover ->
NEW
159
                false;
×
160
            _ ->
161
                true
×
162
        end,
163
    detach(State, Clear).
×
164

165
%%--------------------------------------------------------------------
166
%%- Internal functions
167
%%--------------------------------------------------------------------
168
-spec handle_incoming(socket_packet(), state()) -> state().
169
handle_incoming(
170
    Data,
171
    #{transport := Transport, peer := Peer, connection_mod := Mod, connection_state := CState} =
172
        State
173
) ->
174
    State2 = State#{last_time := ?NOW},
×
175
    case esockd_udp_proxy_connection:get_connection_id(Mod, Transport, Peer, CState, Data) of
×
176
        {ok, CId, Packet, CState2} ->
177
            dispatch(Mod, CId, Data, Packet, State2#{connection_state := CState2});
×
178
        invalid ->
179
            ?ERROR_MSG("Can't get connection id, Transport:~0p, Peer:~0p, Mod:~0p", [
×
180
                Transport, Peer, Mod
181
            ]),
182
            State2
×
183
    end.
184

185
-spec dispatch(
186
    connection_module(),
187
    esockd_transport:socket(),
188
    connection_id(),
189
    connection_packet(),
190
    state()
191
) ->
192
    state().
193
dispatch(
194
    Mod,
195
    CId,
196
    Data,
197
    Packet,
198
    #{
199
        transport := Transport,
200
        peer := Peer,
201
        connection_state := CState,
202
        connection_options := Opts
203
    } =
204
        State
205
) ->
206
    case lookup(Mod, Transport, Peer, CId, Opts) of
×
207
        {ok, Pid} ->
NEW
208
            Result = attach(CId, State, Pid),
×
209
            esockd_udp_proxy_connection:dispatch(
×
210
                Mod, Pid, CState, {Transport, Data, Packet}
211
            ),
NEW
212
            Result;
×
213
        {error, Reason} ->
214
            ?ERROR_MSG("Dispatch failed, Reason:~0p", [Reason]),
×
215
            State
×
216
    end.
217

218
-spec attach(connection_id(), state(), pid()) -> state().
219
attach(CId, #{connection_mod := Mod, connection_id := undefined} = State, Pid) ->
NEW
220
    esockd_udp_proxy_db:attach(Mod, CId, Pid),
×
NEW
221
    _ = erlang:monitor(process, Pid),
×
NEW
222
    ct:print(">>> monitor, self:~p, pid:~p~n", [self(), Pid]),
×
NEW
223
    State#{connection_id := CId, connection_pid := Pid};
×
224
attach(CId, #{connection_id := OldId} = State, Pid) when CId =/= OldId ->
NEW
225
    State2 = detach(State, false),
×
NEW
226
    attach(CId, State2, Pid);
×
227
attach(_CId, State, _Pid) ->
228
    State.
×
229

230
detach(State) ->
231
    detach(State, true).
×
232

233
-spec detach(state()) -> state().
234
detach(#{connection_id := undefined} = State, _Clear) ->
235
    State;
×
236
detach(
237
    #{
238
        connection_id := CId,
239
        connection_pid := Pid,
240
        connection_mod := Mod,
241
        connection_state := CState
242
    } = State,
243
    Clear
244
) ->
NEW
245
    _ = erlang:demonitor(Pid),
×
NEW
246
    ct:print(">>> de monitor, self:~p, pid:~p~n", [self(), Pid]),
×
247
    case esockd_udp_proxy_db:detach(Mod, CId) of
×
248
        {Clear, Pid} ->
249
            case erlang:is_process_alive(Pid) of
×
250
                true ->
251
                    esockd_udp_proxy_connection:close(Mod, Pid, CState);
×
252
                _ ->
253
                    ok
×
254
            end;
255
        _ ->
256
            ok
×
257
    end,
NEW
258
    State#{connection_id := undefined, connection_pid := undefined}.
×
259

260
-spec socket_exit(state()) -> state().
261
socket_exit(State) ->
262
    detach(State).
×
263

264
-spec heartbeat(timespan()) -> ok.
265
heartbeat(Span) ->
266
    erlang:send_after(timer:seconds(Span), self(), {?FUNCTION_NAME, Span}),
×
267
    ok.
×
268

269
-spec lookup(
270
    connection_module(),
271
    proxy_transport(),
272
    peer(),
273
    connection_id(),
274
    connection_options()
275
) -> {ok, pid()} | {error, Reason :: term()}.
276
lookup(Mod, Transport, Peer, CId, Opts) ->
277
    case esockd_udp_proxy_db:lookup(Mod, CId) of
×
278
        {ok, _} = Ok ->
279
            Ok;
×
280
        undefined ->
281
            case esockd_udp_proxy_connection:create(Mod, Transport, Peer, Opts) of
×
282
                {ok, Pid} ->
283
                    {ok, Pid};
×
284
                ignore ->
285
                    {error, ignore};
×
286
                Error ->
287
                    Error
×
288
            end
289
    end.
290

291
-spec send(proxy_transport(), peer(), binary()) -> _.
292
send({?PROXY_TRANSPORT, _, Socket}, {IP, Port}, Data) when is_port(Socket) ->
293
    gen_udp:send(Socket, IP, Port, Data);
×
294
send({?PROXY_TRANSPORT, _, Socket}, _Peer, Data) ->
295
    esockd_transport:send(Socket, Data).
×
296

297
init_transport({udp, _, Sock}, Peer, State) ->
298
    {ok, State#{
×
299
        transport => {?PROXY_TRANSPORT, self(), Sock},
300
        peer => Peer
301
    }};
302
init_transport(esockd_transport, Sock, State) ->
303
    case esockd_transport:wait(Sock) of
×
304
        {ok, NSock} ->
305
            {ok, State#{
×
306
                transport => {?PROXY_TRANSPORT, self(), NSock},
307
                peer => esockd_transport:peername(NSock)
308
            }};
309
        Error ->
310
            Error
×
311
    end.
312

313
close_transport({?PROXY_TRANSPORT, _, Sock}) when is_port(Sock) ->
314
    ok;
×
315
close_transport({?PROXY_TRANSPORT, _, Sock}) ->
316
    esockd_transport:fast_close(Sock).
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc