Skip to content

Commit 1ca47f2

Browse files
committed
apply timeout on lower layer to make sure it close the raw connection, also apply timeout on conn
1 parent 8bae795 commit 1ca47f2

3 files changed

Lines changed: 26 additions & 14 deletions

File tree

lib/io.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,13 @@ class HttpHelper {
9292
}
9393

9494
if (useStandardWebSocket == true && uri.scheme != "wss") {
95-
return await WebSocket.connect(
95+
return await awaitWithTimeout(WebSocket.connect(
9696
url,
9797
protocols: protocols,
9898
headers: headers
99-
);
99+
), 60000, onSuccessAfterTimeout: (WebSocket socket){
100+
socket.close();
101+
});
100102
}
101103

102104
if (uri.scheme != "ws" && uri.scheme != "wss") {
@@ -191,6 +193,9 @@ class HttpHelper {
191193
serverSide: false
192194
);
193195
});
196+
}).timeout(new Duration(minutes: 1), onTimeout:(){
197+
_client.close(force: true);
198+
throw new WebSocketException('timeout');
194199
});
195200
}
196201

lib/src/http/client_link.dart

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class HttpClientLink extends ClientLink {
141141
// https://github.com/dart-lang/sdk/issues/31275
142142
// When it is fixed, we should go back to a regular try-catch
143143
runZoned(() async {
144+
await ()async{
144145
HttpClientRequest request = await client.postUrl(connUri);
145146
Map requestJson = {
146147
'publicKey': privateKey.publicKey.qBase64,
@@ -198,7 +199,10 @@ class HttpClientLink extends ClientLink {
198199
if (serverConfig['format'] is String) {
199200
format = serverConfig['format'];
200201
}
201-
202+
}().timeout(new Duration(minutes: 1), onTimeout:(){
203+
_client.close(force: true);
204+
throw new Exception('timeout');
205+
});
202206
await initWebsocket(false);
203207
}, onError: (e, s) {
204208
logger.warning("Client socket crashed: $e $s");
@@ -227,14 +231,8 @@ class HttpClientLink extends ClientLink {
227231
wsUrl = '$wsUrl$tokenHash';
228232
}
229233

230-
var socket = await awaitWithTimeout(
231-
HttpHelper.connectToWebSocket(wsUrl,
232-
useStandardWebSocket: useStandardWebSocket),
233-
// websocket initialization should take no more than 60 seconds
234-
60000,
235-
(WebSocket socket){socket.close();},
236-
(err){}
237-
);
234+
var socket = await HttpHelper.connectToWebSocket(wsUrl,
235+
useStandardWebSocket: useStandardWebSocket);
238236

239237
_wsConnection = new WebSocketConnection(
240238
socket,

lib/src/utils/promise_timeout.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
part of dslink.utils;
22

33
Future<T> awaitWithTimeout<T>(Future<T> future, int timeoutMs,
4-
Function onSuccessAfterTimeout, Function onErrorAfterTimeout) {
4+
{Function onTimeout = null,
5+
Function onSuccessAfterTimeout = null,
6+
Function onErrorAfterTimeout = null}) {
57
Completer<T> completer = new Completer();
68

79
Timer timer = new Timer(new Duration(milliseconds: timeoutMs), () {
810
if (!completer.isCompleted) {
11+
if (onTimeout != null) {
12+
onTimeout();
13+
}
914
completer.completeError(new Exception('Future timeout before complete'));
1015
}
1116
});
1217
future.then((T t) {
1318
if (completer.isCompleted) {
14-
onSuccessAfterTimeout(t);
19+
if (onSuccessAfterTimeout != null) {
20+
onSuccessAfterTimeout(t);
21+
}
1522
} else {
1623
timer.cancel();
1724
completer.complete(t);
1825
}
1926
}).catchError((err) {
2027
if (completer.isCompleted) {
21-
onErrorAfterTimeout(err);
28+
if (onErrorAfterTimeout != null) {
29+
onErrorAfterTimeout(err);
30+
}
2231
} else {
2332
timer.cancel();
2433
completer.completeError(err);

0 commit comments

Comments
 (0)