JuanjoA

JuanjoA

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
  end

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

And the code plug:

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

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

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: 127.0.0.1:4000 (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.

First 2 of 2 Posts Switch mode

hauleth

hauleth

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

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

JuanjoA OP

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.

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement