Skip to content

Instantly share code, notes, and snippets.

@xiongxin
Last active September 19, 2018 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiongxin/6d18b277c47a9dd47d32f9c900227c09 to your computer and use it in GitHub Desktop.
Save xiongxin/6d18b277c47a9dd47d32f9c900227c09 to your computer and use it in GitHub Desktop.
Elixir Supervisor 三种不同的strategy
defmodule KVServer.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    # List all child processes to be supervised
    # port = String.to_integer(System.get_env("PORT") || raise "missing $PORT environment variable")

    children = [
      # Starts a worker by calling: KVServer.Worker.start_link(arg)
      # {KVServer.Worker, arg},
      {Task.Supervisor, name: KVServer.TaskSupervisor},
      {Task, fn -> KVServer.accept(4040) end}
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: KVServer.Supervisor]
    Supervisor.start_link(children, opts)
  end
end
  1. :one_for_one,通过上面的代码,我们可以看出该应用启动了两个进程,当其中一个crash时,只会重启自身的进程
  2. :one_for_all,其中一个子进程crash时,重启所有子进程
  3. :rest_for_all, 按顺序重启当前进程之后的所有进程
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment