JuanjoA
How fast can I return http status 200 response from a plug
Hi, to begin with, I am new to elixir/phoenix (
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 2 of 2 Posts
hauleth
You need to set the response to anything, even empty response. So the simplest plug that achieve what you want would be:
JuanjoA
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.