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

emqx / esockd / 435

13 Jul 2024 11:07AM UTC coverage: 66.998%. First build
435

Pull #190

github

web-flow
Merge 8478e6815 into 4ec038215
Pull Request #190: fix: refactor the udp proxy

0 of 46 new or added lines in 4 files covered. (0.0%)

877 of 1309 relevant lines covered (67.0%)

56.66 hits per line

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

13.64
/src/udp_proxy/esockd_udp_proxy_db.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_db).
18

19
-behaviour(gen_server).
20

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

23
%% API
24
-export([
25
    start_link/0,
26
    attach/2,
27
    detach/2
28
]).
29

30
%% gen_server callbacks
31
-export([
32
    init/1,
33
    handle_call/3,
34
    handle_cast/2,
35
    handle_info/2,
36
    terminate/2
37
]).
38

39
-define(ID(Mod, CId), {Mod, CId}).
40

41
-record(connection, {
42
    id :: ?ID(connection_module(), connection_id()),
43
    %% Reference Counter
44
    proxy :: pid()
45
}).
46

47
-define(TAB, esockd_udp_proxy_db).
48

49
%%--------------------------------------------------------------------
50
%%- API
51
%%--------------------------------------------------------------------
52
-spec start_link() -> {ok, pid()}.
53
start_link() ->
54
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
4✔
55

56
-spec attach(connection_module(), connection_id()) -> true.
57
attach(Mod, CId) ->
NEW
58
    ID = ?ID(Mod, CId),
×
NEW
59
    case ets:lookup(?TAB, ID) of
×
60
        [] ->
NEW
61
            ok;
×
62
        [#connection{proxy = ProxyId}] ->
NEW
63
            esockd_udp_proxy:takeover(ProxyId, CId)
×
64
    end,
NEW
65
    ets:insert(?TAB, #connection{id = ID, proxy = self()}).
×
66

67
-spec detach(connection_module(), connection_id()) -> boolean().
68
detach(Mod, CId) ->
NEW
69
    ProxyId = self(),
×
NEW
70
    ID = ?ID(Mod, CId),
×
NEW
71
    case ets:lookup(?TAB, ID) of
×
72
        [#connection{proxy = ProxyId}] ->
NEW
73
            ets:delete(?TAB, ID),
×
NEW
74
            true;
×
75
        _ ->
NEW
76
            false
×
77
    end.
78

79
%%--------------------------------------------------------------------
80
%%- gen_server callbacks
81
%%--------------------------------------------------------------------
82
init([]) ->
83
    ?TAB = ets:new(?TAB, [
4✔
84
        set,
85
        public,
86
        named_table,
87
        {keypos, #connection.id},
88
        {write_concurrency, true},
89
        {read_concurrency, true}
90
    ]),
91
    {ok, #{}}.
4✔
92

93
handle_call(Req, _From, State) ->
94
    error_logger:error_msg("Unexpected call: ~p", [Req]),
×
95
    {reply, ignore, State}.
×
96

97
handle_cast(Msg, State) ->
98
    error_logger:error_msg("Unexpected cast: ~p~n", [Msg]),
×
99
    {noreply, State}.
×
100

101
handle_info(Info, State) ->
102
    error_logger:error_msg("Unexpected info: ~p~n", [Info]),
×
103
    {noreply, State}.
×
104

105
terminate(_Reason, _State) ->
106
    ets:delete(?TAB),
×
107
    ok.
×
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