Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to specify timeout for SocketHttpServer. #371

Open
LacticWhale opened this issue Sep 26, 2024 · 2 comments
Open

How to specify timeout for SocketHttpServer. #371

LacticWhale opened this issue Sep 26, 2024 · 2 comments
Labels

Comments

@LacticWhale
Copy link

Problem: I need to create api and some methods I call can take up to 45 seconds to generate a response.
Is there a way to remove or increase timeout.

I copied example with delayed response and increased wait time to 30 seconds.

#!/usr/bin/env php
<?php declare(strict_types=1);

require "vendor/autoload.php";

use Amp\ByteStream;
use Amp\Http\HttpStatus;
use Amp\Http\Server\DefaultErrorHandler;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Server\SocketHttpServer;
use Amp\Log\ConsoleFormatter;
use Amp\Log\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;
use function Amp\delay;
use function Amp\trapSignal;

// Run this script, then visit http://localhost:1338/ in your browser.

$logHandler = new StreamHandler(ByteStream\getStdout());
$logHandler->pushProcessor(new PsrLogMessageProcessor());
$logHandler->setFormatter(new ConsoleFormatter());
$logger = new Logger('server');
$logger->pushHandler($logHandler);

$server = SocketHttpServer::createForDirectAccess($logger);

$server->expose("0.0.0.0:1338");
$server->expose("[::]:1338");

$server->start(new ClosureRequestHandler(function (Request $request) use($logger): Response {
    // We delay the response here, but this could also be non-blocking I/O.
    // Further requests are still processed concurrently.
    delay(30);

    $logger->info("End of delay.");

    return new Response(HttpStatus::OK, [
        "content-type" => "text/plain; charset=utf-8",
    ], "Hello, World!");
}), new DefaultErrorHandler());

// Await a termination signal to be received.
$signal = trapSignal([\SIGHUP, \SIGINT, \SIGQUIT, \SIGTERM]);

$logger->info(sprintf("Received signal %d, stopping HTTP server", $signal));

$server->stop();

And result I get:

$ time curl -m 60 http://127.0.0.1:1338/
curl: (52) Empty reply from server

real    0m14.483s
user    0m0.009s
sys     0m0.006s
@Brissan2025
Copy link

I also encountered this problem, please advise the solution to this issue

@kelunik kelunik added the bug label Sep 29, 2024
@kelunik
Copy link
Member

kelunik commented Sep 29, 2024

It seems like the timeout is implemented improperly here. It should only apply while the client is still sending data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants