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); Duration delay = const Duration(milliseconds: 100);
int attempts = 0; int attempts = 0;
io.WebSocket socket; 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) { while (attempts < _kMaxAttempts && socket == null) {
attempts += 1; attempts += 1;
try { try {
socket = await io.WebSocket.connect(uri.toString()); socket = await io.WebSocket.connect(uri.toString());
} catch (e) { } on io.WebSocketException catch (e) {
printTrace('Exception attempting to connect to observatory: $e'); await onError(e);
printTrace('This was attempt #$attempts. Will retry in $delay.'); } on io.SocketException catch (e) {
await new Future<Null>.delayed(delay); await onError(e);
delay *= 2;
} }
} }