Skip to content

Commit

Permalink
Merge pull request #5 from Innmind/reduce-circular-references
Browse files Browse the repository at this point in the history
Reduce circular references
  • Loading branch information
Baptouuuu authored Oct 26, 2024
2 parents 0eff741 + ccbb50a commit b3b8ab5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
19 changes: 14 additions & 5 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
Expand All @@ -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()
Expand Down

0 comments on commit b3b8ab5

Please sign in to comment.