Skip to content

Commit

Permalink
send 500 to client (#5644)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFreeman authored Dec 31, 2024
1 parent 2163783 commit 4cb03b4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
12 changes: 11 additions & 1 deletion ext-src/swoole_http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,17 @@ bool swoole_http_server_onBeforeRequest(HttpContext *ctx) {
void swoole_http_server_onAfterResponse(HttpContext *ctx) {
ctx->onAfterResponse = nullptr;
Server *serv = (Server *) ctx->private_data;
if (!sw_server() || !sw_worker() || sw_worker()->is_shutdown()) {
if (sw_unlikely(!sw_server() || !sw_worker())) {
return;
}

if (sw_unlikely(sw_worker()->is_shutdown())) {
while (!queued_http_contexts.empty()) {
HttpContext *ctx = queued_http_contexts.front();
queued_http_contexts.pop();
ctx->send(ctx, SW_STRL(SW_HTTP_SERVICE_UNAVAILABLE_PACKET));
ctx->close(ctx);
}
return;
}

Expand Down
78 changes: 78 additions & 0 deletions tests/swoole_http_server/send_500_to_client.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
--TEST--
swoole_http_server: When the process restarts, send a 500 status code to the clients waiting in the queue
--SKIPIF--
<?php
require __DIR__ . '/../include/skipif.inc';
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Coroutine\WaitGroup;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;
use Swoole\Coroutine\Http\Client;

$pm = new ProcessManager;
$pm->parentFunc = function () use ($pm) {
run(function() use ($pm) {
$waitGroup = new WaitGroup();
go(function() use ($waitGroup, $pm) {
$waitGroup->add();
$client = new Client('127.0.0.1', $pm->getFreePort());
$client->set(['timeout' => 15]);
$client->get('/');
Assert::true($client->statusCode == 200);
$waitGroup->done();
});

sleep(1);

for ($i = 0; $i < 10; $i++) {
go(function() use ($waitGroup, $pm) {
$waitGroup->add();
$client = new Client('127.0.0.1', $pm->getFreePort());
$client->set(['timeout' => 15]);
$client->get('/');
Assert::true($client->statusCode == 503);
$waitGroup->done();
});
}

$waitGroup->wait();
$pm->kill();
});
echo 'DONE';
};

$pm->childFunc = function () use ($pm) {
$http = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_PROCESS);
$http->set([
'worker_num' => 1,
'log_file' => '/dev/null',
'enable_coroutine' => true,
'worker_max_concurrency' => 1,
'max_wait_time' => 10,
'reload_async' => true,
'hook_flags' => SWOOLE_HOOK_ALL
]);
$http->on('workerStart', function () use ($pm) {
$pm->wakeup();
});

$http->on('request', function (Request $request, Response $response) use ($http) {
$http->reload();
sleep(3);
$response->end();
});

$http->start();
};
$pm->childFirst();
$pm->run();
?>
--EXPECT--
DONE

0 comments on commit 4cb03b4

Please sign in to comment.