RequestTimeout

Build StatusHex.pm

Plug that kills Phoenix controller processes when they run for too long.

To do that, this Plug starts an additional process alongside the one that's handling the user request and monitors it until it dies normally or it runs out of time.

It also

Setting up

In your mix.exs:

  defp deps,
    do: [
      # ...
      {:request_timeout, "~> 1.0"}
      # ...
    ]

Then add this plug as early as possible in your pipelines:

defmodule MyWeb.Router do
  use MyWeb, :router

  pipeline :api do
    plug :accepts, @accepts
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery

    # Remember: :kill exit signal is untrappable
    plug RequestTimeout, after: 500, err: :kill, sup: RequestTimeout.Sup

    # ...
  end

  # ...
end

It is recommended you supervise the monitoring processes under a one_for_all. You can use the supervisor provided with this library this way:

defmodule MyWeb.Supervisor do
  use Supervisor

  def start_link() do
    Supervisor.start_link(__MODULE__, nil, name: __MODULE__)
  end

  def init(_opts) do
    [
      supervisor(MyWeb.Endpoint, []),
      supervisor(RequestTimeout.Sup, []),
      # ...
    ]
    |> Supervisor.init(strategy: :one_for_one)
  end
end

Options

All options come with defaults listed here