Просмотр исходного кода

Change some '-type' attrs to '-opaque' and improve emqx_gc, emqx_inflight modules

- Define 'GCS(St)' macro to improve emqx_gc module
- Define 'Inflight(MaxSize, Tree)' macro to improve emqx_inflight module
Feng Lee 7 лет назад
Родитель
Сommit
c8b243ed22
7 измененных файлов с 59 добавлено и 46 удалено
  1. 4 1
      src/emqx_batch.erl
  2. 1 1
      src/emqx_frame.erl
  3. 13 8
      src/emqx_gc.erl
  4. 36 32
      src/emqx_inflight.erl
  5. 1 1
      src/emqx_keepalive.erl
  6. 3 2
      src/emqx_protocol.erl
  7. 1 1
      src/emqx_topic.erl

+ 4 - 1
src/emqx_batch.erl

@@ -22,6 +22,7 @@
         linger_ms => pos_integer(),
         commit_fun := function()
        }).
+
 -export_type([options/0]).
 
 -record(batch, {
@@ -31,7 +32,9 @@
           linger_timer :: reference() | undefined,
           commit_fun :: function()
          }).
--type(batch() :: #batch{}).
+
+-opaque(batch() :: #batch{}).
+
 -export_type([batch/0]).
 
 -spec(init(options()) -> batch()).

+ 1 - 1
src/emqx_frame.erl

@@ -24,7 +24,7 @@
 -type(options() :: #{max_packet_size => 1..?MAX_PACKET_SIZE,
                      version         => emqx_mqtt_types:version()}).
 
--type(parse_state() :: {none, options()} | cont_fun(binary())).
+-opaque(parse_state() :: {none, options()} | cont_fun(binary())).
 
 -type(cont_fun(Bin) :: fun((Bin) -> {ok, emqx_mqtt_types:packet(), binary()}
                                   | {more, cont_fun(Bin)})).

+ 13 - 8
src/emqx_gc.erl

@@ -31,7 +31,11 @@
 -type(st() :: #{cnt => {integer(), integer()},
                 oct => {integer(), integer()}}).
 
--type(gc_state() :: {?MODULE, st()}).
+-opaque(gc_state() :: {?MODULE, st()}).
+
+-export_type([gc_state/0]).
+
+-define(GCS(St), {?MODULE, St}).
 
 -define(disabled, disabled).
 -define(ENABLED(X), (is_integer(X) andalso X > 0)).
@@ -41,14 +45,15 @@
 init(#{count := Count, bytes := Bytes}) ->
     Cnt = [{cnt, {Count, Count}} || ?ENABLED(Count)],
     Oct = [{oct, {Bytes, Bytes}} || ?ENABLED(Bytes)],
-    {?MODULE, maps:from_list(Cnt ++ Oct)};
+    ?GCS(maps:from_list(Cnt ++ Oct));
 init(false) -> undefined.
 
 %% @doc Try to run GC based on reduntions of count or bytes.
--spec(run(pos_integer(), pos_integer(), gc_state()) -> {boolean(), gc_state()}).
-run(Cnt, Oct, {?MODULE, St}) ->
+-spec(run(pos_integer(), pos_integer(), gc_state())
+      -> {boolean(), gc_state()}).
+run(Cnt, Oct, ?GCS(St)) ->
     {Res, St1} = run([{cnt, Cnt}, {oct, Oct}], St),
-    {Res, {?MODULE, St1}};
+    {Res, ?GCS(St1)};
 run(_Cnt, _Oct, undefined) ->
     {false, undefined}.
 
@@ -64,15 +69,15 @@ run([{K, N}|T], St) ->
 
 %% @doc Info of GC state.
 -spec(info(gc_state()) -> maybe(map())).
-info({?MODULE, St}) ->
+info(?GCS(St)) ->
     St;
 info(undefined) ->
     undefined.
 
 %% @doc Reset counters to zero.
 -spec(reset(gc_state()) -> gc_state()).
-reset({?MODULE, St}) ->
-    {?MODULE, do_reset(St)};
+reset(?GCS(St)) ->
+    ?GCS(do_reset(St));
 reset(undefined) ->
     undefined.
 

+ 36 - 32
src/emqx_inflight.erl

@@ -14,11 +14,15 @@
 
 -module(emqx_inflight).
 
--export([new/1, contain/2, lookup/2, insert/3, update/3, update_size/2, delete/2, values/1,
-         to_list/1, size/1, max_size/1, is_full/1, is_empty/1, window/1]).
+-export([new/1, contain/2, lookup/2, insert/3, update/3, update_size/2, delete/2,
+         values/1, to_list/1, size/1, max_size/1, is_full/1, is_empty/1, window/1]).
 
+-type(key() :: term()).
 -type(max_size() :: pos_integer()).
--type(inflight() :: {?MODULE, max_size(), gb_trees:tree()}).
+-opaque(inflight() :: {?MODULE, max_size(), gb_trees:tree()}).
+
+-define(Inflight(Tree), {?MODULE, _MaxSize, Tree}).
+-define(Inflight(MaxSize, Tree), {?MODULE, MaxSize, (Tree)}).
 
 -export_type([inflight/0]).
 
@@ -26,68 +30,68 @@
 new(MaxSize) when MaxSize >= 0 ->
     {?MODULE, MaxSize, gb_trees:empty()}.
 
--spec(contain(Key :: term(), inflight()) -> boolean()).
-contain(Key, {?MODULE, _MaxSize, Tree}) ->
+-spec(contain(key(), inflight()) -> boolean()).
+contain(Key, ?Inflight(Tree)) ->
     gb_trees:is_defined(Key, Tree).
 
--spec(lookup(Key :: term(), inflight()) -> {value, term()} | none).
-lookup(Key, {?MODULE, _MaxSize, Tree}) ->
+-spec(lookup(key(), inflight()) -> {value, term()} | none).
+lookup(Key, ?Inflight(Tree)) ->
     gb_trees:lookup(Key, Tree).
 
--spec(insert(Key :: term(), Value :: term(), inflight()) -> inflight()).
-insert(Key, Value, {?MODULE, MaxSize, Tree}) ->
-    {?MODULE, MaxSize, gb_trees:insert(Key, Value, Tree)}.
+-spec(insert(key(), Val :: term(), inflight()) -> inflight()).
+insert(Key, Val, ?Inflight(MaxSize, Tree)) ->
+    ?Inflight(MaxSize, gb_trees:insert(Key, Val, Tree)).
 
--spec(delete(Key :: term(), inflight()) -> inflight()).
-delete(Key, {?MODULE, MaxSize, Tree}) ->
-    {?MODULE, MaxSize, gb_trees:delete(Key, Tree)}.
+-spec(delete(key(), inflight()) -> inflight()).
+delete(Key, ?Inflight(MaxSize, Tree)) ->
+    ?Inflight(MaxSize, gb_trees:delete(Key, Tree)).
 
--spec(update(Key :: term(), Val :: term(), inflight()) -> inflight()).
-update(Key, Val, {?MODULE, MaxSize, Tree}) ->
-    {?MODULE, MaxSize, gb_trees:update(Key, Val, Tree)}.
+-spec(update(key(), Val :: term(), inflight()) -> inflight()).
+update(Key, Val, ?Inflight(MaxSize, Tree)) ->
+    ?Inflight(MaxSize, gb_trees:update(Key, Val, Tree)).
 
 -spec(update_size(integer(), inflight()) -> inflight()).
-update_size(MaxSize, {?MODULE, _OldMaxSize, Tree}) ->
-    {?MODULE, MaxSize, Tree}.
+update_size(MaxSize, ?Inflight(Tree)) ->
+    ?Inflight(MaxSize, Tree).
 
 -spec(is_full(inflight()) -> boolean()).
-is_full({?MODULE, 0, _Tree}) ->
+is_full(?Inflight(0, _Tree)) ->
     false;
-is_full({?MODULE, MaxSize, Tree}) ->
+is_full(?Inflight(MaxSize, Tree)) ->
     MaxSize =< gb_trees:size(Tree).
 
 -spec(is_empty(inflight()) -> boolean()).
-is_empty({?MODULE, _MaxSize, Tree}) ->
+is_empty(?Inflight(Tree)) ->
     gb_trees:is_empty(Tree).
 
--spec(smallest(inflight()) -> {K :: term(), V :: term()}).
-smallest({?MODULE, _MaxSize, Tree}) ->
+-spec(smallest(inflight()) -> {key(), term()}).
+smallest(?Inflight(Tree)) ->
     gb_trees:smallest(Tree).
 
--spec(largest(inflight()) -> {K :: term(), V :: term()}).
-largest({?MODULE, _MaxSize, Tree}) ->
+-spec(largest(inflight()) -> {key(), term()}).
+largest(?Inflight(Tree)) ->
     gb_trees:largest(Tree).
 
 -spec(values(inflight()) -> list()).
-values({?MODULE, _MaxSize, Tree}) ->
+values(?Inflight(Tree)) ->
     gb_trees:values(Tree).
 
--spec(to_list(inflight()) -> list({K :: term(), V :: term()})).
-to_list({?MODULE, _MaxSize, Tree}) ->
+-spec(to_list(inflight()) -> list({key(), term()})).
+to_list(?Inflight(Tree)) ->
     gb_trees:to_list(Tree).
 
 -spec(window(inflight()) -> list()).
-window(Inflight = {?MODULE, _MaxSize, Tree}) ->
+window(Inflight = ?Inflight(Tree)) ->
     case gb_trees:is_empty(Tree) of
-        true  -> [];
+        true -> [];
         false -> [Key || {Key, _Val} <- [smallest(Inflight), largest(Inflight)]]
     end.
 
 -spec(size(inflight()) -> non_neg_integer()).
-size({?MODULE, _MaxSize, Tree}) ->
+size(?Inflight(Tree)) ->
     gb_trees:size(Tree).
 
 -spec(max_size(inflight()) -> non_neg_integer()).
-max_size({?MODULE, MaxSize, _Tree}) ->
+max_size(?Inflight(MaxSize, _Tree)) ->
     MaxSize.
 

+ 1 - 1
src/emqx_keepalive.erl

@@ -18,7 +18,7 @@
 
 -record(keepalive, {statfun, statval, tsec, tmsg, tref, repeat = 0}).
 
--type(keepalive() :: #keepalive{}).
+-opaque(keepalive() :: #keepalive{}).
 
 -export_type([keepalive/0]).
 

+ 3 - 2
src/emqx_protocol.erl

@@ -34,6 +34,8 @@
 -export([send/2]).
 -export([shutdown/2]).
 
+-export_type([state/0]).
+
 -record(pstate, {
           zone,
           sendfun,
@@ -66,8 +68,7 @@
           topic_alias_maximum
         }).
 
--type(state() :: #pstate{}).
--export_type([state/0]).
+-opaque(state() :: #pstate{}).
 
 -ifdef(TEST).
 -compile(export_all).

+ 1 - 1
src/emqx_topic.erl

@@ -29,7 +29,7 @@
 -type(topic() :: binary()).
 -type(word() :: '' | '+' | '#' | binary()).
 -type(words() :: list(word())).
--type(triple() :: {root | binary(), word(), binary()}).
+-opaque(triple() :: {root | binary(), word(), binary()}).
 
 -export_type([group/0, topic/0, word/0, triple/0]).