Commit 8a3e5a6d authored by KT315's avatar KT315

Use original handler

parent 4e19d621
defmodule Dummy do
@moduledoc false
@behaviour :cowboy_stream
@body "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mollis dictum ligula, ut sagittis nisl malesuada nec. Fusce hendrerit leo augue, nec pretium dolor porta sodales. Sed consequat sed purus eu aliquet. Etiam laoreet nibh vel ex sodales, non egestas lorem tempor. Pellentesque placerat facilisis felis, nec bibendum metus finibus quis. Donec lobortis, sapien at tristique placerat, nibh libero volutpat eros, eget mollis nibh elit et enim. Vestibulum consequat ut lorem sed eleifend. Ut eu dolor ut lectus faucibus rhoncus. Nam vestibulum vitae massa vel congue. Nam ac odio lacus. Nam condimentum ante eget mollis vestibulum. Cras nisi sapien, tempor nec diam at, vulputate cursus odio. Maecenas vitae tellus efficitur arcu mollis ultrices id vitae ex. Suspendisse potenti. Duis nec vestibulum dui. Donec ultricies sit amet lorem eu feugiat. Ut pretium vitae lectus at tempor. Curabitur condimentum arcu varius nulla ultricies, id feugiat odio dictum. Vivamus sollicitudin consectetur nullam."
def init(_stream_id, req, opts) do
env = Map.get(opts, :env, %{})
{:ok, req, %{handler: DummyHandler}} = :cowboy_router.execute(req, env)
def init(req, state) do
Process.sleep(trunc(100 * (1.0 + 0.5 * (:rand.uniform() * 2.0 - 1.0))))
{[
{:response, 200, :cowboy_req.response_headers(%{"content-type" => "text/plain", "content-length" => Integer.to_string(byte_size(@body))}, req),
@body},
:stop
], :undefined}
end
def data(stream_id, is_fin, data, next) do
:cowboy_stream.data(stream_id, is_fin, data, next)
end
def info(stream_id, info, next) do
:cowboy_stream.info(stream_id, info, next)
end
def terminate(stream_id, reason, next) do
:cowboy_stream.terminate(stream_id, reason, next)
end
def early_error(stream_id, reason, partial_req, resp, opts) do
:cowboy_stream.early_error(stream_id, reason, partial_req, resp, opts)
req = :cowboy_req.reply(200, %{"content-type" => "text/plain"}, @body, req)
{:ok, req, state}
end
end
......@@ -8,7 +8,7 @@ defmodule Dummy.Application do
:cowboy_router.compile([
{:_,
[
{"/", DummyHandler, %{}}
{"/", Dummy, %{}}
]}
])
......@@ -20,7 +20,12 @@ defmodule Dummy.Application do
[
:dummy,
%{num_acceptors: 10, max_connections: 999_999, socket_opts: [port: 5000]},
%{max_keepalive: 1_000, max_received_frame_rate: {1_000_000, 1}, env: %{dispatch: dispatch}, stream_handlers: [Dummy]}
%{
max_keepalive: 1_000,
max_received_frame_rate: {1_000_000, 1},
env: %{dispatch: dispatch},
stream_handlers: [Dummy.FastStream]
}
]},
restart: :permanent,
shutdown: :infinity,
......
defmodule Dummy.FastStream do
@moduledoc false
@behaviour :cowboy_stream
def init(_stream_id, req, opts) do
env = Map.get(opts, :env, %{})
{:ok, req, %{handler: handler, handler_opts: handler_opts}} = :cowboy_router.execute(req, env)
{:ok, _req, _state} = Kernel.apply(handler, :init, [req, handler_opts])
receive do
{_, msg} ->
{[msg, :stop], :undefined}
end
end
def data(stream_id, is_fin, data, next) do
:cowboy_stream.data(stream_id, is_fin, data, next)
end
def info(stream_id, info, next) do
:cowboy_stream.info(stream_id, info, next)
end
def terminate(stream_id, reason, next) do
:cowboy_stream.terminate(stream_id, reason, next)
end
def early_error(stream_id, reason, partial_req, resp, opts) do
:cowboy_stream.early_error(stream_id, reason, partial_req, resp, opts)
end
end
......@@ -21,7 +21,7 @@ defmodule Dummy.MixProject do
defp deps do
[
{:cowboy, "~> 2.7.0"}
#{:cowboy, git: "https://github.com/ninenines/cowboy.git", branch: "active_n"}
# {:cowboy, git: "https://github.com/ninenines/cowboy.git", branch: "active_n"}
]
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment