Skip to content

Commit

Permalink
Merge pull request #2924 from emqx/master
Browse files Browse the repository at this point in the history
Auto-pull-request-by-2019-09-21
  • Loading branch information
turtleDeng committed Sep 21, 2019
2 parents a449cb5 + af9fb08 commit 8f0e16e
Show file tree
Hide file tree
Showing 41 changed files with 1,333 additions and 1,121 deletions.
2 changes: 1 addition & 1 deletion include/emqx.hrl
Expand Up @@ -64,7 +64,7 @@
%% Message flags
flags :: #{atom() => boolean()},
%% Message headers, or MQTT 5.0 Properties
headers = #{},
headers :: map(),
%% Topic that the message is published to
topic :: binary(),
%% Message Payload
Expand Down
4 changes: 2 additions & 2 deletions src/emqx_access_rule.erl
Expand Up @@ -108,9 +108,9 @@ match_who(#{client_id := ClientId}, {client, ClientId}) ->
true;
match_who(#{username := Username}, {user, Username}) ->
true;
match_who(#{peername := undefined}, {ipaddr, _Tup}) ->
match_who(#{peerhost := undefined}, {ipaddr, _Tup}) ->
false;
match_who(#{peername := {IP, _}}, {ipaddr, CIDR}) ->
match_who(#{peerhost := IP}, {ipaddr, CIDR}) ->
esockd_cidr:match(IP, CIDR);
match_who(Client, {'and', Conds}) when is_list(Conds) ->
lists:foldl(fun(Who, Allow) ->
Expand Down
23 changes: 12 additions & 11 deletions src/emqx_banned.erl
Expand Up @@ -30,7 +30,7 @@
-boot_mnesia({mnesia, [boot]}).
-copy_mnesia({mnesia, [copy]}).

-export([start_link/0]).
-export([start_link/0, stop/0]).

-export([ check/1
, add/1
Expand Down Expand Up @@ -69,11 +69,14 @@ mnesia(copy) ->
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

%% for tests
-spec(stop() -> ok).
stop() -> gen_server:stop(?MODULE).

-spec(check(emqx_types:client()) -> boolean()).
check(#{client_id := ClientId,
username := Username,
peername := {IPAddr, _}
}) ->
peerhost := IPAddr}) ->
ets:member(?BANNED_TAB, {client_id, ClientId})
orelse ets:member(?BANNED_TAB, {username, Username})
orelse ets:member(?BANNED_TAB, {ipaddr, IPAddr}).
Expand All @@ -82,11 +85,10 @@ check(#{client_id := ClientId,
add(Banned) when is_record(Banned, banned) ->
mnesia:dirty_write(?BANNED_TAB, Banned).

-spec(delete({client_id, emqx_types:client_id()} |
{username, emqx_types:username()} |
{peername, emqx_types:peername()}) -> ok).
delete(Key) ->
mnesia:dirty_delete(?BANNED_TAB, Key).
-spec(delete({client_id, emqx_types:client_id()}
| {username, emqx_types:username()}
| {peerhost, emqx_types:peerhost()}) -> ok).
delete(Key) -> mnesia:dirty_delete(?BANNED_TAB, Key).

info(InfoKey) ->
mnesia:table_info(?BANNED_TAB, InfoKey).
Expand All @@ -107,8 +109,7 @@ handle_cast(Msg, State) ->
{noreply, State}.

handle_info({timeout, TRef, expire}, State = #{expiry_timer := TRef}) ->
mnesia:async_dirty(fun expire_banned_items/1,
[erlang:system_time(second)]),
mnesia:async_dirty(fun expire_banned_items/1, [erlang:system_time(second)]),
{noreply, ensure_expiry_timer(State), hibernate};

handle_info(Info, State) ->
Expand All @@ -127,7 +128,7 @@ code_change(_OldVsn, State, _Extra) ->

-ifdef(TEST).
ensure_expiry_timer(State) ->
State#{expiry_timer := emqx_misc:start_timer(timer:seconds(1), expire)}.
State#{expiry_timer := emqx_misc:start_timer(10, expire)}.
-else.
ensure_expiry_timer(State) ->
State#{expiry_timer := emqx_misc:start_timer(timer:minutes(1), expire)}.
Expand Down

0 comments on commit 8f0e16e

Please sign in to comment.