Skip to content

Commit 018d563

Browse files
authored
Merge pull request #57 from IOT-DSA/features/timeout_ws_connection
add timeout on ws connection
2 parents 0945062 + 1ca47f2 commit 018d563

4 files changed

Lines changed: 53 additions & 7 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 & 5 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,10 +231,8 @@ class HttpClientLink extends ClientLink {
227231
wsUrl = '$wsUrl$tokenHash';
228232
}
229233

230-
var socket = await HttpHelper.connectToWebSocket(
231-
wsUrl,
232-
useStandardWebSocket: useStandardWebSocket
233-
);
234+
var socket = await HttpHelper.connectToWebSocket(wsUrl,
235+
useStandardWebSocket: useStandardWebSocket);
234236

235237
_wsConnection = new WebSocketConnection(
236238
socket,

lib/src/utils/promise_timeout.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
part of dslink.utils;
2+
3+
Future<T> awaitWithTimeout<T>(Future<T> future, int timeoutMs,
4+
{Function onTimeout = null,
5+
Function onSuccessAfterTimeout = null,
6+
Function onErrorAfterTimeout = null}) {
7+
Completer<T> completer = new Completer();
8+
9+
Timer timer = new Timer(new Duration(milliseconds: timeoutMs), () {
10+
if (!completer.isCompleted) {
11+
if (onTimeout != null) {
12+
onTimeout();
13+
}
14+
completer.completeError(new Exception('Future timeout before complete'));
15+
}
16+
});
17+
future.then((T t) {
18+
if (completer.isCompleted) {
19+
if (onSuccessAfterTimeout != null) {
20+
onSuccessAfterTimeout(t);
21+
}
22+
} else {
23+
timer.cancel();
24+
completer.complete(t);
25+
}
26+
}).catchError((err) {
27+
if (completer.isCompleted) {
28+
if (onErrorAfterTimeout != null) {
29+
onErrorAfterTimeout(err);
30+
}
31+
} else {
32+
timer.cancel();
33+
completer.completeError(err);
34+
}
35+
});
36+
37+
return completer.future;
38+
}

lib/utils.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ part "src/utils/codec.dart";
1717
part "src/utils/dslink_json.dart";
1818
part "src/utils/list.dart";
1919
part "src/utils/uri_component.dart";
20+
part "src/utils/promise_timeout.dart";
2021

2122
typedef ExecutableFunction();
2223
typedef T Producer<T>();

0 commit comments

Comments
 (0)