add rule sup

This commit is contained in:
安礼成 2023-03-01 16:05:26 +08:00
parent dd234af4d0
commit 9f49fa9b5c
3 changed files with 170 additions and 4 deletions

View File

@ -0,0 +1,98 @@
%%%-------------------------------------------------------------------
%%% @author licheng5
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 01. 3 2023 16:03
%%%-------------------------------------------------------------------
-module(iot_router).
-author("licheng5").
-behaviour(gen_server).
%% API
-export([start_link/0]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]).
-define(SERVER, ?MODULE).
-record(iot_router_state, {}).
%%%===================================================================
%%% API
%%%===================================================================
%% @doc Spawns the server and registers the local name (unique)
-spec(start_link() ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
%% @private
%% @doc Initializes the server
-spec(init(Args :: term()) ->
{ok, State :: #iot_router_state{}} | {ok, State :: #iot_router_state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([]) ->
{ok, #iot_router_state{}}.
%% @private
%% @doc Handling call messages
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
State :: #iot_router_state{}) ->
{reply, Reply :: term(), NewState :: #iot_router_state{}} |
{reply, Reply :: term(), NewState :: #iot_router_state{}, timeout() | hibernate} |
{noreply, NewState :: #iot_router_state{}} |
{noreply, NewState :: #iot_router_state{}, timeout() | hibernate} |
{stop, Reason :: term(), Reply :: term(), NewState :: #iot_router_state{}} |
{stop, Reason :: term(), NewState :: #iot_router_state{}}).
handle_call(_Request, _From, State = #iot_router_state{}) ->
{reply, ok, State}.
%% @private
%% @doc Handling cast messages
-spec(handle_cast(Request :: term(), State :: #iot_router_state{}) ->
{noreply, NewState :: #iot_router_state{}} |
{noreply, NewState :: #iot_router_state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #iot_router_state{}}).
handle_cast(_Request, State = #iot_router_state{}) ->
{noreply, State}.
%% @private
%% @doc Handling all non call/cast messages
-spec(handle_info(Info :: timeout() | term(), State :: #iot_router_state{}) ->
{noreply, NewState :: #iot_router_state{}} |
{noreply, NewState :: #iot_router_state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #iot_router_state{}}).
handle_info(_Info, State = #iot_router_state{}) ->
{noreply, State}.
%% @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 :: #iot_router_state{}) -> term()).
terminate(_Reason, _State = #iot_router_state{}) ->
ok.
%% @private
%% @doc Convert process state when code is changed
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #iot_router_state{},
Extra :: term()) ->
{ok, NewState :: #iot_router_state{}} | {error, Reason :: term()}).
code_change(_OldVsn, State = #iot_router_state{}, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================

View File

@ -0,0 +1,61 @@
%%%-------------------------------------------------------------------
%%% @author licheng5
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 01. 3 2023 16:01
%%%-------------------------------------------------------------------
-module(iot_router_sup).
-author("licheng5").
-behaviour(supervisor).
%% API
-export([start_link/0]).
%% Supervisor callbacks
-export([init/1]).
-define(SERVER, ?MODULE).
%%%===================================================================
%%% API functions
%%%===================================================================
%% @doc Starts the supervisor
-spec(start_link() -> {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
%%%===================================================================
%%% Supervisor callbacks
%%%===================================================================
%% @private
%% @doc Whenever a supervisor is started using supervisor:start_link/[2,3],
%% this function is called by the new process to find out about
%% restart strategy, maximum restart frequency and child
%% specifications.
-spec(init(Args :: term()) ->
{ok, {SupFlags :: {RestartStrategy :: supervisor:strategy(),
MaxR :: non_neg_integer(), MaxT :: non_neg_integer()},
[ChildSpec :: supervisor:child_spec()]}}
| ignore | {error, Reason :: term()}).
init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
AChild = #{
id => 'iot_router',
start => {'iot_router', start_link, []},
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['iot_router']
},
{ok, {SupFlags, [AChild]}}.
%%%===================================================================
%%% Internal functions
%%%===================================================================

View File

@ -26,10 +26,17 @@ start_link() ->
%% type => worker(), % optional
%% modules => modules()} % optional
init([]) ->
SupFlags = #{strategy => one_for_all,
intensity => 0,
period => 1},
ChildSpecs = [],
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
ChildSpecs = [
#{
id => 'iot_router_sup',
start => {'iot_router_sup', start_link, []},
restart => permanent,
shutdown => 2000,
type => supervisor,
modules => ['iot_router_sup']
}
],
{ok, {SupFlags, ChildSpecs}}.
%% internal functions