svilen

svilen

Author of Concurrent Data Processing in Elixir

GenStage's ConsumerSupervisor max_restarts option doesn't work šŸ˜„

There’s something really strange going on here, and I’m sure someone will be able to point out a silly mistake somewhere in the code.

I’m using GenStage’s ConsumerSupervisor to subscribe to a Producer:

defmodule UserConsumerSupervisor do
  use ConsumerSupervisor

  @opts [
    strategy: :one_for_one,
    max_restarts: 2,
    subscribe_to: [{Producer, max_demand: 3}]
  ]

  def start_link(_) do
    ConsumerSupervisor.start_link(__MODULE__, :ok)
  end

  def init(:ok) do
    children = [
      %{
        id: UserConsumer,
        start: {UserConsumer, :start_link, []},
        restart: :transient
      }
    ]

    ConsumerSupervisor.init(children, @opts)
  end
end

As specified in the @opts, I’d like the supervisor to restart a consumer only twice, if the consumer fails for whatever reason. The consumer itself is quite simple:

defmodule UserConsumer do

  def start_link(user) do
    Task.start_link(fn ->
      :timer.sleep(3000)
      if user == 3, do: raise "Ooops, error!"
    end)
  end
end

Despite the max_restarts option, when the consumer errors, the consumer process keeps getting restarted forever…

How is this possible? Am I misreading the configurations somehow :thinking:

Link to docs: https://hexdocs.pm/gen_stage/ConsumerSupervisor.html#c:init/1

Marked As Solved

jola

jola

From your link

:max_restarts - the maximum amount of restarts allowed in a time frame. Defaults to 3 times.

note ā€œtime frameā€. Right after that one comes

:max_seconds - the time frame in which :max_restarts applies in seconds. Defaults to 5 seconds.

So the supervisor will restart the children up to 2 times (your setting) per 5 seconds. You have a 3 second wait in your start link, so the child will only fail every 3 seconds, and can’t fail twice in 5 seconds. So it will restart forever.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

max_restarts is per unit time, as specified by :max_seconds. The default of :max_seconds is 5 seconds, and since you sleep for 3 seconds, you won’t exceed 2 restarts in 5 seconds.

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement