From cc9a920d3171de6ea59225461d9af5b5a8156566 Mon Sep 17 00:00:00 2001 From: SisMaker <156736github> Date: Sun, 3 Apr 2022 23:57:45 +0800 Subject: [PATCH] =?UTF-8?q?ft=EF=BC=9A=20=E5=A4=9A=E5=B7=A5=E4=BD=9C?= =?UTF-8?q?=E8=80=85=E7=9A=84=E5=85=A8=E5=B1=80=E5=AE=9A=E6=97=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gTimer.erl | 26 +++++++++++--------------- src/gTimer_sup.erl | 26 +++++++++++++------------- src/timer/gtWork.erl | 8 ++++++-- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/gTimer.erl b/src/gTimer.erl index 4a4ab3f..1699721 100644 --- a/src/gTimer.erl +++ b/src/gTimer.erl @@ -12,11 +12,10 @@ , delTimer/1 ]). - - timerName(Idx) -> binary_to_atom(<<"$gtWSork_", (integer_to_binary(Idx))/binary>>). +-spec startWork(Cnt :: non_neg_integer()) -> ok | {error, term()}. startWork(Cnt) when Cnt > 0 -> case ?gTimerCfg:getV(?workCnt) of 0 -> @@ -24,7 +23,7 @@ startWork(Cnt) when Cnt > 0 -> [supervisor:start_child(gTimer_sup, [WorkName]) || {_Idx, WorkName} <- NameList], CfgList = [{?workCnt, Cnt} | NameList], gtKvsToBeam:load(?gTimerCfg, CfgList), - ok1; + ok; _Cnt -> {error, started} end. @@ -36,25 +35,22 @@ stop() -> gtKvsToBeam:load(?gTimerCfg, [{?workCnt, 0}]), application:stop(gTimer). -setTimer(Time, Msg) -> +-spec setTimer(Time :: non_neg_integer(), MFA :: {module(), atom(), term()}) -> reference(). +setTimer(Time, MFA) -> Cnt = ?gTimerCfg:getV(?workCnt), Idx = rand:uniform(Cnt), - erlang:start_timer(Time, ?gTimerCfg:getV(Idx), Msg). + erlang:start_timer(Time, ?gTimerCfg:getV(Idx), MFA). -setTimer(Time, Msg, Strategy) -> +-spec setTimer(Time :: non_neg_integer(), MFA :: {module(), atom(), term()}, Strategy :: rand | bind) -> reference(). +setTimer(Time, MFA, Strategy) -> Cnt = ?gTimerCfg:getV(?workCnt), Idx = ?IIF(Strategy == rand, rand:uniform(Cnt), erlang:phash2(self(), Cnt) + 1), - erlang:start_timer(Time, ?gTimerCfg:getV(Idx), Msg). + erlang:start_timer(Time, ?gTimerCfg:getV(Idx), MFA). +-spec getTimer(TimerRef :: reference()) -> false | non_neg_integer(). getTimer(TimerRef) -> erlang:read_timer(TimerRef). +-spec delTimer(TimerRef :: reference()) -> false | non_neg_integer(). delTimer(TimerRef) -> - erlang:cancel_timer(TimerRef) . - - - - - - - + erlang:cancel_timer(TimerRef) . \ No newline at end of file diff --git a/src/gTimer_sup.erl b/src/gTimer_sup.erl index 12e5ee3..3af7b66 100644 --- a/src/gTimer_sup.erl +++ b/src/gTimer_sup.erl @@ -11,17 +11,17 @@ start_link() -> supervisor:start_link({local, ?SERVER}, ?MODULE, []). -init([]) -> - SupFlags = #{strategy => simple_one_for_one, intensity => 1000, period => 3600}, - ChildSpecs = [ - #{ - id => gtWork, - start => {gtWork, start_link, []}, - restart => permanent, - shutdown => 3000, - type => worker, - modules => [gtWork] - } - ], - {ok, {SupFlags, ChildSpecs}}. +init(_) -> + SupFlags = #{strategy => simple_one_for_one, intensity => 1000, period => 3600}, + ChildSpecs = [ + #{ + id => gtWork, + start => {gtWork, start_link, []}, + restart => permanent, + shutdown => 3000, + type => worker, + modules => [gtWork] + } + ], + {ok, {SupFlags, ChildSpecs}}. diff --git a/src/timer/gtWork.erl b/src/timer/gtWork.erl index 65c8f1e..3a4d0a7 100644 --- a/src/timer/gtWork.erl +++ b/src/timer/gtWork.erl @@ -32,9 +32,13 @@ handleCall(_Msg, _State, _FROM) -> handleCast(_Msg, _State) -> kpS. -handleInfo({timeout, TimerRef, Msg}, _State) -> +handleInfo({timeout, TimerRef, MFA}, _State) -> %% 确认Msg格式 然后做分发处理 - io:format("the timer time out ~p ~p ~n", [TimerRef, Msg]), + {M, F, A} = MFA, + try M:F(A, TimerRef) + catch C:R -> + error_logger:error_msg("gTimer timeout error MFA:~p C:~p R:~p~n", [MFA, C, R]) + end, kpS; handleInfo(_Msg, _State) -> kpS.