Skip to content

Commit

Permalink
[FIX] web: add exponential backoff strategy for lost connection
Browse files Browse the repository at this point in the history
Before this commit, the web client had a naive strategy to handle lost
connections: it tried to poll the server every 2 seconds until a rpc
succeeds.

This works quite well from the perspective of the user, but may be a problem
from the perspective of the server.  If a server is down for a longish period,
then each users active tabs will then perform a request every 2 seconds. This
means that the server will be progressively hammered by many requests, which
will clutter the logs, and make it more difficult to gracefully recover.

With this commit, we simply exponentially increase the delay each time, and add
a little jitter to give a better distribution.

closes odoo#30136
  • Loading branch information
ged-odoo committed Jan 11, 2019
1 parent 83f5dc8 commit 4a3f04b
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions addons/web/static/src/js/services/crash_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,45 @@ var map_title ={
var CrashManager = core.Class.extend({
init: function() {
this.active = true;
this.isConnected = true;
},
enable: function () {
this.active = true;
},
disable: function () {
this.active = false;
},
rpc_error: function(error) {
handleLostConnection: function () {
var self = this;
if (!this.isConnected) {
// already handled, nothing to do. This can happen when several
// rpcs are done in parallel and fail because of a lost connection.
return;
}
this.isConnected = false;
var delay = 2000;
core.bus.trigger('connection_lost');

setTimeout(function checkConnection() {
ajax.jsonRpc('/web/webclient/version_info', 'call', {}, {shadow:true}).then(function () {
core.bus.trigger('connection_restored');
self.isConnected = true;
}).fail(function () {
// exponential backoff, with some jitter
delay = (delay * 1.5) + 500*Math.random();
setTimeout(checkConnection, delay);
});
}, delay);
},
rpc_error: function(error) {
if (!this.active) {
return;
}
if (this.connection_lost) {
return;
}
if (error.code === -32098) {
core.bus.trigger('connection_lost');
this.connection_lost = true;
var timeinterval = setInterval(function() {
var options = {shadow: true};
ajax.jsonRpc('/web/webclient/version_info', 'call', {}, options).then(function () {
clearInterval(timeinterval);
core.bus.trigger('connection_restored');
self.connection_lost = false;
});
}, 2000);
this.handleLostConnection();
return;
}
var handler = core.crash_registry.get(error.data.name, true);
Expand Down

0 comments on commit 4a3f04b

Please sign in to comment.