Skip to content

Commit

Permalink
docs: update worker.md to prevent infinite loop (#455)
Browse files Browse the repository at this point in the history
* Update worker.md to prevent infinite loop

Quick fix typo `<=` rather than `>=` which prevents infinite loop and get out of the loop when MAX_REQUESTS is reached.

* Update worker.md to fix impossible evaluation

Fix impossible case where if MAX_REQUESTS is not defined the rest of the condition is not evaluated as it is an && operator as mentioned by #453 (comment)

* Update worker.md change to for loop and remove handler from the loop

Change to for loop and remove handler from the loop based on the feedback #455 (comment)
  • Loading branch information
alexandreelise authored Jan 8, 2024
1 parent 3692818 commit f19c153
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions docs/worker.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,21 @@ require __DIR__.'/vendor/autoload.php';
$myApp = new \App\Kernel();
$myApp->boot();

$nbRequests = 0;
do {
$handler = static function () use ($myApp) {
// Handler outside the loop for better performance (doing less work)
$handler = static function () use ($myApp) {
// Called when a request is received,
// superglobals, php://input and the like are reset
echo $myApp->handle($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER);
};
};
for($nbRequests = 0, $running = true; isset($_SERVER['MAX_REQUESTS']) && ($nbRequests < ((int)$_SERVER['MAX_REQUESTS'])) && $running; ++$nbRequests) {
$running = \frankenphp_handle_request($handler);

// Do something after sending the HTTP response
$myApp->terminate();

// Call the garbage collector to reduce the chances of it being triggered in the middle of a page generation
gc_collect_cycles();
} while ($running && !(isset($_SERVER['MAX_REQUESTS']) && ++$nbRequests >= $_SERVER['MAX_REQUESTS']));

}
// Cleanup
$myApp->shutdown();
```
Expand Down

0 comments on commit f19c153

Please sign in to comment.