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

Proposal: Remove send and receive timeouts #1387

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 11 additions & 60 deletions dio/lib/src/adapters/browser_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
options.headers.remove(Headers.contentLengthHeader);
options.headers.forEach((key, v) => xhr.setRequestHeader(key, '$v'));

final connectTimeout = options.connectTimeout;
final receiveTimeout = options.receiveTimeout;
if (connectTimeout != null &&
receiveTimeout != null &&
receiveTimeout > Duration.zero) {
xhr.timeout = (connectTimeout + receiveTimeout).inMilliseconds;
}
xhr.timeout = options.connectionTimeout?.inMilliseconds;

var completer = Completer<ResponseBody>();

Expand All @@ -70,15 +64,15 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {

bool haveSent = false;

final connectionTimeout = options.connectTimeout;
final connectionTimeout = options.connectionTimeout;
if (connectionTimeout != null) {
Future.delayed(connectionTimeout).then(
(value) {
if (!haveSent) {
completer.completeError(
DioError(
requestOptions: options,
error: 'Connecting timed out [${options.connectTimeout}ms]',
error: 'Connecting timed out [${options.connectionTimeout}ms]',
type: DioErrorType.connectTimeout,
),
StackTrace.current,
Expand All @@ -89,62 +83,19 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
);
}

final uploadStopwatch = Stopwatch();
xhr.upload.onProgress.listen((event) {
haveSent = true;
final sendTimeout = options.sendTimeout;
if (sendTimeout != null) {
if (!uploadStopwatch.isRunning) {
uploadStopwatch.start();
}

var duration = uploadStopwatch.elapsed;
if (duration > sendTimeout) {
uploadStopwatch.stop();
completer.completeError(
DioError(
requestOptions: options,
error: 'Sending timed out [${options.sendTimeout}ms]',
type: DioErrorType.sendTimeout,
),
StackTrace.current,
);
xhr.abort();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ueman abort is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks for clarifying 👍

}
}
if (options.onSendProgress != null &&
event.loaded != null &&
event.total != null) {
options.onSendProgress!(event.loaded!, event.total!);
final loaded = event.loaded;
final total = event.total;
if (loaded != null && total != null) {
options.onSendProgress?.call(loaded, total);
}
});

final downloadStopwatch = Stopwatch();
xhr.onProgress.listen((event) {
final reveiveTimeout = options.receiveTimeout;
if (reveiveTimeout != null) {
if (!uploadStopwatch.isRunning) {
uploadStopwatch.start();
}

final duration = downloadStopwatch.elapsed;
if (duration > reveiveTimeout) {
downloadStopwatch.stop();
completer.completeError(
DioError(
requestOptions: options,
error: 'Receiving timed out [${options.receiveTimeout}ms]',
type: DioErrorType.receiveTimeout,
),
StackTrace.current,
);
xhr.abort();
}
}
if (options.onReceiveProgress != null) {
if (event.loaded != null && event.total != null) {
options.onReceiveProgress!(event.loaded!, event.total!);
}
final loaded = event.loaded;
final total = event.total;
if (loaded != null && total != null) {
options.onReceiveProgress?.call(loaded, total);
}
});

Expand Down
77 changes: 18 additions & 59 deletions dio/lib/src/adapters/io_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class DefaultHttpClientAdapter implements HttpClientAdapter {

bool _closed = false;

void _throwConnectingTimeout(RequestOptions options) {
throw DioError(
requestOptions: options,
error: 'Connecting timed out [${options.connectionTimeout}]',
type: DioErrorType.connectTimeout,
);
}

@override
Future<ResponseBody> fetch(
RequestOptions options,
Expand All @@ -30,22 +38,16 @@ class DefaultHttpClientAdapter implements HttpClientAdapter {
) async {
if (_closed) {
throw Exception(
"Can't establish connection after [HttpClientAdapter] closed!");
}
var _httpClient = _configHttpClient(cancelFuture, options.connectTimeout);
var reqFuture = _httpClient.openUrl(options.method, options.uri);

void _throwConnectingTimeout() {
throw DioError(
requestOptions: options,
error: 'Connecting timed out [${options.connectTimeout}ms]',
type: DioErrorType.connectTimeout,
"Can't establish connection after [HttpClientAdapter] closed!",
);
}
var _httpClient =
_configHttpClient(cancelFuture, options.connectionTimeout);
var reqFuture = _httpClient.openUrl(options.method, options.uri);

late HttpClientRequest request;
try {
final connectionTimeout = options.connectTimeout;
final connectionTimeout = options.connectionTimeout;
if (connectionTimeout != null) {
request = await reqFuture.timeout(connectionTimeout);
} else {
Expand All @@ -58,70 +60,27 @@ class DefaultHttpClientAdapter implements HttpClientAdapter {
});
} on SocketException catch (e) {
if (e.message.contains('timed out')) {
_throwConnectingTimeout();
_throwConnectingTimeout(options);
}
rethrow;
} on TimeoutException {
_throwConnectingTimeout();
_throwConnectingTimeout(options);
}

request.followRedirects = options.followRedirects;
request.maxRedirects = options.maxRedirects;

if (requestStream != null) {
// Transform the request data
var future = request.addStream(requestStream);
final sendTimeout = options.sendTimeout;
if (sendTimeout != null) {
future = future.timeout(sendTimeout);
}
try {
await future;
} on TimeoutException {
throw DioError(
requestOptions: options,
error: 'Sending timeout[${options.sendTimeout}ms]',
type: DioErrorType.sendTimeout,
);
}
await request.addStream(requestStream);
}

final stopwatch = Stopwatch()..start();
var future = request.close();
final receiveTimeout = options.receiveTimeout;
if (receiveTimeout != null) {
future = future.timeout(receiveTimeout);
}
late HttpClientResponse responseStream;
try {
responseStream = await future;
} on TimeoutException {
throw DioError(
requestOptions: options,
error: 'Receiving data timeout[${options.receiveTimeout}]',
type: DioErrorType.receiveTimeout,
);
}
HttpClientResponse responseStream = await request.close();

var stream =
responseStream.transform<Uint8List>(StreamTransformer.fromHandlers(
handleData: (data, sink) {
stopwatch.stop();
final duration = stopwatch.elapsed;
final receiveTimeout = options.receiveTimeout;
if (receiveTimeout != null && duration > receiveTimeout) {
sink.addError(
DioError(
requestOptions: options,
error: 'Receiving data timeout[${options.receiveTimeout}]',
type: DioErrorType.receiveTimeout,
),
);
//todo: to verify
responseStream.detachSocket().then((socket) => socket.close());
} else {
sink.add(Uint8List.fromList(data));
}
sink.add(Uint8List.fromList(data));
},
));

Expand Down
16 changes: 0 additions & 16 deletions dio/lib/src/entry/dio_for_native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,22 +216,6 @@ class DioForNative with DioMixin implements Dio {
await _closeAndDelete();
});

final timeout = response.requestOptions.receiveTimeout;
if (timeout != null) {
future = future.timeout(timeout).catchError((Object err) async {
await subscription.cancel();
await _closeAndDelete();
if (err is TimeoutException) {
throw DioError(
requestOptions: response.requestOptions,
error: 'Receiving data timeout[$timeout]',
type: DioErrorType.receiveTimeout,
);
} else {
throw err;
}
});
}
return DioMixin.listenCancelForAsyncTask(cancelToken, future);
}

Expand Down
4 changes: 1 addition & 3 deletions dio/lib/src/interceptors/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ class LogInterceptor extends Interceptor {
_printKV('method', options.method);
_printKV('responseType', options.responseType.toString());
_printKV('followRedirects', options.followRedirects);
_printKV('connectTimeout', options.connectTimeout);
_printKV('sendTimeout', options.sendTimeout);
_printKV('receiveTimeout', options.receiveTimeout);
_printKV('connectTimeout', options.connectionTimeout);
_printKV(
'receiveDataWhenStatusError', options.receiveDataWhenStatusError);
_printKV('extra', options.extra);
Expand Down
Loading