Remove Context#app to reduce GC load (experimental)

This commit is contained in:
Johannes Müller 2017-10-18 17:53:44 +02:00 committed by sdogruyol
parent 72bcac6dd5
commit 5917af3f14
5 changed files with 8 additions and 15 deletions

View file

@ -12,11 +12,10 @@ class CustomLogHandler < Kemal::BaseLogHandler
end
end
def create_request_and_return_io(handler, request, app = Kemal.application)
def create_request_and_return_io(handler, request)
io = IO::Memory.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
context.app = app
handler.call(context)
response.close
io.rewind

View file

@ -22,14 +22,14 @@ describe "ParamParser" do
end
it "parses url params" do
kemal = Kemal.application.route_handler
kemal.add_route "POST", "/hello/:hasan" do |env|
route_handler = Kemal.application.route_handler
route_handler.add_route "POST", "/hello/:hasan" do |env|
"hello #{env.params.url["hasan"]}"
end
request = HTTP::Request.new("POST", "/hello/cemal")
# Radix tree MUST be run to parse url params.
context = create_request_and_return_io_and_context(kemal, request)[1]
url_params = Kemal::ParamParser.new(request, context.route_lookup.params).url
io_with_context = create_request_and_return_io(route_handler, request)
url_params = Kemal::ParamParser.new(request).url
url_params["hasan"].should eq "cemal"
end

View file

@ -1,10 +1,9 @@
require "./spec_helper"
private def create_ws_request_and_return_io(handler, request, app = Kemal.application)
private def create_ws_request_and_return_io(handler, request)
io = IO::Memory.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
context.app = app
begin
handler.call context
rescue IO::Error
@ -36,7 +35,6 @@ describe "Kemal::WebSocketHandler" do
it "matches on given route" do
app = Kemal::Base.new
handler = app.websocket_handler
app.ws "/" { |socket, context| socket.send("Match") }
app.ws "/no_match" { |socket, context| socket.send "No Match" }
headers = HTTP::Headers{
@ -47,13 +45,12 @@ describe "Kemal::WebSocketHandler" do
}
request = HTTP::Request.new("GET", "/", headers)
io_with_context = create_ws_request_and_return_io(handler, request, app)
io_with_context = create_ws_request_and_return_io(app.websocket_handler, request)
io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n\x81\u0005Match")
end
it "fetches named url parameters" do
app = Kemal::Base.new
handler = app.websocket_handler
app.ws "/:id" { |s, c| c.params.url["id"] }
headers = HTTP::Headers{
"Upgrade" => "websocket",
@ -62,7 +59,7 @@ describe "Kemal::WebSocketHandler" do
"Sec-WebSocket-Version" => "13",
}
request = HTTP::Request.new("GET", "/1234", headers)
io_with_context = create_ws_request_and_return_io(handler, request, app)
io_with_context = create_ws_request_and_return_io(app.websocket_handler, request)
io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n")
end

View file

@ -8,8 +8,6 @@ class HTTP::Server
# :nodoc:
STORE_MAPPINGS = [Nil, String, Int32, Int64, Float64, Bool]
property! app : Kemal::Base
macro finished
alias StoreTypes = Union({{ *STORE_MAPPINGS }})
@store = {} of String => StoreTypes

View file

@ -12,7 +12,6 @@ module Kemal
def call(context : HTTP::Server::Context)
context.response.headers.add "X-Powered-By", "Kemal" if Kemal.config.powered_by_header
context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type")
context.app = app
call_next context
end
end