Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 1 addition & 68 deletions bin/swoole-server
Original file line number Diff line number Diff line change
Expand Up @@ -128,58 +128,7 @@ $server->on('request', function ($request, $response) use ($server, $workerState
usleep(10000); // 10ms
}

// Get a worker from the pool with a timeout (Queuing mechanism)
$worker = null;
if ($workerState->clientPool) {
$poolConfig = $serverState['octaneConfig']['swoole']['pool'] ?? [];
$waitTimeout = $poolConfig['wait_timeout'] ?? 30.0;
$rejectOnFull = $poolConfig['reject_on_full'] ?? false;
$overloadStatus = $poolConfig['overload_status'] ?? 503;
$retryAfter = $poolConfig['overload_retry_after'] ?? 5;

if (!is_numeric($waitTimeout)) {
$waitTimeout = 30.0;
}

$waitTimeout = (float) $waitTimeout;
try {
if ($workerState->workerPool) {
$worker = $workerState->workerPool->acquire((float) $waitTimeout, (bool) $rejectOnFull);
} else {
$worker = $workerState->clientPool->pop(0.001);

if ($worker === false && ! $rejectOnFull && $waitTimeout > 0) {
$worker = $workerState->clientPool->pop((float) $waitTimeout);
}
}
} catch (\Throwable $e) {
error_log("❌ Failed to acquire worker from pool: " . $e->getMessage());
$worker = null;
}

if (! $worker) {
$errCode = $workerState->clientPool->errCode;
error_log("❌ Pool pop failed! ErrCode: {$errCode}, Stats: " . json_encode($workerState->clientPool->stats()));

// Timeout occurred - pool is exhausted and queue is full/slow
$response->status((int) $overloadStatus);
$response->header('Content-Type', 'application/json');
if (is_numeric($retryAfter) && (int) $retryAfter > 0) {
$response->header('Retry-After', (string) $retryAfter);
}
$response->end(json_encode([
'error' => 'Service Unavailable',
'message' => 'Server is too busy, please try again later',
'debug_pool_stats' => $workerState->clientPool->stats(),
'debug_err_code' => $errCode,
'debug_worker_pool' => $workerState->workerPool ? $workerState->workerPool->stats() : null,
]));
return;
}
} else {
// Fallback if pool not initialized (shouldn't happen with new OnWorkerStart)
$worker = $workerState->worker;
}
$worker = $workerState->worker;

$cid = Coroutine::getCid();
$context = Coroutine::getContext();
Expand Down Expand Up @@ -244,22 +193,6 @@ $server->on('request', function ($request, $response) use ($server, $workerState

Monitor::unregisterRequestCoroutine($cid);

// Return worker to the pool so it remains available for subsequent requests.
if ($workerState->workerPool && $worker) {
try {
$kept = $workerState->workerPool->release($worker);
} catch (\Throwable $e) {
$kept = false;
error_log('⚠️ Worker pool release failed - terminating worker: '.$e->getMessage());
}

if (! $kept) {
$worker->terminate();
}
} elseif ($workerState->clientPool && $worker) {
$workerState->clientPool->push($worker, 0.001);
}

// Ensure coroutine context is cleared (Swoole does this automatically, but explicit is safe)
Context::clear();
}
Expand Down
48 changes: 48 additions & 0 deletions src/Concerns/ProvidesDefaultConfigurationOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

trait ProvidesDefaultConfigurationOptions
{
/**
* Determine if the runtime is using the Swoole coroutine application proxy.
*/
protected static function usingSwooleCoroutineMode(): bool
{
return env('OCTANE_SERVER', 'roadrunner') === 'swoole' && extension_loaded('swoole');
}

/**
* Determine if a listener should run once at worker boot in coroutine mode.
*/
protected static function isCoroutineBootOnlyListener(string $listener): bool
{
return str_starts_with($listener, 'Laravel\\Octane\\Listeners\\GiveNewApplicationInstanceTo');
}

/**
* Get the listeners that will prepare the Laravel application for a new request.
*/
Expand All @@ -25,6 +41,38 @@ public static function prepareApplicationForNextRequest(): array
* Get the listeners that will prepare the Laravel application for a new operation.
*/
public static function prepareApplicationForNextOperation(): array
{
$listeners = static::prepareApplicationForNextOperationListeners();

if (! static::usingSwooleCoroutineMode()) {
return $listeners;
}

return array_values(array_filter(
$listeners,
fn (string $listener) => ! static::isCoroutineBootOnlyListener($listener)
));
}

/**
* Get the listeners that should run once at worker boot in coroutine mode.
*/
public static function prepareApplicationForCoroutineBoot(): array
{
if (! static::usingSwooleCoroutineMode()) {
return [];
}

return array_values(array_filter(
static::prepareApplicationForNextOperationListeners(),
fn (string $listener) => static::isCoroutineBootOnlyListener($listener)
));
}

/**
* Get the full operation listener list before coroutine-mode filtering.
*/
protected static function prepareApplicationForNextOperationListeners(): array
{
return [
\Laravel\Octane\Listeners\CreateConfigurationSandbox::class,
Expand Down
10 changes: 9 additions & 1 deletion src/Listeners/GiveNewRequestInstanceToApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Laravel\Octane\Listeners;

use Laravel\Octane\Swoole\Coroutine\Context;
use Laravel\Octane\Swoole\Coroutine\CoroutineApplication;

class GiveNewRequestInstanceToApplication
{
/**
Expand All @@ -11,7 +14,12 @@ class GiveNewRequestInstanceToApplication
*/
public function handle($event): void
{
$event->app->instance('request', $event->request);
$event->sandbox->instance('request', $event->request);

if (Context::inCoroutine() || $event->sandbox instanceof CoroutineApplication) {
return;
}

$event->app->instance('request', $event->request);
}
}
Loading
Loading