• 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

12.0
/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/3,
27
    detach/2,
28
    lookup/2
29
]).
30

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

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

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

50
-define(TAB, esockd_udp_proxy_db).
51

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

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

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

82
-spec lookup(connection_module(), connection_id()) -> {ok, pid()} | undefined.
83
lookup(Mod, CId) ->
84
    case ets:lookup(?TAB, ?ID(Mod, CId)) of
×
85
        [#connection{connection = ConnId}] ->
NEW
86
            {ok, ConnId};
×
87
        _ ->
88
            undefined
×
89
    end.
90

91
%%--------------------------------------------------------------------
92
%%- gen_server callbacks
93
%%--------------------------------------------------------------------
94
init([]) ->
95
    ?TAB = ets:new(?TAB, [
4✔
96
        set,
97
        public,
98
        named_table,
99
        {keypos, #connection.id},
100
        {write_concurrency, true},
101
        {read_concurrency, true}
102
    ]),
103
    {ok, #{}}.
4✔
104

105
handle_call(Req, _From, State) ->
106
    error_logger:error_msg("Unexpected call: ~p", [Req]),
×
107
    {reply, ignore, State}.
×
108

109
handle_cast(Msg, State) ->
110
    error_logger:error_msg("Unexpected cast: ~p~n", [Msg]),
×
111
    {noreply, State}.
×
112

113
handle_info(Info, State) ->
114
    error_logger:error_msg("Unexpected info: ~p~n", [Info]),
×
115
    {noreply, State}.
×
116

117
terminate(_Reason, _State) ->
118
    ets:delete(?TAB),
×
119
    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