多进程工作者的全局定时器 替代单进程工作模式的erlangtimer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 

54 行
1.0 KiB

-module(gTimer).
-include("gTimer.hrl").
-export([
start/0
, stop/0
, startWork/1
, setTimer/2
, getTimer/1
, delTimer/1
]).
timerName(Idx) ->
binary_to_atom(<<"$gtWSork_", (integer_to_binary(Idx))/binary>>).
startWork(Cnt) when Cnt > 0 ->
case ?gTimerCfg:getV(?workCnt) of
0 ->
NameList = [{Idx, timerName(Idx)} || Idx <- lists:seq(1, Cnt)],
[supervisor:start_child(gTimer_sup, [WorkName]) || {_Idx, WorkName} <- NameList],
CfgList = [{?workCnt, Cnt} | NameList],
gtKvsToBeam:load(?gTimerCfg, CfgList),
ok1;
_Cnt ->
{error, started}
end.
start() ->
application:ensure_all_started(gTimer).
stop() ->
gtKvsToBeam:load(?gTimerCfg, [{?workCnt, 0}]),
application:stop(gTimer).
setTimer(Time, Msg) ->
Cnt = ?gTimerCfg:getV(?workCnt),
Idx = erlang:phash2(self(), Cnt) + 1,
erlang:start_timer(Time, ?gTimerCfg:getV(Idx), Msg).
getTimer(TimerRef) ->
erlang:read_timer(TimerRef).
delTimer(TimerRef) ->
erlang:cancel_timer(TimerRef).