Skip to content

Commit 2b00f4d

Browse files
committed
Merge pull request #144 from 10gen/INT-648_fetch-connection-backoff
🐛 INT-648 fetch connection details retries w/ exponential backoff
2 parents 3743423 + d134ace commit 2b00f4d

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"ampersand-sync-localforage": "^0.1.1",
8989
"ampersand-view": "^8.0.0",
9090
"ampersand-view-switcher": "^2.0.0",
91+
"backoff": "^2.4.1",
9192
"bootstrap": "https://github.com/twbs/bootstrap/archive/v3.3.5.tar.gz",
9293
"browserify": "^10.2.4",
9394
"bugsnag-js": "^2.4.8",
@@ -119,8 +120,8 @@
119120
"mocha": "^2.2.5",
120121
"moment": "^2.10.3",
121122
"mongodb-extended-json": "^1.3.1",
122-
"mongodb-js-precommit": "^0.2.2",
123123
"mongodb-js-fmt": "^0.0.3",
124+
"mongodb-js-precommit": "^0.2.2",
124125
"mongodb-language-model": "^0.2.1",
125126
"mongodb-schema": "^3.3.0",
126127
"mousetrap": "^1.5.3",

src/app.js

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var pkg = require('../package.json');
44
var app = require('ampersand-app');
5+
var backoff = require('backoff');
6+
57
app.extend({
68
// @todo (imlucas) Move to config
79
// `scout-server` to point at.
@@ -30,6 +32,31 @@ var Statusbar = require('./statusbar');
3032

3133
var debug = require('debug')('scout:app');
3234

35+
function getConnection(model, done) {
36+
function _fetch(fn) {
37+
model.fetch({
38+
success: function() {
39+
debug('_fetch connection succeeded!');
40+
fn();
41+
},
42+
error: function() {
43+
debug('_fetch connection failed', arguments);
44+
fn(new Error('Error retrieving connection details'));
45+
}
46+
});
47+
}
48+
49+
var call = backoff.call(_fetch, done);
50+
call.setStrategy(new backoff.ExponentialStrategy({
51+
randomisationFactor: 0,
52+
initialDelay: 10,
53+
maxDelay: 500
54+
}));
55+
call.failAfter(10);
56+
call.start();
57+
}
58+
59+
3360
// Inter-process communication with main process (Electron window)
3461
var ipc = window.require('ipc');
3562

@@ -253,25 +280,21 @@ app.extend({
253280
});
254281

255282
debug('looking up connection `%s`...', connection_id);
256-
state.connection.fetch({
257-
success: function() {
258-
app.statusbar.show('Connection details loaded! Initializing client...');
259-
260-
var endpoint = app.endpoint;
261-
var connection = state.connection.serialize();
262-
263-
app.client = getOrCreateClient(endpoint, connection)
264-
.on('readable', state.onClientReady.bind(state))
265-
.on('error', state.onFatalError.bind(state, 'create client'));
266-
267-
state.startClientStalledTimer();
268-
},
269-
error: function() {
270-
// @todo (imlucas) `ampersand-sync-localforage` currently drops
271-
// the real error so for now just use a generic.
272-
state.onFatalError(state, 'fetch connection',
273-
new Error('Error retrieving connection. Please reload the page.'));
283+
getConnection(state.connection, function(err) {
284+
if (err) {
285+
state.onFatalError('fetch connection', err);
286+
return;
274287
}
288+
app.statusbar.show('Connecting to MongoDB...');
289+
290+
var endpoint = app.endpoint;
291+
var connection = state.connection.serialize();
292+
293+
app.client = getOrCreateClient(endpoint, connection)
294+
.on('readable', state.onClientReady.bind(state))
295+
.on('error', state.onFatalError.bind(state, 'create client'));
296+
297+
state.startClientStalledTimer();
275298
});
276299
});
277300
// set up ipc

0 commit comments

Comments
 (0)