Added ability to set connectTimeout of socket (#575)

This commit is contained in:
MattyBoy 2022-12-12 02:54:23 -06:00 committed by GitHub
parent 6c0829ed8b
commit 60311a7492
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 1 deletions

View File

@ -1,5 +1,10 @@
## 3.1.1-dev
* `ChannelOptions` now exposes `connectTimeout`, which is used on the
socket connect. This is used to specify the maximum allowed time to wait
for a connection to be established. If `connectTime` is longer than the system
level timeout duration, a timeout may occur sooner than specified in
`connectTimeout`. On timeout, a `SocketException` is thrown.
* Require Dart 2.17 or greater.
* Fix issue [#51](https://github.com/grpc/grpc-dart/issues/51), add support for custom error handling.

View File

@ -334,7 +334,8 @@ class _SocketTransportConnector implements ClientTransportConnector {
@override
Future<ClientTransportConnection> connect() async {
final securityContext = _options.credentials.securityContext;
_socket = await Socket.connect(_host, _port);
_socket =
await Socket.connect(_host, _port, timeout: _options.connectTimeout);
// Don't wait for io buffers to fill up before sending requests.
if (_socket.address.type != InternetAddressType.unix) {
_socket.setOption(SocketOption.tcpNoDelay, true);

View File

@ -50,6 +50,11 @@ class ChannelOptions {
/// The maximum time a single connection will be used for new requests.
final Duration connectionTimeout;
/// The maximum allowed time to wait for a connection to be established.
/// If [connectTimeout] is longer than the system level timeout duration,
/// a timeout may occur sooner than specified in [connectTimeout].
final Duration? connectTimeout;
final BackoffStrategy backoffStrategy;
final String userAgent;
@ -58,6 +63,7 @@ class ChannelOptions {
this.idleTimeout = defaultIdleTimeout,
this.userAgent = defaultUserAgent,
this.backoffStrategy = defaultBackoffStrategy,
this.connectTimeout,
this.connectionTimeout = defaultConnectionTimeOut,
this.codecRegistry,
});

View File

@ -69,6 +69,8 @@ class FakeChannelOptions implements ChannelOptions {
@override
Duration connectionTimeout = const Duration(seconds: 10);
@override
Duration? connectTimeout;
@override
String userAgent = 'dart-grpc/1.0.0 test';
@override
BackoffStrategy backoffStrategy = testBackoff;