From 871edea7887e8de42686834bf6601d99254b0f72 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Tue, 17 Aug 2021 20:35:10 +0200 Subject: [PATCH] Don't run requests through configuration middleware While working on another PR that introduces another configuration middleware it dawned on me that we preferably don't requests through middleware that don't do anything. This change will filter it out those middleware before they are passed into the MiddlewareRunner. While running benchmarks I gained around a 10% requests per second gain when running it against example 63. --- src/HttpServer.php | 8 ++++++++ tests/HttpServerTest.php | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/HttpServer.php b/src/HttpServer.php index d5f947d5..95ccc5ff 100644 --- a/src/HttpServer.php +++ b/src/HttpServer.php @@ -247,6 +247,14 @@ public function __construct($requestHandlerOrLoop) $middleware = \array_merge($middleware, $requestHandlers); + /** + * Filter out any configuration middleware, no need to run requests through something that isn't + * doing anything with the request. + */ + $middleware = \array_filter($middleware, function ($handler) { + return !($handler instanceof StreamingRequestMiddleware); + }); + $this->streamingServer = new StreamingServer($loop, new MiddlewareRunner($middleware)); $that = $this; diff --git a/tests/HttpServerTest.php b/tests/HttpServerTest.php index 9a0a789a..9200aa66 100644 --- a/tests/HttpServerTest.php +++ b/tests/HttpServerTest.php @@ -431,4 +431,27 @@ public function testConstructServerWithMemoryLimitDoesLimitConcurrency() $this->assertTrue(is_array($middleware)); $this->assertInstanceOf('React\Http\Middleware\LimitConcurrentRequestsMiddleware', $middleware[0]); } + + public function testConstructFiltersOutConfigurationMiddlewareBefore() + { + $http = new HttpServer(new StreamingRequestMiddleware(), function () { }); + + $ref = new \ReflectionProperty($http, 'streamingServer'); + $ref->setAccessible(true); + + $streamingServer = $ref->getValue($http); + + $ref = new \ReflectionProperty($streamingServer, 'callback'); + $ref->setAccessible(true); + + $middlewareRunner = $ref->getValue($streamingServer); + + $ref = new \ReflectionProperty($middlewareRunner, 'middleware'); + $ref->setAccessible(true); + + $middleware = $ref->getValue($middlewareRunner); + + $this->assertTrue(is_array($middleware)); + $this->assertCount(1, $middleware); + } }