Sdk: add Timeout functionality for http package

Created on 4 Jul 2014  Â·  10Comments  Â·  Source: dart-lang/sdk

_This issue was originally filed by theburnin...@gmail.com_


What steps will clearly show the issue / need for enhancement?

  1. when making http requests you usually want to timeout the request after a reasonable amount of time (usually in the region of seconds)
  2. it would be great to have away to specify timeout as Duration when making a http request via the http/browser client/http client classes.

What version of the product are you using? On what operating system?
Dart 1.5.1, http 0.11.1+1

area-pkg type-enhancement

Most helpful comment

_This comment was originally written by @seaneagan_


Doesn't Future.timeout solve this pretty well already?

https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-async.Future#id_timeout

Example:

    http.read('http://google.com')
        .timeout(const Duration(seconds: 5))
        .then(...);

All 10 comments

_Added Pkg-Http, Triaged labels._

Probably the best way to support this would be to add a separate http_timeout_client package that exposes an HttpTimeoutClient that wraps an existing client.

_This comment was originally written by theburnin...@gmail.com_


Is there any reason why timeout couldn't be added as an optional parameter to the existing clients? I think the usage pattern would be more obvious this way than having to first create a client, then create another (HttpTimeoutClient) to wrap around it before you do a HTTP request.

The http package is based on a composition model where each layer (that is, a class implementing http.Client) is very simple and has a well-defined purpose. This model makes it very easy to mix and match functionality, but one of the costs is that the API for a layer has to be very simple in order to keep them easy to implement. It would be bad if everyone implementing their own [http.Client] had to re-implement the timeout logic from scratch.

having to first create a client, then create another (HttpTimeoutClient) to wrap around it before you do a HTTP request.

The API wouldn't be that difficult. The constructor would look something like this:

    HttpTimeoutClient([HttpClient inner])
        : _inner = inner == null ? new HttpClient() : inner;

The user only needs to construct one client if they only care about one aspect of the client.

_This comment was originally written by theburning...@gmail.com_


Hi, thanks for the explanation, that makes a lot of sense.

Issue #10411 has been merged into this issue.

_This comment was originally written by @seaneagan_


Doesn't Future.timeout solve this pretty well already?

https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-async.Future#id_timeout

Example:

    http.read('http://google.com')
        .timeout(const Duration(seconds: 5))
        .then(...);

I don't think HttpTimeoutClient can accomplish what we want.

Users would like to actually cancel the inflight request.

dart:html has abort() on HttpRequest
dart:io HttpClient* has no equivalente – the idleTimeout just manages keep-alive connections while idea AKAICT


_Added Area-Pkg label._

_Marked this as being blocked by #22265._

This issue has been moved to dart-lang/http#21.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DartBot picture DartBot  Â·  3Comments

ranquild picture ranquild  Â·  3Comments

gspencergoog picture gspencergoog  Â·  3Comments

brooth picture brooth  Â·  3Comments

matanlurey picture matanlurey  Â·  3Comments