How fast can I return http status 200 response from a plug

Hi, to begin with, I am new to elixir/phoenix (:slight_smile: disclaimer)

For what has already been discussed in this thread:
Benchmark report the-benchmarker and phoenix

I trying to create a response 200, ok, from a custom plug, without others steps in the pipeline.
I have endpoint (default phoenix) → a router (here I declare a new pipeline with only two plugs, html and my custom plug) . . .
but I cant halt the request and return the 200ok status, without return any html.
This is probably very easy, or maybe I need to use a controller, but the idea is to stay in the surface layer of the web server and return the status without touching anything else (template, ecto, etc…)
Any suggestions are welcome!

Some code:

# router.ex
pipeline :browser_for_benchmark do
    plug :accepts, ["html"]
    plug NothingPlug

  scope "/", RumblWeb do
    pipe_through :browser_for_benchmark
    get "/", PageController, :index

And the code plug:

# nothing_plug.ex
defmodule NothingPlug do
  def init(opts), do: opts

  def call(conn, _opts) do
    |> Plug.Conn.put_status(200)
    # this dont work, dont return any response
    |> Plug.Conn.halt()

With halt, we have this log error message:

> 20:03:01.200 request_id=FmOyhkAVtCWcFnwAAAAC [info] GET /
> 20:03:01.212 [error] #PID<0.484.0> running RumblWeb.Endpoint (connection #PID<0.483.0>, stream id 1) terminated
> Server: (http)
> Request: GET /
> ** (exit) an exception was raised:
>     ** (Plug.Conn.NotSentError) a response was neither set nor sent from the connection
>         (phoenix 1.5.7) lib/phoenix/endpoint/cowboy2_handler.ex:110: Phoenix.Endpoint.Cowboy2Handler.maybe_send/2
>         (phoenix 1.5.7) lib/phoenix/endpoint/cowboy2_handler.ex:66: Phoenix.Endpoint.Cowboy2Handler.init/4
>         (cowboy 2.8.0) /home/juanjo/repos/elixir/phoenix/rumbl/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
>         (cowboy 2.8.0) /home/juanjo/repos/elixir/phoenix/rumbl/deps/cowboy/src/cowboy_stream_h.erl:300: :cowboy_stream_h.execute/3
>         (cowboy 2.8.0) /home/juanjo/repos/elixir/phoenix/rumbl/deps/cowboy/src/cowboy_stream_h.erl:291: :cowboy_stream_h.request_process/3
>         (stdlib 3.14) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

Thanks, it is nice to see a sane community.

1 Like

You need to set the response to anything, even empty response. So the simplest plug that achieve what you want would be:

|> Plug.Conn.send_resp(200, [])
|> Plug.Conn.halt()

Thanks @hauleth, now it works.

Just for commenting, before (without the plug, only default phoenix index) we have 6369 request/s, now, with this plug, 9098 rps on my dev pc, lenovo i5, 4 cores, wrk (-d 20 -c 20), phoenix same machine and MIX_ENV=prod.