-module(esockd_server).
-behaviour(gen_server).
-export([start_link/0]).
%% stats API 导出模块接口给外部调用
-export([stats_fun/2, init_stats/2, get_stats/1, inc_stats/3, dec_stats/3, del_stats/1]).
%% gen_server callbacks 通用回到方法
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-record(state, {}).
-define(SERVER, ?MODULE).
%% 定义统计表 esockd_stats
-define(STATS_TAB, esockd_stats).
%%------------------------------------------------------------------------------
%% API
%%------------------------------------------------------------------------------
%% 启动模块
-spec(start_link() -> {ok, pid()} | ignore | {error, term()}).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%% 统计度量
-spec(stats_fun({atom(), esockd:listen_on()}, atom()) -> fun()).
stats_fun({Protocol, ListenOn}, Metric) ->
init_stats({Protocol, ListenOn}, Metric),
fun({inc, Num}) -> esockd_server:inc_stats({Protocol, ListenOn}, Metric, Num);
({dec, Num}) -> esockd_server:dec_stats({Protocol, ListenOn}, Metric, Num)
end.
-spec(init_stats({atom(), esockd:listen_on()}, atom()) -> ok).
init_stats({Protocol, ListenOn}, Metric) ->
gen_server:call(?SERVER, {init, {Protocol, ListenOn}, Metric}).
%% 获取统计信息
-spec(get_stats({atom(), esockd:listen_on()}) -> [{atom(), non_neg_integer()}]).
get_stats({Protocol, ListenOn}) ->
[{Metric, Val} || [Metric, Val] <- ets:match(?STATS_TAB, {{{Protocol, ListenOn}, '$1'}, '$2'})].
%% 增加
-spec(inc_stats({atom(), esockd:listen_on()}, atom(), pos_integer()) -> any()).
inc_stats({Protocol, ListenOn}, Metric, Num) when is_integer(Num) ->
update_counter({{Protocol, ListenOn}, Metric}, Num).
%% 减少
-spec(dec_stats({atom(), esockd:listen_on()}, atom(), pos_integer()) -> any()).
dec_stats({Protocol, ListenOn}, Metric, Num) when is_integer(Num) ->
update_counter({{Protocol, ListenOn}, Metric}, -Num).
%% 通过{Protocol, ListenOn}更新计数
update_counter(Key, Num) ->
ets:update_counter(?STATS_TAB, Key, {2, Num}).
%% 异步发送消息 {del, {Protocol, ListenOn}},次消息被handle_cast 函数处理
-spec(del_stats({atom(), esockd:listen_on()}) -> ok).
del_stats({Protocol, ListenOn}) ->
gen_server:cast(?SERVER, {del, {Protocol, ListenOn}}).
%%------------------------------------------------------------------------------
%% gen_server callbacks
%%------------------------------------------------------------------------------
init([]) ->
%% io:format("esockd esockd_server init create stats table ~n"),
_ = ets:new(?STATS_TAB, [public, set, named_table, {write_concurrency, true}]),
{ok, #state{}}.
%% 同步插入数据到esockd_stats表中
handle_call({init, {Protocol, ListenOn}, Metric}, _From, State) ->
_ = ets:insert(?STATS_TAB, {{{Protocol, ListenOn}, Metric}, 0}),
{reply, ok, State, hibernate};
handle_call(Req, _From, State) ->
error_logger:error_msg("[~s] unexpected call: ~p", [?MODULE, Req]),
{reply, ignore, State}.
%% 异步通过key {Protocol, ListenOn} 删除数据
handle_cast({del, {Protocol, ListenOn}}, State) ->
ets:match_delete(?STATS_TAB, {{{Protocol, ListenOn}, '_'}, '_'}),
{noreply, State, hibernate};
handle_cast(Msg, State) ->
error_logger:error_msg("[~s] unexpected cast: ~p", [?MODULE, Msg]),
{noreply, State}.
handle_info(Info, State) ->
error_logger:error_msg("[~s] unexpected info: ~p", [?MODULE, Info]),
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
该模块主要是统计工作,结合其他模块一起来完成,下一篇介绍 esockd_listener_sup 模块