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: ConsumerSupervisor — gen_stage v1.3.2

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

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
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=āœ“&query=perfume&page=2, I would like to get: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ā€˜elixir’ comma...
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
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
jason.o
In the code below, if the create action is not set to accept ā€œextra_keyā€ as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement