Skip to content

Commit 871edea

Browse files
committed
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.
1 parent cd36b4f commit 871edea

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/HttpServer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ public function __construct($requestHandlerOrLoop)
247247

248248
$middleware = \array_merge($middleware, $requestHandlers);
249249

250+
/**
251+
* Filter out any configuration middleware, no need to run requests through something that isn't
252+
* doing anything with the request.
253+
*/
254+
$middleware = \array_filter($middleware, function ($handler) {
255+
return !($handler instanceof StreamingRequestMiddleware);
256+
});
257+
250258
$this->streamingServer = new StreamingServer($loop, new MiddlewareRunner($middleware));
251259

252260
$that = $this;

tests/HttpServerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,4 +431,27 @@ public function testConstructServerWithMemoryLimitDoesLimitConcurrency()
431431
$this->assertTrue(is_array($middleware));
432432
$this->assertInstanceOf('React\Http\Middleware\LimitConcurrentRequestsMiddleware', $middleware[0]);
433433
}
434+
435+
public function testConstructFiltersOutConfigurationMiddlewareBefore()
436+
{
437+
$http = new HttpServer(new StreamingRequestMiddleware(), function () { });
438+
439+
$ref = new \ReflectionProperty($http, 'streamingServer');
440+
$ref->setAccessible(true);
441+
442+
$streamingServer = $ref->getValue($http);
443+
444+
$ref = new \ReflectionProperty($streamingServer, 'callback');
445+
$ref->setAccessible(true);
446+
447+
$middlewareRunner = $ref->getValue($streamingServer);
448+
449+
$ref = new \ReflectionProperty($middlewareRunner, 'middleware');
450+
$ref->setAccessible(true);
451+
452+
$middleware = $ref->getValue($middlewareRunner);
453+
454+
$this->assertTrue(is_array($middleware));
455+
$this->assertCount(1, $middleware);
456+
}
434457
}

0 commit comments

Comments
 (0)