Skip to content

Commit 6a2ea1b

Browse files
committed
retry SSH connections, for more reliable install
1 parent 958f78b commit 6a2ea1b

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/cloud/social/provider.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import arraybuffers = require('../../arraybuffers/arraybuffers');
77
import linefeeder = require('../../net/linefeeder');
88
import logging = require('../../logging/logging');
9+
import promises = require('../../promises/promises');
910
import queue = require('../../handler/queue');
1011

1112
// https://github.com/borisyankov/DefinitelyTyped/blob/master/ssh2/ssh2-tests.ts
@@ -25,6 +26,9 @@ const STORAGE_KEY = 'cloud-social-contacts';
2526
// Timeout for establishing an SSH connection.
2627
const CONNECT_TIMEOUT_MS = 10000;
2728

29+
// Maximum number of times to try establishing an SSH connection.
30+
const MAX_CONNECT_ATTEMPTS = 3;
31+
2832
// Credentials for accessing a cloud instance.
2933
// The serialised, base64 form is distributed amongst users.
3034
interface Invite {
@@ -199,15 +203,23 @@ export class CloudSocialProvider {
199203
});
200204
}
201205

202-
var connection = new Connection(invite, (message: Object) => {
203-
this.dispatchEvent_('onMessage', {
204-
from: makeClientState(invite.host),
205-
// SIGNAL_FROM_SERVER_PEER,
206-
message: JSON.stringify(makeVersionedPeerMessage(3002, message))
206+
let numAttempts = 0;
207+
const connect = () => {
208+
log.debug('connection attempt %1...', (++numAttempts));
209+
const connection = new Connection(invite, (message: Object) => {
210+
this.dispatchEvent_('onMessage', {
211+
from: makeClientState(invite.host),
212+
// SIGNAL_FROM_SERVER_PEER,
213+
message: JSON.stringify(makeVersionedPeerMessage(3002, message))
214+
});
207215
});
208-
});
209-
210-
this.clients_[invite.host] = connection.connect().then(() => {
216+
return connection.connect().then(() => {
217+
return connection;
218+
});
219+
};
220+
221+
this.clients_[invite.host] = promises.retry(connect,
222+
MAX_CONNECT_ATTEMPTS).then((connection:Connection) => {
211223
log.info('connected to zork on %1', invite.host);
212224

213225
// Fetch the banner, if available, then emit an instance message.

0 commit comments

Comments
 (0)