@@ -35,6 +35,7 @@ mess with most of the low-level details.
3535 * [ Methods] ( #methods )
3636 * [ Promises] ( #promises )
3737 * [ Cancellation] ( #cancellation )
38+ * [ Timeouts] ( #timeouts )
3839 * [ Authentication] ( #authentication )
3940 * [ Redirects] ( #redirects )
4041 * [ Blocking] ( #blocking )
@@ -172,6 +173,42 @@ $loop->addTimer(2.0, function () use ($promise) {
172173});
173174```
174175
176+ #### Timeouts
177+
178+ This library uses a very efficient HTTP implementation, so most HTTP requests
179+ should usually be completed in mere milliseconds. However, when sending HTTP
180+ requests over an unreliable network (the internet), there are a number of things
181+ that can go wrong and may cause the request to fail after a time. As such, this
182+ library respects PHP's ` default_socket_timeout ` setting (default 60s) as a timeout
183+ for sending the outgoing HTTP request and waiting for a successful response and
184+ will otherwise cancel the pending request and reject its value with an Exception.
185+
186+ Note that this timeout value covers creating the underlying transport connection,
187+ sending the HTTP request, receiving the HTTP response headers and its full
188+ response body and following any eventual [ redirects] ( #redirects ) . See also
189+ [ redirects] ( #redirects ) below to configure the number of redirects to follow (or
190+ disable following redirects altogether) and also [ streaming] ( #streaming ) below
191+ to not take receiving large response bodies into account for this timeout.
192+
193+ You can use the [ ` timeout ` option] ( #withoptions ) to pass a custom timeout value
194+ in seconds like this:
195+
196+ ``` php
197+ $browser = $browser->withOptions(array(
198+ 'timeout' => 10.0
199+ ));
200+
201+ $browser->get($uri)->then(function (ResponseInterface $response) {
202+ // response received within 10 seconds maximum
203+ var_dump($response->getHeaders());
204+ });
205+ ```
206+
207+ Similarly, you can use a negative timeout value to not apply a timeout at all
208+ or use a ` null ` value to restore the default handling. Note that the underlying
209+ connection may still impose a different timeout value. See also
210+ [ ` Browser ` ] ( #browser ) above and [ ` withOptions() ` ] ( #withoptions ) for more details.
211+
175212#### Authentication
176213
177214This library supports [ HTTP Basic Authentication] ( https://en.wikipedia.org/wiki/Basic_access_authentication )
@@ -381,6 +418,12 @@ $body->read(); // throws BadMethodCallException
381418$body->getContents(); // throws BadMethodCallException
382419```
383420
421+ Note how [ timeouts] ( #timeouts ) apply slightly differently when using streaming.
422+ In streaming mode, the timeout value covers creating the underlying transport
423+ connection, sending the HTTP request, receiving the HTTP response headers and
424+ following any eventual [ redirects] ( #redirects ) . In particular, the timeout value
425+ does not take receiving (possibly large) response bodies into account.
426+
384427If you want to integrate the streaming response into a higher level API, then
385428working with Promise objects that resolve with Stream objects is often inconvenient.
386429Consider looking into also using [ react/promise-stream] ( https://github.com/reactphp/promise-stream ) .
@@ -451,15 +494,16 @@ can be controlled via the following API (and their defaults):
451494
452495``` php
453496$newBrowser = $browser->withOptions(array(
497+ 'timeout' => null,
454498 'followRedirects' => true,
455499 'maxRedirects' => 10,
456500 'obeySuccessCode' => true,
457501 'streaming' => false,
458502));
459503```
460504
461- See also [ redirects ] ( #redirects ) and [ streaming ] ( #streaming ) for more
462- details.
505+ See also [ timeouts ] ( #timeouts ) , [ redirects ] ( #redirects ) and
506+ [ streaming ] ( #streaming ) for more details.
463507
464508Notice that the [ ` Browser ` ] ( #browser ) is an immutable object, i.e. this
465509method actually returns a * new* [ ` Browser ` ] ( #browser ) instance with the
0 commit comments