WyriHaximus / react-guzzle-http-client

Meta package for ReactPHP HttpClient adapters/handles for Guzzle 4, 5, and 6.
MIT License
10 stars 8 forks source link

Timeout does not work #11

Open DangerLifter opened 6 years ago

DangerLifter commented 6 years ago

it seems timeout does not work for me, or I use them wrong way. Here it is test code with request to slow page (20 seconds sleep before response). Run tests without usage handler returns timeout which is expected and good:

$ php ./testTimeout.php
Request at: 11:21:27
BEFORE LOOP 
AFTER LOOP 
Error at: 11:21:28

Run tests with handler hangs until response from server:


$ php ./testTimeout.php --use-ring
Request at: 11:20:41
BEFORE LOOP 
Done at: 11:21:01
AFTER LOOP 
require __DIR__ . '/bootstrap/autoload.php';

$loop = \React\EventLoop\Factory::create();
$options = [
    'defaults' => [
        'timeout' => 1, //sec,
        'connect_timeout' => 1, //sec,
    ]
];

if (in_array('--use-ring', $argv)) {
    $innerOptions = [
        'timeout' => 1,
        'connect_timeout' => 1, //sec,
    ];
    $handler = new \WyriHaximus\React\RingPHP\HttpClientAdapter(
        $loop,
        new \React\HttpClient\Client(
            $loop, new \React\Socket\Connector($loop, $innerOptions)
        )
    );
    $options['handler'] = $handler;
}

$httpClient = new \GuzzleHttp\Client($options);
$url = 'http://dev.localhost/crawler7/test.php';
echo "Request at: ".date('H:i:s')."\n";
$httpClient
    ->get($url, ['future' => true])
    ->then(
        function ($v) {echo "Done at: ".date('H:i:s')."\n";},
        function ($v) {echo "Error at: ".date('H:i:s')."\n";}
    )->done();

echo "BEFORE LOOP \n";
$loop->run();
echo "AFTER LOOP \n";

$ composer info | grep "guzzle\|react"
clue/buzz-react                        v2.2.0             Simple, async PSR-7 HTTP client for concurrently processing any number of HTTP requests, built on top of ReactPHP
clue/http-proxy-react                  v1.2.0             Async HTTP CONNECT proxy connector, use any TCP/IP protocol through an HTTP proxy server, built on top of ReactPHP
clue/socks-react                       v0.8.6             Async SOCKS4, SOCKS4a and SOCKS5 proxy client and server implementation, built on top of ReactPHP
guzzlehttp/guzzle                      5.3.1              Guzzle is a PHP HTTP client library and framework for building RESTful web service clients
guzzlehttp/psr7                        1.3.0             
guzzlehttp/ringphp                     1.1.0              Provides a simple API and specification that abstracts away the details of HTTP into a single PHP function.
guzzlehttp/streams                     3.0.0              Provides a simple abstraction over streams of data
react/cache                            v0.4.1             Async caching.
react/dns                              v0.4.11            Async DNS resolver for ReactPHP
react/event-loop                       dev-master f6dbe84 Event loop abstraction layer that libraries can use for evented I/O.
react/mysql                            dev-master 71320e0 mysql driver for reactphp.
react/promise                          v2.5.1             A lightweight implementation of CommonJS Promises/A for PHP
react/promise-stream                   v1.1.0             The missing link between Promise-land and Stream-land for ReactPHP
react/promise-timer                    v1.2.0             Trivial timeout implementation for Promises
react/socket                           v0.8.6             Async, streaming plaintext TCP/IP and secure TLS socket server and client connections for ReactPHP
react/stream                           v0.7.5             Event-driven readable and writable streams for non-blocking I/O in ReactPHP
wyrihaximus/react-guzzle-http-client   4.0.1              Asynchronous adapter for different Guzzle versions powered by react/http-client
wyrihaximus/react-guzzle-ring          dev-master a828712 Asyncronous GuzzleRing adapter powered by react/http-client
WyriHaximus commented 6 years ago

No the answer to this is very simple, timeout isn't supported where connect_timeout is. I'll try and get it in later this week :)

DangerLifter commented 6 years ago

FYI: I did in my fork https://github.com/DangerLifter/react-guzzle-http-client/commit/b041c3b821146b83c7ab108b097b24f0d2a40c81.

WyriHaximus commented 6 years ago

Does that work? And would you mind making a PR for that? :)

DangerLifter commented 6 years ago

it seemed works. Will do PR but later, Still very busy at my work.

WyriHaximus commented 6 years ago

Cool, looking forward to that PR :+1: