`

erlang行为模式

 
阅读更多
一、supervisor监督者

重启策略:
one_for_one 只影响当前child
one_for_all - 影响所有children
rest_for_one - 影响当前和"rest" order 后面的child
simple_one_for_one - 所有的child运行同一个模块

delete_child/2 and restart_child/2 are invalid for simple_one_for_one

child_spec() = {Id, StartFunc, Restart, Shutdown, Type, Modules}

Id -> 标示符.

StartFunc -> {M,F,A}

Restart :permanent | transient | temporary
permanent -> always be restarted
temporary -> never be restarted
transient -> 非正常死亡是会重启,exit
                  即exit reason 不是 normal, shutdown or {shutdown,Term}之一.

Shutdown = brutal_kill | int()>0 | infinity
Type = worker | supervisor
Modules = [Module] | dynamic  用于代码升级


创建并链接监督者进程:
start_link(Module, Args) ->
start_link(SupName, Module, Args) ->  // 注册进程名SupName

SupName -> {local, Name::atom()} | {global, Name::atom()}

Args 当做回调模块 Module:init/1 的参数.

如果有child进程启动失败,那么监督者进程也会启动失败


启动子进程:
start_child(SupRef, ChildSpec) -> startchild_ret()

SupRef -> pid | name | {name, node} | {global, name}  表示监督者进程
当  simple_one_for_one , ChildSpec 表示一个参数List, apply(M, F, A++List)
当 others,  ChildSpec 才表示子进程的规范,如果规范Id已经存在,那么会报错。

终止子进程:
terminate_child(SupRef, Id) -> Result
当 simple_one_for_one, Id表示子进程的pid
当 others  表示 规范Id.

删除规范Id (前提SpecId对应的进程已经终止)
delete_child(SupRef, Id) -> Result

重启子进程:
restart_child(SupRef, Id) -> Result

回调函数:
Module:init(Args) -> Result
Result -> {ok,{{RestartStrategy, MaxR, MaxT},[ChildSpec]}} | ignore

MaxR and MaxT 表示在MaxT时间重启的次数不能超过 MaxR


二、gen_server通用服务器

gen_server:start_link -----> Module:init/1
gen_server:call       -----> Module:handle_call/3
gen_server:cast       -----> Module:handle_cast/2
-                     -----> Module:handle_info/2
-                     -----> Module:terminate/2
-                     -----> Module:code_change/3

如果回调函数fail或返回bad value,那么gen_server将会终止.

a gen_server does not trap exit signals automatically, 必须要手动设置.

启动并链接gen_server:
start_link(Module, Args, Options) -> Result     // 不注册别名
start_link(ServerName, Module, Args, Options) -> Result  //
// ServerName -> {local, name} | {global, name}

同步调用:
call(ServerRef, Request) -> Reply
call(ServerRef, Request, Timeout) -> Reply

该调用等待 a reply arrives or a timeout occurs 才返回。


异步调用:
cast(ServerRef, Request) -> ok
就算gen_server不存在,也会立刻返回 ok.


初始化回调:Module:init(Args) -> Result
  Result = {ok,State} | {ok,State,Timeout} | {stop,Reason} | ignore

Timeout 为0则立刻触发一个 timeout消息。  为infinity则不会触发timeout。(毫秒)
注:handle_info 处理timeout消息

同步回调:
Module:handle_call(Request, From, State) -> Result 

Result = {reply,Reply,NewState} | {reply,Reply,NewState,Timeout}
  | {noreply,NewState} | {noreply,NewState,Timeout}
  | {stop,Reason,Reply,NewState} | {stop,Reason,NewState}

From 表示发送者的进程pid或注册名
State is the internal state of the gen_server.

reply 表示 Reply 作为 gen_server:call的返回值
noreply 表示 Reply表示显示地调用 gen_server:reply/2.
stop 表示 gen_server 将会调用 terminate(Reason, NewState) 进而终止.
NewState 更新后的 internal state


异步回调:
Module:handle_cast(Request, State) -> Result
Result = {noreply,NewState} | {noreply,NewState,Timeout}
  | {stop,Reason,NewState}


Timeout等消息回调:

Module:handle_info(Info, State) -> Result
Result = {noreply,NewState} | {noreply,NewState,Timeout}
  | {stop,Reason,NewState}

终止回调:
Module:terminate(Reason, State)
Reason = normal | shutdown | {shutdown,term()} | term()

当回调返回stop项式或产生异常,会体现在Reason里.


代码更新回调:
Module:code_change(OldVsn, State, Extra) -> {ok, NewState} | {error, Reason}


三、 application应用程序

加载 app规范 (包括依赖App的, 但不是加载代码) :
load(AppDescr) -> ok | {error, Reason}
load(AppDescr, Distributed) -> ok | {error, Reason}

application controller控制者,
AppDescr 可以是应用的名字 或者 应用规范的项式


启动应用:
start(Application) -> ok | {error, Reason}
start(Application, Type) -> ok | {error, Reason}

The application controller then creates an application master for the application. The application master is the group leader of all the processes in the application.

The application master starts the application by calling the application callback function Module:start/2 as defined by the application specification key mod.

停止应用:
stop(Application) -> ok | {error, Reason}

获取配置变量 (通过.app中配置):
get_env(Par) -> undefined | {ok, Val}
get_env(Application, Par) -> undefined | {ok, Val}

应用.app文件:在ebin目录下.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics