Bug 1243856 - Remove alarms from the Push H2 backend. r=dragana

MozReview-Commit-ID: LZJY0DtLE4E

--HG--
extra : rebase_source : 336c99819da6437a6a353946809f41b623bdc611
extra : source : a3a8b32866013bc64bf31c62a1fe9cf0a109cf33
This commit is contained in:
Kit Cambridge 2016-02-03 18:16:10 -08:00
parent f82589cbd1
commit 705744ff4b
2 changed files with 11 additions and 90 deletions

View File

@ -24,9 +24,6 @@ const {PushCrypto} = Cu.import("resource://gre/modules/PushCrypto.jsm");
// Currently supported protocols: WebSocket.
const CONNECTION_PROTOCOLS = [PushServiceWebSocket, PushServiceHttp2];
XPCOMUtils.defineLazyModuleGetter(this, "AlarmService",
"resource://gre/modules/AlarmService.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "gContentSecurityManager",
"@mozilla.org/contentsecuritymanager;1",
"nsIContentSecurityManager");
@ -96,7 +93,6 @@ this.PushService = {
_state: PUSH_SERVICE_UNINIT,
_db: null,
_options: null,
_alarmID: null,
_visibleNotifications: new Map(),
// Callback that is called after attempting to
@ -548,13 +544,11 @@ this.PushService = {
return;
}
this.stopAlarm();
this._stopObservers();
this._service.disconnect();
this._service.uninit();
this._service = null;
this.stopAlarm();
if (!this._db) {
return Promise.resolve();
@ -609,57 +603,6 @@ this.PushService = {
});
},
/** |delay| should be in milliseconds. */
setAlarm: function(delay) {
if (this._state <= PUSH_SERVICE_ACTIVATING) {
return;
}
// Bug 909270: Since calls to AlarmService.add() are async, calls must be
// 'queued' to ensure only one alarm is ever active.
if (this._settingAlarm) {
// onSuccess will handle the set. Overwriting the variable enforces the
// last-writer-wins semantics.
this._queuedAlarmDelay = delay;
this._waitingForAlarmSet = true;
return;
}
// Stop any existing alarm.
this.stopAlarm();
this._settingAlarm = true;
AlarmService.add(
{
date: new Date(Date.now() + delay),
ignoreTimezone: true
},
() => {
if (this._state > PUSH_SERVICE_ACTIVATING) {
this._service.onAlarmFired();
}
}, (alarmID) => {
this._alarmID = alarmID;
console.debug("setAlarm: Set alarm", delay, "in the future",
this._alarmID);
this._settingAlarm = false;
if (this._waitingForAlarmSet) {
this._waitingForAlarmSet = false;
this.setAlarm(this._queuedAlarmDelay);
}
}
);
},
stopAlarm: function() {
if (this._alarmID !== null) {
console.debug("stopAlarm: Stopped existing alarm", this._alarmID);
AlarmService.remove(this._alarmID);
this._alarmID = null;
}
},
/**
* Drops all active registrations and notifies the associated service
* workers. This function is called when the user switches Push servers,

View File

@ -465,7 +465,7 @@ this.PushServiceHttp2 = {
listener: null,
countUnableToConnect: 0,
lastStartListening: 0,
waitingForAlarm: false
retryTimerID: 0,
};
this._listenForMsgs(result.subscriptionUri);
return result;
@ -583,28 +583,20 @@ this.PushServiceHttp2 = {
if (retryAfter !== -1) {
// This is a 5xx response.
// To respect RetryAfter header, setTimeout is used. setAlarm sets a
// cumulative alarm so it will not always respect RetryAfter header.
this._conns[aSubscriptionUri].countUnableToConnect++;
setTimeout(_ => this._listenForMsgs(aSubscriptionUri), retryAfter);
this._conns[aSubscriptionUri].retryTimerID =
setTimeout(_ => this._listenForMsgs(aSubscriptionUri), retryAfter);
return;
}
// we set just one alarm because most probably all connection will go over
// a single TCP connection.
retryAfter = prefs.get("http2.retryInterval") *
Math.pow(2, this._conns[aSubscriptionUri].countUnableToConnect);
retryAfter = retryAfter * (0.8 + Math.random() * 0.4); // add +/-20%.
this._conns[aSubscriptionUri].countUnableToConnect++;
if (retryAfter === 0) {
setTimeout(_ => this._listenForMsgs(aSubscriptionUri), 0);
} else {
this._conns[aSubscriptionUri].waitingForAlarm = true;
this._mainPushService.setAlarm(retryAfter);
}
this._conns[aSubscriptionUri].retryTimerID =
setTimeout(_ => this._listenForMsgs(aSubscriptionUri), retryAfter);
console.debug("retryAfterBackoff: Retry in", retryAfter);
},
@ -626,7 +618,11 @@ this.PushServiceHttp2 = {
}
this._conns[subscriptionUri].listener = null;
this._conns[subscriptionUri].channel = null;
this._conns[subscriptionUri].waitingForAlarm = false;
if (this._conns[subscriptionUri].retryTimerID > 0) {
clearTimeout(this._conns[subscriptionUri].retryTimerID);
}
if (deleteInfo) {
delete this._conns[subscriptionUri];
}
@ -655,27 +651,13 @@ this.PushServiceHttp2 = {
this._conns[record.subscriptionUri] = {channel: null,
listener: null,
countUnableToConnect: 0,
waitingForAlarm: false};
retryTimerID: 0};
}
if (!this._conns[record.subscriptionUri].conn) {
this._conns[record.subscriptionUri].waitingForAlarm = false;
this._listenForMsgs(record.subscriptionUri);
}
},
// Start listening if subscriptions present.
_startConnectionsWaitingForAlarm: function() {
console.debug("startConnectionsWaitingForAlarm()");
for (let subscriptionUri in this._conns) {
if ((this._conns[subscriptionUri]) &&
!this._conns[subscriptionUri].conn &&
this._conns[subscriptionUri].waitingForAlarm) {
this._conns[subscriptionUri].waitingForAlarm = false;
this._listenForMsgs(subscriptionUri);
}
}
},
// Close connection and notify apps that subscription are gone.
_shutdownSubscription: function(aSubscriptionUri) {
console.debug("shutdownSubscriptions()");
@ -785,10 +767,6 @@ this.PushServiceHttp2 = {
err);
});
},
onAlarmFired: function() {
this._startConnectionsWaitingForAlarm();
},
};
function PushRecordHttp2(record) {