Be specific about which exceptions are retried (#16818)

This commit is contained in:
Yegor 2018-04-20 14:45:50 -07:00 committed by GitHub
parent a90a850462
commit ee735c4f25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -49,15 +49,26 @@ Future<StreamChannel<String>> _defaultOpenChannel(Uri uri) async {
Duration delay = const Duration(milliseconds: 100);
int attempts = 0;
io.WebSocket socket;
Future<void> onError(dynamic e) async {
printTrace('Exception attempting to connect to observatory: $e');
printTrace('This was attempt #$attempts. Will retry in $delay.');
// Delay next attempt.
await new Future<Null>.delayed(delay);
// Back off exponentially.
delay *= 2;
}
while (attempts < _kMaxAttempts && socket == null) {
attempts += 1;
try {
socket = await io.WebSocket.connect(uri.toString());
} catch (e) {
printTrace('Exception attempting to connect to observatory: $e');
printTrace('This was attempt #$attempts. Will retry in $delay.');
await new Future<Null>.delayed(delay);
delay *= 2;
} on io.WebSocketException catch (e) {
await onError(e);
} on io.SocketException catch (e) {
await onError(e);
}
}