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

Resty: timeout support #36

Open
jaumard opened this issue Nov 23, 2018 · 1 comment
Open

Resty: timeout support #36

jaumard opened this issue Nov 23, 2018 · 1 comment

Comments

@jaumard
Copy link
Contributor

jaumard commented Nov 23, 2018

As dart http doesn't support read timeout, would be nice if resty provide this functionality instead of still having to use Future.timeout

@rhalff
Copy link
Contributor

rhalff commented Jun 18, 2019

A quick solution to at least force a timeout globally on the client is:

resty_io_client.dart:

import 'package:http/http.dart';
import 'package:http/io_client.dart';

class RestyIOClient extends IOClient {
  Duration timeout;
  RestyIOClient([inner]) : super(inner);
  Future<StreamedResponse> send(BaseRequest request) async {
    return timeout != null
        ? super.send(request).timeout(timeout)
        : super.send(request);
  }
}

Example usage:

Route('http://test.com').withClient(
      RestyIOClient()..timeout = const Duration(seconds: 2),
    );

Note that it is possible to set timeouts directly on the http client, but in my case those didn't get triggered,
as it was a 504 error which does accept the connection (not sure why idleTimeout didn't kick in either):

Route('http://test.com').withClient(
      RestyIOClient(
        HttpClient()
          ..connectionTimeout = const Duration(seconds: 2)
          ..idleTimeout = const Duration(seconds: 3)
          ..userAgent = 'Test Agent',
      )..timeout = Duration(seconds: 2),
    );

One benefit of Future.timeout is it will always behave as expected.

For reference; dio is also solves this issue using Future.timeout: https://github.com/flutterchina/dio/blob/master/package_src/lib/src/adapter.dart#L100-L134

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

No branches or pull requests

2 participants