Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 1b86cfc

Browse files
authored
fix: fixing scheduling of flush intervals (segmentio#457)
* fix: fixing scheduling of flush intervals * fix: use fake timers on flush tests
1 parent 890eb60 commit 1b86cfc

File tree

8 files changed

+17
-31
lines changed

8 files changed

+17
-31
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ You must pass at least the `writeKey`. Additional configuration options are list
113113
| `debug` | true\* | When set to false, it will not generate any logs. |
114114
| `flushAt` | 20 | How many events to accumulate before sending events to the backend. |
115115
| `flushInterval` | 30 | In seconds, how often to send events to the backend. |
116-
| `retryInterval` | 60 | In seconds, how often to to retry sending events the request failed (e.g. in case of a network failure). |
117116
| `maxBatchSize` | 1000 | How many events to send to the API at once |
118-
| `maxEventsToRetry` | 1000 | How many events to keep around for to retry sending if the initial request failed |
119117
| `trackAppLifecycleEvents` | false | Enable automatic tracking for [app lifecycle events](https://segment.com/docs/connections/spec/mobile/#lifecycle-events): application installed, opened, updated, backgrounded) |
120118
| `trackDeepLinks` | false | Enable automatic tracking for when the user opens the app via a deep link (Note: Requires additional setup on iOS, [see instructions](#ios-deep-link-tracking-setup)) |
121119
| `defaultSettings` | undefined | Settings that will be used if the request to get the settings from Segment fails |

packages/core/src/__tests__/analytics.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ describe('SegmentClient', () => {
1414
config: {
1515
writeKey: 'SEGMENT_KEY',
1616
flushAt: 10,
17-
retryInterval: 40,
1817
trackAppLifecycleEvents: true,
1918
},
2019
logger: getMockLogger(),
@@ -56,8 +55,6 @@ describe('SegmentClient', () => {
5655
await client.init();
5756

5857
const flush = jest.spyOn(client, 'flush');
59-
// An interval should be set to check each second
60-
expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 1000);
6158

6259
// Wait 10 secs for the flush interval to happen
6360
jest.advanceTimersByTime(10 * 1000);
@@ -143,7 +140,6 @@ describe('SegmentClient onUpdateStore', () => {
143140
config: {
144141
writeKey: 'SEGMENT_KEY',
145142
flushAt: 10,
146-
retryInterval: 40,
147143
trackAppLifecycleEvents: true,
148144
},
149145
logger: getMockLogger(),
@@ -182,7 +178,6 @@ describe('SegmentClient onUpdateStore', () => {
182178
config: {
183179
...clientArgs.config,
184180
flushAt,
185-
retryInterval: 1,
186181
},
187182
};
188183
const segmentClient = new SegmentClient(args);

packages/core/src/__tests__/methods/flush.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ describe('methods #flush', () => {
2525
logger: getMockLogger(),
2626
store: store,
2727
};
28+
beforeEach(() => {
29+
jest.useFakeTimers();
30+
});
2831

2932
afterEach(() => {
3033
store.reset();

packages/core/src/analytics.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ export class SegmentClient {
3939
// Storage
4040
private store: Storage;
4141

42-
// how many seconds has elapsed since the last time events were sent
43-
private secondsElapsed: number = 0;
44-
4542
// current app state
4643
private appState: AppStateStatus | 'unknown' = 'unknown';
4744

@@ -221,7 +218,7 @@ export class SegmentClient {
221218
await this.fetchSettings();
222219

223220
// flush any stored events
224-
this.flush();
221+
this.flush(false);
225222

226223
// set up the timer/subscription for knowing when to flush events
227224
this.setupInterval();
@@ -304,7 +301,10 @@ export class SegmentClient {
304301
if (this.flushInterval !== null && this.flushInterval !== undefined) {
305302
clearInterval(this.flushInterval);
306303
}
307-
this.flushInterval = setInterval(() => this.tick(), 1000) as any;
304+
305+
this.flushInterval = setTimeout(() => {
306+
this.flush();
307+
}, this.config.flushInterval! * 1000);
308308
}
309309

310310
private setupStorageSubscribers() {
@@ -445,23 +445,19 @@ export class SegmentClient {
445445
}
446446
}
447447

448-
private tick() {
449-
if (this.secondsElapsed + 1 >= this.config.flushInterval!) {
450-
this.flush();
451-
} else {
452-
this.secondsElapsed += 1;
448+
async flush(debounceInterval: boolean = true) {
449+
if (this.destroyed) {
450+
return;
451+
}
452+
453+
if (debounceInterval) {
454+
// Reset interval
455+
this.setupInterval();
453456
}
454-
}
455457

456-
async flush() {
457458
if (!this.isPendingUpload) {
458459
this.isPendingUpload = true;
459460
try {
460-
if (this.destroyed) {
461-
return;
462-
}
463-
464-
this.secondsElapsed = 0;
465461
const events = this.store.events.get();
466462

467463
if (events.length > 0) {

packages/core/src/constants.e2e.mock.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ export const defaultConfig: Config = {
1212
writeKey: '',
1313
flushAt: 20,
1414
flushInterval: 30,
15-
retryInterval: 60,
1615
maxBatchSize: 1000,
17-
maxEventsToRetry: 1000,
1816
trackDeepLinks: false,
1917
trackAppLifecycleEvents: false,
2018
autoAddSegmentDestination: true,

packages/core/src/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ export const defaultConfig: Config = {
77
writeKey: '',
88
flushAt: 20,
99
flushInterval: 30,
10-
retryInterval: 60,
1110
maxBatchSize: 1000,
12-
maxEventsToRetry: 1000,
1311
trackDeepLinks: false,
1412
trackAppLifecycleEvents: false,
1513
autoAddSegmentDestination: true,

packages/core/src/plugins/SegmentDestination.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class SegmentDestination extends DestinationPlugin {
5757
this.analytics?.getConfig().maxBatchSize ?? MAX_EVENTS_PER_BATCH
5858
);
5959

60-
let sentEvents: any[] = [];
60+
let sentEvents: SegmentEvent[] = [];
6161
let numFailedEvents = 0;
6262

6363
await Promise.all(

packages/core/src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,8 @@ export type Config = {
117117
flushAt?: number;
118118
flushInterval?: number;
119119
trackAppLifecycleEvents?: boolean;
120-
retryInterval?: number;
121120
maxBatchSize?: number;
122121
trackDeepLinks?: boolean;
123-
maxEventsToRetry?: number;
124122
defaultSettings?: SegmentAPISettings;
125123
autoAddSegmentDestination?: boolean;
126124
collectDeviceId?: boolean;

0 commit comments

Comments
 (0)