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

emqx / esockd / 396

08 Jul 2024 10:56AM UTC coverage: 69.194%. First build
396

Pull #188

github

web-flow
Merge 9c2399c81 into 313713eff
Pull Request #188: feat: add content-sensitive proxy behaviour for UDP

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

867 of 1253 relevant lines covered (69.19%)

59.0 hits per line

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

0.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
-include("include/esockd_proxy.hrl").
20

21
%% API
22
-export([
23
    insert/2,
24
    attach/1,
25
    detach/1,
26
    lookup/1,
27
    start/0
28
]).
29

30
-record(connection, {
31
    id :: connection_id(),
32
    %% the connection pid
33
    pid :: pid(),
34
    %% Reference Counter
35
    count :: non_neg_integer()
36
}).
37

38
-define(TAB, esockd_udp_proxy_db).
39
-define(MINIMUM_VAL, -2147483647).
40

41
%%--------------------------------------------------------------------
42
%%- API
43
%%--------------------------------------------------------------------
44
start() ->
NEW
45
    ets:new(?TAB, [
×
46
        set,
47
        public,
48
        named_table,
49
        {keypos, #connection.id},
50
        {write_concurrency, true},
51
        {read_concurrency, true}
52
    ]).
53

54
-spec insert(connection_id(), pid()) -> boolean().
55
insert(Cid, Pid) ->
NEW
56
    ets:insert_new(?TAB, #connection{
×
57
        id = Cid,
58
        pid = Pid,
59
        count = 1
60
    }).
61

62
-spec attach(connection_id()) -> integer().
63
attach(Cid) ->
NEW
64
    ets:update_counter(?TAB, Cid, {#connection.count, 1}, 1).
×
65

66
-spec detach(connection_id()) -> {Clear :: true, connection_state()} | false.
67
detach(Cid) ->
NEW
68
    RC = ets:update_counter(?TAB, Cid, {#connection.count, -1, 0, ?MINIMUM_VAL}, 0),
×
NEW
69
    if
×
70
        RC < 0 ->
NEW
71
            case ets:lookup(?TAB, Cid) of
×
72
                [#connection{pid = Pid}] ->
NEW
73
                    ets:delete(?TAB, Cid),
×
NEW
74
                    {true, Pid};
×
75
                _ ->
NEW
76
                    false
×
77
            end;
78
        true ->
NEW
79
            false
×
80
    end.
81

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

91
%%--------------------------------------------------------------------
92
%% @doc
93
%% @spec
94
%% @end
95
%%--------------------------------------------------------------------
96

97
%%--------------------------------------------------------------------
98
%%- Internal functions
99
%%--------------------------------------------------------------------
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