mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Remove Context#app to reduce GC load (experimental)
This commit is contained in:
		
							parent
							
								
									72bcac6dd5
								
							
						
					
					
						commit
						5917af3f14
					
				
					 5 changed files with 8 additions and 15 deletions
				
			
		|  | @ -12,11 +12,10 @@ class CustomLogHandler < Kemal::BaseLogHandler | ||||||
|   end |   end | ||||||
| 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 |   io = IO::Memory.new | ||||||
|   response = HTTP::Server::Response.new(io) |   response = HTTP::Server::Response.new(io) | ||||||
|   context = HTTP::Server::Context.new(request, response) |   context = HTTP::Server::Context.new(request, response) | ||||||
|   context.app = app |  | ||||||
|   handler.call(context) |   handler.call(context) | ||||||
|   response.close |   response.close | ||||||
|   io.rewind |   io.rewind | ||||||
|  |  | ||||||
|  | @ -22,14 +22,14 @@ describe "ParamParser" do | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   it "parses url params" do |   it "parses url params" do | ||||||
|     kemal = Kemal.application.route_handler |     route_handler = Kemal.application.route_handler | ||||||
|     kemal.add_route "POST", "/hello/:hasan" do |env| |     route_handler.add_route "POST", "/hello/:hasan" do |env| | ||||||
|       "hello #{env.params.url["hasan"]}" |       "hello #{env.params.url["hasan"]}" | ||||||
|     end |     end | ||||||
|     request = HTTP::Request.new("POST", "/hello/cemal") |     request = HTTP::Request.new("POST", "/hello/cemal") | ||||||
|     # Radix tree MUST be run to parse url params. |     # Radix tree MUST be run to parse url params. | ||||||
|     context = create_request_and_return_io_and_context(kemal, request)[1] |     io_with_context = create_request_and_return_io(route_handler, request) | ||||||
|     url_params = Kemal::ParamParser.new(request, context.route_lookup.params).url |     url_params = Kemal::ParamParser.new(request).url | ||||||
|     url_params["hasan"].should eq "cemal" |     url_params["hasan"].should eq "cemal" | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,10 +1,9 @@ | ||||||
| require "./spec_helper" | 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 |   io = IO::Memory.new | ||||||
|   response = HTTP::Server::Response.new(io) |   response = HTTP::Server::Response.new(io) | ||||||
|   context = HTTP::Server::Context.new(request, response) |   context = HTTP::Server::Context.new(request, response) | ||||||
|   context.app = app |  | ||||||
|   begin |   begin | ||||||
|     handler.call context |     handler.call context | ||||||
|   rescue IO::Error |   rescue IO::Error | ||||||
|  | @ -36,7 +35,6 @@ describe "Kemal::WebSocketHandler" do | ||||||
| 
 | 
 | ||||||
|   it "matches on given route" do |   it "matches on given route" do | ||||||
|     app = Kemal::Base.new |     app = Kemal::Base.new | ||||||
|     handler =  app.websocket_handler |  | ||||||
|     app.ws "/" { |socket, context| socket.send("Match") } |     app.ws "/" { |socket, context| socket.send("Match") } | ||||||
|     app.ws "/no_match" { |socket, context| socket.send "No Match" } |     app.ws "/no_match" { |socket, context| socket.send "No Match" } | ||||||
|     headers = HTTP::Headers{ |     headers = HTTP::Headers{ | ||||||
|  | @ -47,13 +45,12 @@ describe "Kemal::WebSocketHandler" do | ||||||
|     } |     } | ||||||
|     request = HTTP::Request.new("GET", "/", headers) |     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") |     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 |   end | ||||||
| 
 | 
 | ||||||
|   it "fetches named url parameters" do |   it "fetches named url parameters" do | ||||||
|     app = Kemal::Base.new |     app = Kemal::Base.new | ||||||
|     handler =  app.websocket_handler |  | ||||||
|     app.ws "/:id" { |s, c| c.params.url["id"] } |     app.ws "/:id" { |s, c| c.params.url["id"] } | ||||||
|     headers = HTTP::Headers{ |     headers = HTTP::Headers{ | ||||||
|       "Upgrade"               => "websocket", |       "Upgrade"               => "websocket", | ||||||
|  | @ -62,7 +59,7 @@ describe "Kemal::WebSocketHandler" do | ||||||
|       "Sec-WebSocket-Version" => "13", |       "Sec-WebSocket-Version" => "13", | ||||||
|     } |     } | ||||||
|     request = HTTP::Request.new("GET", "/1234", headers) |     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") |     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 |   end | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -8,8 +8,6 @@ class HTTP::Server | ||||||
|     # :nodoc: |     # :nodoc: | ||||||
|     STORE_MAPPINGS = [Nil, String, Int32, Int64, Float64, Bool] |     STORE_MAPPINGS = [Nil, String, Int32, Int64, Float64, Bool] | ||||||
| 
 | 
 | ||||||
|     property! app : Kemal::Base |  | ||||||
| 
 |  | ||||||
|     macro finished |     macro finished | ||||||
|       alias StoreTypes = Union({{ *STORE_MAPPINGS }}) |       alias StoreTypes = Union({{ *STORE_MAPPINGS }}) | ||||||
|       @store = {} of String => StoreTypes |       @store = {} of String => StoreTypes | ||||||
|  |  | ||||||
|  | @ -12,7 +12,6 @@ module Kemal | ||||||
|     def call(context : HTTP::Server::Context) |     def call(context : HTTP::Server::Context) | ||||||
|       context.response.headers.add "X-Powered-By", "Kemal" if Kemal.config.powered_by_header |       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.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type") | ||||||
|       context.app = app |  | ||||||
|       call_next context |       call_next context | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue