Bug 1385396 - Cache early setExperimentActive calls r=gfritzsche

Calling setExperimentActive too early during startup can change
the order of some initialization. setExperimentActive probably
shouldn't have this kind of effect, so simply cache early calls
to it until gGlobalEnvironment has been initialized through other
functions.

Additionally, I am speculatively including work to ensure that
setExperimentInactive and getActiveExperiments have the same
behavior, while remaining correct by working from the same cache
that setExperimentActive uses.

MozReview-Commit-ID: IlzT1J0o6gK

--HG--
extra : rebase_source : b39b8d7e7b0970b520ce3f63af9750846d08b0eb
This commit is contained in:
Doug Thayer 2017-08-02 15:10:34 -07:00
Родитель fbf0accf5f
Коммит 60c221fa54
1 изменённых файлов: 30 добавлений и 3 удалений

Просмотреть файл

@ -52,6 +52,11 @@ var Policy = {
now: () => new Date(),
};
// This is used to buffer calls to setExperimentActive and friends, so that we
// don't prematurely initialize our environment if it is called early during
// startup.
var gActiveExperimentStartupBuffer = new Map();
var gGlobalEnvironment;
function getGlobal() {
if (!gGlobalEnvironment) {
@ -92,7 +97,11 @@ this.TelemetryEnvironment = {
* @param {String} [options.type=false] The specific experiment type.
*/
setExperimentActive(id, branch, options = {}) {
return getGlobal().setExperimentActive(id, branch, options);
if (gGlobalEnvironment) {
gGlobalEnvironment.setExperimentActive(id, branch, options);
} else {
gActiveExperimentStartupBuffer.set(id, {branch, options});
}
},
/**
@ -102,7 +111,11 @@ this.TelemetryEnvironment = {
* @param {String} id The id of the active experiment.
*/
setExperimentInactive(id) {
return getGlobal().setExperimentInactive(id);
if (gGlobalEnvironment) {
gGlobalEnvironment.setExperimentInactive(id);
} else {
gActiveExperimentStartupBuffer.delete(id);
}
},
/**
@ -116,7 +129,15 @@ this.TelemetryEnvironment = {
* }
*/
getActiveExperiments() {
return getGlobal().getActiveExperiments();
if (gGlobalEnvironment) {
return gGlobalEnvironment.getActiveExperiments();
}
const result = {};
for (const [id, {branch}] of gActiveExperimentStartupBuffer.entries()) {
result[id] = branch;
}
return result;
},
shutdown() {
@ -151,6 +172,7 @@ this.TelemetryEnvironment = {
testCleanRestart() {
getGlobal().shutdown();
gGlobalEnvironment = null;
gActiveExperimentStartupBuffer = new Map();
return getGlobal();
},
};
@ -847,6 +869,11 @@ function EnvironmentCache() {
p.push(this._updateAttribution());
}
for (const [id, {branch, options}] of gActiveExperimentStartupBuffer.entries()) {
this.setExperimentActive(id, branch, options);
}
gActiveExperimentStartupBuffer = null;
let setup = () => {
this._initTask = null;
this._startWatchingPrefs();