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

add sendTimeout and receiveTimeout in Http2Adapter #1942

Merged
merged 4 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion plugins/http2_adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

*None.*
- Add send timeout and receive timeout
AlexV525 marked this conversation as resolved.
Show resolved Hide resolved

## 2.3.1+1

Expand Down
30 changes: 27 additions & 3 deletions plugins/http2_adapter/lib/src/http2_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,21 @@ class Http2Adapter implements HttpClientAdapter {
}

if (hasRequestData) {
await requestStream!.listen((data) {
final requestStreamFuture = requestStream!.listen((data) {
stream.outgoingMessages.add(DataStreamMessage(data));
}).asFuture();
final sendTimeout = options.sendTimeout;
if (sendTimeout != null) {
await requestStreamFuture.timeout(sendTimeout, onTimeout: () {
throw DioException.sendTimeout(
timeout: sendTimeout,
requestOptions: options,
);
});
sunhapper marked this conversation as resolved.
Show resolved Hide resolved
} else {
await requestStreamFuture;
}
Comment on lines +106 to +108
Copy link
Member

Choose a reason for hiding this comment

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

TBH I'd prefer how the IO adapter wrote, future = future.timeout, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer final variable rather than reassigning it

}

await stream.outgoingMessages.close();

final sc = StreamController<Uint8List>();
Expand Down Expand Up @@ -145,7 +155,21 @@ class Http2Adapter implements HttpClientAdapter {
cancelOnError: true,
);

await completer.future;
final receiveTimeout = options.receiveTimeout;
if (receiveTimeout != null) {
await completer.future.timeout(
receiveTimeout,
onTimeout: () {
subscription.cancel().whenComplete(() => sc.close());
throw DioException.receiveTimeout(
timeout: receiveTimeout,
requestOptions: options,
);
},
);
} else {
await completer.future;
}

// Handle redirection
if (needRedirect) {
Expand Down
35 changes: 35 additions & 0 deletions plugins/http2_adapter/test/http2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,39 @@ void main() {
final res = await dio.post('post', data: 'TEST');
expect(res.data.toString(), contains('TEST'));
});

test('catch DioException when receiveTimeout', () {
final dio = Dio()
..options.baseUrl = 'https://httpbun.com/'
..httpClientAdapter = Http2Adapter(
ConnectionManager(
idleTimeout: Duration(milliseconds: 10),
),
);
dio.options.receiveTimeout = Duration(seconds: 5);
sunhapper marked this conversation as resolved.
Show resolved Hide resolved

expectLater(
dio.get('/drip?delay=10&numbytes=1'),
allOf([
throwsA(isA<DioException>()),
throwsA(predicate(
(DioException e) => e.type == DioExceptionType.receiveTimeout)),
throwsA(predicate(
(DioException e) => e.message!.contains('0:00:05.000000'))),
]),
);
}, testOn: 'vm');
AlexV525 marked this conversation as resolved.
Show resolved Hide resolved

test('no DioException when receiveTimeout > request duration', () async {
final dio = Dio()
..options.baseUrl = 'https://httpbun.com/'
..httpClientAdapter = Http2Adapter(
ConnectionManager(
idleTimeout: Duration(milliseconds: 10),
),
);
dio.options.receiveTimeout = Duration(seconds: 5);

await dio.get('/drip?delay=1&numbytes=1');
});
}
Binary file added plugins/http2_adapter/test/mock/flutter.png
sunhapper marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading