supervisor也是Erlang/OTP里一个常用的behavior,用于构建supervisor tree实现进程监控,故障恢复。
而RabbitMQ实现了一个supervisor2,我们从源码角度分析二者的实现和区别。
先介绍一些supervisor的基本概念,假设node_manager_sup是一个supervisor,它的init函数会定义supervisor的一些参数和它的children。
参数:
1. Restart Strategy:
Strategy必须是simple_one_for_one,one_for_one, one_for_all, rest_for_one 中的一种
simple_one_for_one是指supervisor启动时并不启动children,children个数不限,但只能是同一个类型的child,共享一份代码
one_for_one是指supervisor启动时就启动所有children,一旦一个child挂了,supervisor只去重启这一个child process,不影响其他children
one_for_all是指supervisor启动时就启动所有children,一旦一个child挂了,supervisor重启所有children processes