diff --git a/CHANGELOG.md b/CHANGELOG.md index ca38c36..9cbd9d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Changed + +- Use `static` closures as much as possible to reduce the probability of creating circular references by capturing `$this` as it can lead to memory root buffer exhaustion. + ## 3.0.1 - 2024-08-03 ### Fixed diff --git a/src/Encode.php b/src/Encode.php index 1ced845..5fe9672 100644 --- a/src/Encode.php +++ b/src/Encode.php @@ -32,12 +32,13 @@ public function __construct(Clock $clock) */ public function __invoke(Response $response): Sequence { + $now = $this->clock->now(); $headers = $response->headers(); $headers = $headers ->get('date') ->match( static fn() => $headers, - fn() => ($headers)(Date::of($this->clock->now())), + static fn() => ($headers)(Date::of($now)), ); $firstLine = \sprintf( diff --git a/src/Server.php b/src/Server.php index f419fa9..229bd34 100644 --- a/src/Server.php +++ b/src/Server.php @@ -99,10 +99,19 @@ public function __invoke( $console = ($this->display)($console, Str::of("Pending connections...\n")); + $injectEnv = $this->injectEnv; + $handle = $this->handle; + $encode = $this->encode; + $connections = $this ->servers ->accept() - ->map(fn($connection) => Task::of(function($os) use ($connection) { + ->map(static fn($connection) => Task::of(static function($os) use ( + $connection, + $injectEnv, + $handle, + $encode, + ) { $io = $connection ->toEncoding(Str\Encoding::ascii) ->watch(); @@ -114,10 +123,10 @@ public function __invoke( ->map(DecodeCookie::of()) ->map(DecodeQuery::of()) ->map(DecodeForm::of()) - ->map($this->injectEnv) - ->map(function($request) use ($os) { + ->map($injectEnv) + ->map(static function($request) use ($os, $handle) { try { - return ($this->handle)($request, $os); + return $handle($request, $os); } catch (\Throwable $e) { return Response::of( StatusCode::internalServerError, @@ -131,7 +140,7 @@ public function __invoke( null, Content::ofString('Request doesn\'t respect HTTP protocol'), ))) - ->flatMap(fn($response) => $connection->send(($this->encode)($response))) + ->flatMap(static fn($response) => $connection->send($encode($response))) ->flatMap( static fn() => $connection ->unwrap()