官方链接: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 supervisor(3) 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 OTP Design Principles 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.

Supervision 行为模式的更多相关文章

  1. [设计模式] 8 组合模式 Composite

    DP书上给出的定义:将对象组合成树形结构以表示“部分-整体”的层次结构.组合使得用户对单个对象和组合对象的使用具有一致性.注意两个字“树形”.这种树形结构在现实生活中随处可见,比如一个集团公司,它有一 ...

  2. 《通过C#学Proto.Actor模型》之Supervision

    Supervision,字面意思是监督,是父Actor发现子Actor有异常发生后,对子Actor产用保种策略处理的机制,如果父Actor不处理,则往上传递. 子Actor发生异常后处理的策略有: R ...

  3. C++设计模式——组合模式

    问题描述 上图,是一个公司的组织结构图,总部下面有多个子公司,同时总部也有各个部门,子公司下面有多个部门.如果对这样的公司开发一个OA系统,作为程序员的你,如何设计这个OA系统呢?先不说如何设计实现, ...

  4. docker+redis安装与配置,主从+哨兵模式

    docker+redis安装与配置 docker安装redis并且使用redis挂载的配置启动 1.拉取镜像 docker pull redis:3.2 2.准备准备挂载的目录和配置文件 首先在/do ...

  5. [C++设计模式] composite 组合模式

    组合(Composite)模式的其他翻译名称也非常多,比方合成模式.树模式等等.在<设计模式>一书中给出的定义是:将对象以树形结构组织起来,以达成"部分-总体"的层次结 ...

  6. 【原】谈谈对Objective-C中代理模式的误解

    [原]谈谈对Objective-C中代理模式的误解 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 这篇文章主要是对代理模式和委托模式进行了对比,个人认为Objective ...

  7. 彻底理解AC多模式匹配算法

    (本文尤其适合遍览网上的讲解而仍百思不得姐的同学) 一.原理 AC自动机首先将模式组记录为Trie字典树的形式,以节点表示不同状态,边上标以字母表中的字符,表示状态的转移.根节点状态记为0状态,表示起 ...

  8. 制作类似ThinkPHP框架中的PATHINFO模式功能

    一.PATHINFO功能简述 搞PHP的都知道ThinkPHP是一个免费开源的轻量级PHP框架,虽说轻量但它的功能却很强大.这也是我接触学习的第一个框架.TP框架中的URL默认模式即是PathInfo ...

  9. MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息

    MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...

随机推荐

  1. oracle 11G 导出空表失败的解决方法

    一.问题原因:     11G中有个新特性,当表无数据时,不分配segment,以节省空间     1.insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除 ...

  2. 批处理学习笔记12 - 拷贝大文件到特定目录 Copy命令

    ~z 获取文件容量大小,配合变量使用 copy 拷贝命令, copy 原地址 新地址 ok,下面上代码 @echo off setlocal enabledelayedexpansion for %% ...

  3. Dubbo 基础介绍

    1.学习背景 随着互联网的发展,越来越多的企业每天处理着上亿级的请求,以及每秒几万的并发操作,对于传统的 JavaWeb 工程师是一个巨大的挑战.然而越来越复杂的业务以及数据库设计使得代码变得非常庞大 ...

  4. 【Android】21.4 图片动画缩放示例

    分类:C#.Android.VS2015: 创建日期:2016-03-21 一.简介 该例子演示如何动画缩放图片,实现类似"点击看大图"的效果. 二.示例 1.运行截图    2. ...

  5. receiver type *** for instance message is a forward declaration

    转自:http://stackoverflow.com/questions/8815200/receiver-type-for-instance-message-is-a-forward-declar ...

  6. 在 Linux 上使用 Nginx 和 Gunicorn 托管 Django 应用

    介绍 托管 Django Web 应用程序相当简单,虽然它比标准的 PHP 应用程序更复杂一些. 让 Web 服务器对接 Django 的方法有很多. Gunicorn 就是其中最简单的一个. Gun ...

  7. 使用OleDB组件连接和访问Oracle数据库

    访问 Oracle 数据库的步骤 .在 Oracle 中,创建一个名为 TestTable 的表,如下所示: Create Table TestTable (c1 )); .将数据插入到 TestTa ...

  8. Office 2013 Excel 打开文档很慢很慢的解决方法

    这个问题查了很多案例,试了很多方法,但是只有下面这个方法有用! 这几天打开excel文档很慢很慢,双击之后好久没反应,过会儿它才慢慢冒出来,当时将就了,刚刚休息的时候想着查一下吧,不然很影响工作效率! ...

  9. tomcat logs 目录下各日志文件的含义

    tomcat每次启动时,自动在logs目录下生产以下日志文件,按照日期自动备份 localhost.2016-07-05.txt   //经常用到的文件之一 ,程序异常没有被捕获的时候抛出的地方 ca ...

  10. UserAgent判断浏览器类型或爬虫类型

    ### 浏览器------------------------------- IEMozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; T ...