Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Fix race condition when sending daily stats #25

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@types/chai-as-promised": "^7.1.0",
"@types/lokijs": "^1.5.2",
"@types/mocha": "^5.2.0",
"@types/node": "^11.13.0",
"@types/sinon": "^4.3.1",
"@types/uuid": "^3.4.3",
"chai": "^4.1.2",
Expand Down
19 changes: 18 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ export class StatsStore {
/** If true, it'll print every metrics request on the console. */
private verboseMode: boolean;

/** are we currently in the process of reporting stats?
* Since we can have multiple windows using the same db instance, prevent race
* conditions by only sending stats if some other process is not already trying to do so.
*/
private isReporting: boolean;

public constructor(
appName: AppName,
version: string,
Expand All @@ -121,6 +127,7 @@ export class StatsStore {
this.isDevMode = !options.logInDevMode && isDevMode;
this.getAccessToken = getAccessToken;
this.gitHubUser = null;
this.isReporting = false;

this.reportingFrequency = options.reportingFrequency || exports.DailyStatsReportIntervalInMs;

Expand Down Expand Up @@ -160,12 +167,13 @@ export class StatsStore {
}

public async reportStats(getDate: () => string) {
if (this.optOut || this.isDevMode) {
if (this.optOut || this.isDevMode || this.isReporting) {
return;
}
const stats = await this.getDailyStats(getDate);

try {
this.isReporting = true;
const response = await this.post(stats);
if (response.status !== 200) {
throw new Error(`Stats reporting failure: ${response.status})`);
Expand All @@ -178,9 +186,18 @@ export class StatsStore {
// todo (tt, 5/2018): would be good to log these errors to Haystack/Datadog
// so we have some kind of visibility into how often things are failing.
console.log(err);
} finally {
this.isReporting = false;
}
}

/* is the store currently in the process of reporting stats?
* public for test purposes only.
*/
public setIsReporting(isReporting: boolean) {
this.isReporting = isReporting;
}

/* send a ping to indicate that the user has changed their opt-in preferences.
* public for testing purposes only.
*/
Expand Down
6 changes: 6 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ describe("StatsStore", function() {
await storeInDevMode.reportStats(getDate);
sinon.assert.notCalled(postStub);
});
it("does not report stats when isReporting is false", async function() {
store.setIsReporting(true);
postStub.resolves({ status: 200 });
await store.reportStats(getDate);
sinon.assert.notCalled(postStub);
});
it("sends a single ping event instead of reporting stats if a user has opted out", async function() {
postStub.resolves({ status: 200 });
store.setOptOut(true);
Expand Down