博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Supervision 行为模式
阅读量:6710 次
发布时间:2019-06-25

本文共 6102 字,大约阅读时间需要 20 分钟。

官方链接:http://erlang.org/doc/man/supervisor.html

     http://erlang.org/doc/design_principles/sup_princ.html

 

 

The supervisor is responsible for starting, stopping and monitoring its child processes. The basic idea of a supervisor is that it should keep its child processes alive by restarting them when necessary.

The children of a supervisor is defined as a list of child specifications. When the supervisor is started, the child processes are started in order from left to right according to this list. When the supervisor terminates, it first terminates its child processes in reversed start order, from right to left.

A supervisor can have one of the following restart strategies:

  • one_for_one - if one child process terminates and should be restarted, only that child process is affected.

  • one_for_all - if one child process terminates and should be restarted, all other child processes are terminated and then all child processes are restarted.

  • rest_for_one - if one child process terminates and should be restarted, the 'rest' of the child processes -- i.e. the child processes after the terminated child process in the start order -- are terminated. Then the terminated child process and all child processes after it are restarted.

  • simple_one_for_one - a simplified one_for_one supervisor, where all child processes are dynamically added instances of the same process type, i.e. running the same code.

    The functions delete_child/2 and restart_child/2 are invalid for simple_one_for_one supervisors and will return {error,simple_one_for_one} if the specified supervisor uses this restart strategy.

    The function terminate_child/2 can be used for children under simple_one_for_one supervisors by giving the child's pid() as the second argument. If instead the child specification identifier is used, terminate_child/2 will return {error,simple_one_for_one}.

    Because a simple_one_for_one supervisor could have many children, it shuts them all down at same time. So, order in which they are stopped is not defined. For the same reason, it could have an overhead with regards to the Shutdown strategy.

To prevent a supervisor from getting into an infinite loop of child process terminations and restarts, a maximum restart frequency is defined using two integer values MaxR and MaxT. If more than MaxR restarts occur within MaxT seconds, the supervisor terminates all child processes and then itself.

This is the type definition of a child specification:

child_spec() = {Id,StartFunc,Restart,Shutdown,Type,Modules} Id = term() StartFunc = {M,F,A}  M = F = atom()  A = [term()] Restart = permanent | transient | temporary Shutdown = brutal_kill | int()>0 | infinity Type = worker | supervisor Modules = [Module] | dynamic  Module = atom()
  • Id is a name that is used to identify the child specification internally by the supervisor.

  • StartFunc defines the function call used to start the child process. It should be a module-function-arguments tuple {M,F,A} used as apply(M,F,A).

    The start function must create and link to the child process, and should return {ok,Child} or {ok,Child,Info} where Child is the pid of the child process and Info an arbitrary term which is ignored by the supervisor.It should be (or result in) a call to supervisor:start_link, gen_server:start_link, gen_fsm:start_link or gen_event:start_link. (Or a function compliant with these functions, see for details.

   

  • The start function can also return ignore if the child process for some reason cannot be started, in which case the child specification will be kept by the supervisor (unless it is a temporary child) but the non-existing child process will be ignored.

    If something goes wrong, the function may also return an error tuple {error,Error}.

    Note that the start_link functions of the different behaviour modules fulfill the above requirements.

  • Restart defines when a terminated child process should be restarted. A permanent child process should always be restarted, a temporary child process should never be restarted (even when the supervisor's restart strategy is rest_for_one or one_for_all and a sibling's death causes the temporary process to be terminated) and a transient child process should be restarted only if it terminates abnormally, i.e. with another exit reason than normal, shutdown or {shutdown,Term}.

  • Shutdown defines how a child process should be terminated. brutal_kill means the child process will be unconditionally terminated using exit(Child,kill). An integer timeout value means that the supervisor will tell the child process to terminate by calling exit(Child,shutdown) and then wait for an exit signal with reason shutdown back from the child process. If no exit signal is received within the specified number of milliseconds, the child process is unconditionally terminated using exit(Child,kill).

    If the child process is another supervisor, Shutdown should be set to infinity to give the subtree ample time to shutdown. It is also allowed to set it to infinity, if the child process is a worker.

    Warning

    Be careful by setting the Shutdown strategy to infinity when the child process is a worker. Because, in this situation, the termination of the supervision tree depends on the child process, it must be implemented in a safe way and its cleanup procedure must always return.

    Note that all child processes implemented using the standard OTP behavior modules automatically adhere to the shutdown protocol.

  • Type specifies if the child process is a supervisor or a worker.

  • Modules is used by the release handler during code replacement to determine which processes are using a certain module. As a rule of thumb Modules should be a list with one element [Module], where Module is the callback module, if the child process is a supervisor, gen_server or gen_fsm. If the child process is an event manager (gen_event) with a dynamic set of callback modules, Modules should be dynamic. See for more information about release handling.

  • Internally, the supervisor also keeps track of the pid Child of the child process, or undefined if no pid exists.

转载于:https://www.cnblogs.com/ribavnu/p/3528315.html

你可能感兴趣的文章
关于Eclipse生成和导入Patch文件.
查看>>
如何使用Photoshop(PS)将图片的底色变为透明
查看>>
IDEA实现序列号接口
查看>>
人件札记:保持高效的办公室环境
查看>>
Mysql中使用流式查询避免数据量过大导致OOM
查看>>
为什么中台是传统企业数字化转型的关键?
查看>>
中国技术开放日专场在美国旧金山隆重开幕
查看>>
从责任界定和问题预警角度 解读全栈溯源对DevOps的价值
查看>>
百度发布开源智能边缘计算平台OpenEdge
查看>>
JavaScript引擎V8 5.1遵循了更多的ECMAScript规范并支持WASM
查看>>
广度、深度、易用性,详解6大机器学习云
查看>>
雇佣和留住开发人员,打造优秀的团队
查看>>
关于5G被激烈讨论的那些争端和冲突
查看>>
Jenkins部署码云SpringBoot项目
查看>>
抛弃NVelocity,来玩玩Razor
查看>>
在JavaScript面向对象编程中使用继承(1)
查看>>
高铁与机场成交通信息化建设的双驾马车
查看>>
chmod命令
查看>>
货币的起源和职能是什么?绘制货币资金管理思维导图简单的方法介绍
查看>>
springboot+kafka+elk+docker+docker-compose+centos搭建日志收集系统
查看>>