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

emqx / esockd / 433

13 Jul 2024 09:38AM UTC coverage: 67.152%. First build
433

Pull #190

github

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

0 of 43 new or added lines in 3 files covered. (0.0%)

877 of 1306 relevant lines covered (67.15%)

56.79 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_ref := reference() | undefined,
55
    connection_options := connection_options(),
56
    %% last source's connection active time
57
    last_time := pos_integer(),
58
    transport := proxy_transport(),
59
    peer := peer()
60
}.
61

62
%%--------------------------------------------------------------------
63
%%- API
64
%%--------------------------------------------------------------------
65

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

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

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

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

85
%%--------------------------------------------------------------------
86
%%- gen_server callbacks
87
%%--------------------------------------------------------------------
88

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

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

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

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

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

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

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

224
-spec attach(connection_id(), state(), pid()) -> state().
225
attach(CId, #{connection_mod := Mod, connection_id := undefined} = State, Pid) ->
226
    esockd_udp_proxy_db:attach(Mod, CId),
×
NEW
227
    Ref = erlang:monitor(process, Pid),
×
NEW
228
    State#{connection_id := CId, connection_pid := Pid, connection_ref := Ref};
×
229
attach(CId, #{connection_id := OldId} = State, Pid) when CId =/= OldId ->
NEW
230
    State2 = detach(State, false),
×
NEW
231
    attach(CId, State2, Pid);
×
232
attach(_CId, State, _Pid) ->
233
    State.
×
234

235
detach(State) ->
236
    detach(State, true).
×
237

238
-spec detach(state()) -> state().
239
detach(#{connection_id := undefined} = State, _Clear) ->
240
    State;
×
241
detach(
242
    #{
243
        connection_id := CId,
244
        connection_pid := Pid,
245
        connection_ref := Ref,
246
        connection_mod := Mod,
247
        connection_state := CState
248
    } = State,
249
    Clear
250
) ->
NEW
251
    erlang:demonitor(Ref),
×
252

NEW
253
    Result = esockd_udp_proxy_db:detach(Mod, CId),
×
NEW
254
    ct:print(">>>> detach:~p result:~p, pid:~p~n", [self(), Result, Pid]),
×
NEW
255
    case Clear andalso Result of
×
256
        true ->
257
            case erlang:is_process_alive(Pid) of
×
258
                true ->
259
                    esockd_udp_proxy_connection:close(Mod, Pid, CState);
×
260
                _ ->
261
                    ok
×
262
            end;
263
        _ ->
264
            ok
×
265
    end,
NEW
266
    State#{connection_id := undefined, connection_pid := undefined, connection_ref := undefined}.
×
267

268
-spec socket_exit(state()) -> state().
269
socket_exit(State) ->
270
    detach(State).
×
271

272
-spec heartbeat(timespan()) -> ok.
273
heartbeat(Span) ->
274
    erlang:send_after(timer:seconds(Span), self(), {?FUNCTION_NAME, Span}),
×
275
    ok.
×
276

277
-spec lookup(connection_id(), state()) -> {ok, pid()} | {error, Reason :: term()}.
278
lookup(_CId, #{connection_pid := Pid}) when is_pid(Pid) ->
NEW
279
    {ok, Pid};
×
280
lookup(CId, #{
281
    connection_pid := undefined,
282
    connection_mod := Mod,
283
    transport := Transport,
284
    peer := Peer,
285
    connection_options := Opts
286
}) ->
NEW
287
    case esockd_udp_proxy_connection:find_or_create(Mod, CId, Transport, Peer, Opts) of
×
288
        {ok, Pid} ->
NEW
289
            {ok, Pid};
×
290
        ignore ->
NEW
291
            {error, ignore};
×
292
        Error ->
NEW
293
            Error
×
294
    end.
295

296
-spec send(proxy_transport(), peer(), binary()) -> _.
297
send({?PROXY_TRANSPORT, _, Socket}, {IP, Port}, Data) when is_port(Socket) ->
298
    gen_udp:send(Socket, IP, Port, Data);
×
299
send({?PROXY_TRANSPORT, _, Socket}, _Peer, Data) ->
300
    esockd_transport:send(Socket, Data).
×
301

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

318
close_transport({?PROXY_TRANSPORT, _, Sock}) when is_port(Sock) ->
319
    ok;
×
320
close_transport({?PROXY_TRANSPORT, _, Sock}) ->
321
    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