Improve the 'info/1', 'attrs/1' and 'stats/1' APIs

This commit is contained in:
Feng Lee 2019-07-25 09:26:36 +08:00
parent b4a0a1c228
commit 64148ac0e8
3 changed files with 128 additions and 80 deletions

View File

@ -27,11 +27,6 @@
-export([ info/1 -export([ info/1
, info/2 , info/2
, attrs/1 , attrs/1
, stats/1
]).
-export([ client_id/1
, session/1
]). ]).
-export([ init/2 -export([ init/2
@ -54,39 +49,66 @@
will_msg :: emqx_types:message(), will_msg :: emqx_types:message(),
enable_acl :: boolean(), enable_acl :: boolean(),
is_bridge :: boolean(), is_bridge :: boolean(),
connected :: boolean(),
connected_at :: erlang:timestamp(),
topic_aliases :: map(), topic_aliases :: map(),
alias_maximum :: map() alias_maximum :: map()
}). }).
-opaque(proto_state() :: #protocol{}). -opaque(proto_state() :: #protocol{}).
info(#protocol{client = Client, session = Session}) -> -spec(info(proto_state()) -> emqx_types:infos()).
lists:append([maps:to_list(Client), emqx_session:info(Session)]). info(#protocol{proto_name = ProtoName,
proto_ver = ProtoVer,
client = Client,
session = Session,
keepalive = Keepalive,
will_msg = WillMsg,
enable_acl = EnableAcl,
is_bridge = IsBridge,
topic_aliases = Aliases}) ->
#{proto_name => ProtoName,
proto_ver => ProtoVer,
client => Client,
session => emqx_session:info(Session),
keepalive => Keepalive,
will_msg => WillMsg,
enable_acl => EnableAcl,
is_bridge => IsBridge,
topic_aliases => Aliases
}.
info(zone, #protocol{client = #{zone := Zone}}) -> -spec(info(atom(), proto_state()) -> term()).
Zone;
info(proto_name, #protocol{proto_name = ProtoName}) -> info(proto_name, #protocol{proto_name = ProtoName}) ->
ProtoName; ProtoName;
info(proto_ver, #protocol{proto_ver = ProtoVer}) -> info(proto_ver, #protocol{proto_ver = ProtoVer}) ->
ProtoVer; ProtoVer;
info(client, #protocol{client = Client}) ->
Client;
info(zone, #protocol{client = #{zone := Zone}}) ->
Zone;
info(client_id, #protocol{client = #{client_id := ClientId}}) ->
ClientId;
info(session, #protocol{session = Session}) ->
Session;
info(keepalive, #protocol{keepalive = Keepalive}) -> info(keepalive, #protocol{keepalive = Keepalive}) ->
Keepalive. Keepalive;
info(is_bridge, #protocol{is_bridge = IsBridge}) ->
IsBridge;
info(topic_aliases, #protocol{topic_aliases = Aliases}) ->
Aliases.
attrs(#protocol{}) -> attrs(#protocol{proto_name = ProtoName,
#{}. proto_ver = ProtoVer,
client = Client,
stats(#protocol{}) -> session = Session,
[]. keepalive = Keepalive,
is_bridge = IsBridge}) ->
-spec(client_id(proto_state()) -> emqx_types:client_id()). #{proto_name => ProtoName,
client_id(#protocol{client = #{client_id := ClientId}}) -> proto_ver => ProtoVer,
ClientId. client => Client,
session => emqx_session:attrs(Session),
-spec(session(proto_state()) -> emqx_session:session()). keepalive => Keepalive,
session(#protocol{session = Session}) -> is_bridge => IsBridge
Session. }.
-spec(init(map(), proplists:proplist()) -> proto_state()). -spec(init(map(), proplists:proplist()) -> proto_state()).
init(ConnInfo, Options) -> init(ConnInfo, Options) ->
@ -105,8 +127,7 @@ init(ConnInfo, Options) ->
#protocol{client = Client, #protocol{client = Client,
mountfun = MountFun, mountfun = MountFun,
enable_acl = EnableAcl, enable_acl = EnableAcl,
is_bridge = false, is_bridge = false
connected = false
}. }.
peer_cert_as_username(Peercert, Options) -> peer_cert_as_username(Peercert, Options) ->
@ -382,10 +403,7 @@ handle_connect(#mqtt_packet_connect{proto_name = ProtoName,
case open_session(ConnPkt, PState) of case open_session(ConnPkt, PState) of
{ok, Session, SP} -> {ok, Session, SP} ->
PState1 = PState#protocol{client = Client1, PState1 = PState#protocol{client = Client1,
session = Session, session = Session},
connected = true,
connected_at = os:timestamp()
},
ok = emqx_cm:register_channel(ClientId), ok = emqx_cm:register_channel(ClientId),
{ok, SP, PState1}; {ok, SP, PState1};
{error, Error} -> {error, Error} ->

View File

@ -48,9 +48,12 @@
-include("logger.hrl"). -include("logger.hrl").
-include("types.hrl"). -include("types.hrl").
-logger_header("[Session]").
-export([init/1]). -export([init/1]).
-export([ info/1 -export([ info/1
, attrs/1
, stats/1 , stats/1
]). ]).
@ -80,12 +83,12 @@
%% Clean Start Flag %% Clean Start Flag
clean_start :: boolean(), clean_start :: boolean(),
%% Max subscriptions allowed
max_subscriptions :: non_neg_integer(),
%% Clients Subscriptions. %% Clients Subscriptions.
subscriptions :: map(), subscriptions :: map(),
%% Max subscriptions allowed
max_subscriptions :: non_neg_integer(),
%% Upgrade QoS? %% Upgrade QoS?
upgrade_qos :: boolean(), upgrade_qos :: boolean(),
@ -112,12 +115,12 @@
%% Inflight QoS2 messages received from client and waiting for pubrel. %% Inflight QoS2 messages received from client and waiting for pubrel.
awaiting_rel :: map(), awaiting_rel :: map(),
%% Awaiting PUBREL Timer
await_rel_timer :: maybe(reference()),
%% Max Packets Awaiting PUBREL %% Max Packets Awaiting PUBREL
max_awaiting_rel :: non_neg_integer(), max_awaiting_rel :: non_neg_integer(),
%% Awaiting PUBREL Timer
await_rel_timer :: maybe(reference()),
%% Awaiting PUBREL Timeout %% Awaiting PUBREL Timeout
await_rel_timeout :: timeout(), await_rel_timeout :: timeout(),
@ -133,8 +136,6 @@
-opaque(session() :: #session{}). -opaque(session() :: #session{}).
-logger_header("[Session]").
-define(DEFAULT_BATCH_N, 1000). -define(DEFAULT_BATCH_N, 1000).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -170,10 +171,10 @@ init_mqueue(Zone) ->
}). }).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Info, Stats of Session %% Infos of the session
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec(info(session()) -> proplists:proplist()). -spec(info(session()) -> emqx_types:infos()).
info(#session{clean_start = CleanStart, info(#session{clean_start = CleanStart,
max_subscriptions = MaxSubscriptions, max_subscriptions = MaxSubscriptions,
subscriptions = Subscriptions, subscriptions = Subscriptions,
@ -187,37 +188,58 @@ info(#session{clean_start = CleanStart,
await_rel_timeout = AwaitRelTimeout, await_rel_timeout = AwaitRelTimeout,
expiry_interval = ExpiryInterval, expiry_interval = ExpiryInterval,
created_at = CreatedAt}) -> created_at = CreatedAt}) ->
[{clean_start, CleanStart}, #{clean_start => CleanStart,
{max_subscriptions, MaxSubscriptions}, subscriptions => Subscriptions,
{subscriptions, Subscriptions}, max_subscriptions => MaxSubscriptions,
{upgrade_qos, UpgradeQoS}, upgrade_qos => UpgradeQoS,
{inflight, Inflight}, inflight => emqx_inflight:size(Inflight),
{retry_interval, RetryInterval}, max_inflight => emqx_inflight:max_size(Inflight),
{mqueue_len, emqx_mqueue:len(MQueue)}, retry_interval => RetryInterval,
{next_pkt_id, PacketId}, mqueue_len => emqx_mqueue:len(MQueue),
{awaiting_rel, AwaitingRel}, max_mqueue => emqx_mqueue:max_len(MQueue),
{max_awaiting_rel, MaxAwaitingRel}, mqueue_dropped => emqx_mqueue:dropped(MQueue),
{await_rel_timeout, AwaitRelTimeout}, next_pkt_id => PacketId,
{expiry_interval, ExpiryInterval div 1000}, awaiting_rel => maps:size(AwaitingRel),
{created_at, CreatedAt}]. max_awaiting_rel => MaxAwaitingRel,
await_rel_timeout => AwaitRelTimeout,
expiry_interval => ExpiryInterval div 1000,
created_at => CreatedAt
}.
%% @doc Get session stats. %%--------------------------------------------------------------------
-spec(stats(session()) -> list({atom(), non_neg_integer()})). %% Attrs of the session
stats(#session{max_subscriptions = MaxSubscriptions, %%--------------------------------------------------------------------
subscriptions = Subscriptions,
-spec(attrs(session()) -> emqx_types:attrs()).
attrs(#session{clean_start = CleanStart,
expiry_interval = ExpiryInterval,
created_at = CreatedAt}) ->
#{clean_start => CleanStart,
expiry_interval => ExpiryInterval,
created_at => CreatedAt
}.
%%--------------------------------------------------------------------
%% Stats of the session
%%--------------------------------------------------------------------
%% @doc Get stats of the session.
-spec(stats(session()) -> emqx_types:stats()).
stats(#session{subscriptions = Subscriptions,
max_subscriptions = MaxSubscriptions,
inflight = Inflight, inflight = Inflight,
mqueue = MQueue, mqueue = MQueue,
max_awaiting_rel = MaxAwaitingRel, awaiting_rel = AwaitingRel,
awaiting_rel = AwaitingRel}) -> max_awaiting_rel = MaxAwaitingRel}) ->
[{max_subscriptions, MaxSubscriptions}, [{subscriptions, maps:size(Subscriptions)},
{subscriptions_count, maps:size(Subscriptions)}, {max_subscriptions, MaxSubscriptions},
{inflight, emqx_inflight:size(Inflight)},
{max_inflight, emqx_inflight:max_size(Inflight)}, {max_inflight, emqx_inflight:max_size(Inflight)},
{inflight_len, emqx_inflight:size(Inflight)},
{max_mqueue, emqx_mqueue:max_len(MQueue)},
{mqueue_len, emqx_mqueue:len(MQueue)}, {mqueue_len, emqx_mqueue:len(MQueue)},
{max_mqueue, emqx_mqueue:max_len(MQueue)},
{mqueue_dropped, emqx_mqueue:dropped(MQueue)}, {mqueue_dropped, emqx_mqueue:dropped(MQueue)},
{max_awaiting_rel, MaxAwaitingRel}, {awaiting_rel, maps:size(AwaitingRel)},
{awaiting_rel_len, maps:size(AwaitingRel)}]. {max_awaiting_rel, MaxAwaitingRel}].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Client -> Broker: SUBSCRIBE %% Client -> Broker: SUBSCRIBE

View File

@ -72,6 +72,11 @@
, command/0 , command/0
]). ]).
-export_type([ infos/0
, attrs/0
, stats/0
]).
-type(zone() :: emqx_zone:zone()). -type(zone() :: emqx_zone:zone()).
-type(ver() :: ?MQTT_PROTO_V3 | ?MQTT_PROTO_V4 | ?MQTT_PROTO_V5). -type(ver() :: ?MQTT_PROTO_V3 | ?MQTT_PROTO_V4 | ?MQTT_PROTO_V5).
-type(qos() :: ?QOS_0 | ?QOS_1 | ?QOS_2). -type(qos() :: ?QOS_0 | ?QOS_1 | ?QOS_2).
@ -87,7 +92,6 @@
rap := 0 | 1, rap := 0 | 1,
nl := 0 | 1, nl := 0 | 1,
qos := qos(), qos := qos(),
rc => reason_code(),
share => binary(), share => binary(),
atom() => term() atom() => term()
}). }).
@ -143,3 +147,7 @@
-type(plugin() :: #plugin{}). -type(plugin() :: #plugin{}).
-type(command() :: #command{}). -type(command() :: #command{}).
-type(infos() :: #{atom() => term()}).
-type(attrs() :: #{atom() => term()}).
-type(stats() :: list({atom(), non_neg_integer()})).