%%%------------------------------------------------------------------- %%% @author anlicheng %%% @copyright (C) 2024, %%% @doc %%% %%% @end %%% Created : 07. 5月 2024 10:30 %%%------------------------------------------------------------------- -module(endpoint_timer). -author("anlicheng"). -include("endpoint.hrl"). -behaviour(gen_server). %% API -export([start_link/1]). -export([task/3, ack/2, cleanup/1]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -record(state, { retry_interval = 0, %% 定时器 timer_map = #{} }). %%%=================================================================== %%% API %%%=================================================================== task(Pid, Id, Task) when is_pid(Pid), is_integer(Id), is_function(Task, 0) -> gen_server:cast(Pid, {task, Id, Task}). ack(Pid, Id) when is_pid(Pid), is_integer(Id) -> gen_server:cast(Pid, {ack, Id}). cleanup(Pid) when is_pid(Pid) -> gen_server:cast(Pid, cleanup). %% @doc Spawns the server and registers the local name (unique) -spec(start_link(RetryInterval :: integer()) -> {ok, Pid :: pid()} | ignore | {error, Reason :: term()}). start_link(RetryInterval) when is_integer(RetryInterval) -> gen_server:start_link(?MODULE, [RetryInterval], []). %%%=================================================================== %%% gen_server callbacks %%%=================================================================== %% @private %% @doc Initializes the server -spec(init(Args :: term()) -> {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} | {stop, Reason :: term()} | ignore). init([RetryInterval]) -> {ok, #state{retry_interval = RetryInterval}}. %% @private %% @doc Handling call messages -spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()}, State :: #state{}) -> {reply, Reply :: term(), NewState :: #state{}} | {reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} | {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), Reply :: term(), NewState :: #state{}} | {stop, Reason :: term(), NewState :: #state{}}). handle_call(_Request, _From, State = #state{}) -> {reply, ok, State}. %% @private %% @doc Handling cast messages -spec(handle_cast(Request :: term(), State :: #state{}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). handle_cast({task, Id, Task}, State = #state{retry_interval = RetryInterval, timer_map = TimerMap}) -> TimerRef = erlang:start_timer(RetryInterval, self(), {repost_ticker, {Id, Task}}), {noreply, State#state{timer_map = maps:put(Id, TimerRef, TimerMap)}}; %% 取消 handle_cast({ack, Id}, State = #state{timer_map = TimerMap}) -> case maps:take(Id, TimerMap) of error -> {noreply, State}; {TimerRef, NTimerMap} -> is_reference(TimerRef) andalso erlang:cancel_timer(TimerRef), {noreply, State#state{timer_map = NTimerMap}} end; handle_cast(cleanup, State = #state{timer_map = TimerMap}) -> lists:foreach(fun({_, TimerRef}) -> catch erlang:cancel_timer(TimerRef) end, maps:to_list(TimerMap)), {noreply, State#state{timer_map = #{}}}. %% @private %% @doc Handling all non call/cast messages -spec(handle_info(Info :: timeout() | term(), State :: #state{}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). handle_info({timeout, _, {repost_ticker, {Id, Task}}}, State = #state{retry_interval = RetryInterval, timer_map = TimerMap}) -> Task(), TimerRef = erlang:start_timer(RetryInterval, self(), {repost_ticker, {Id, Task}}), {noreply, State#state{timer_map = maps:put(Id, TimerRef, TimerMap)}}. %% @private %% @doc This function is called by a gen_server when it is about to %% terminate. It should be the opposite of Module:init/1 and do any %% necessary cleaning up. When it returns, the gen_server terminates %% with Reason. The return value is ignored. -spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()), State :: #state{}) -> term()). terminate(_Reason, _State = #state{}) -> ok. %% @private %% @doc Convert process state when code is changed -spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{}, Extra :: term()) -> {ok, NewState :: #state{}} | {error, Reason :: term()}). code_change(_OldVsn, State = #state{}, _Extra) -> {ok, State}. %%%=================================================================== %%% Internal functions %%%===================================================================