Skip to content

Commit

Permalink
feat: add timer scale factor option
Browse files Browse the repository at this point in the history
  • Loading branch information
saikumarrs committed Mar 19, 2024
1 parent 67e267b commit 9af8ae0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/analytics-js-common/src/types/LoadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export type QueueOpts = {
maxItems?: number;
// Options for batched requests
batch?: BatchOpts;
// The scale factor applied to the default timer values
timerScaleFactor?: number;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
DEFAULT_RECLAIM_TIMEOUT_MS,
DEFAULT_RECLAIM_WAIT_MS,
DEFAULT_BATCH_FLUSH_INTERVAL_MS,
MIN_TIMER_SCALE_FACTOR,
MAX_TIMER_SCALE_FACTOR,
} from './constants';

const sortByTime = (a: QueueItem, b: QueueItem) => a.time - b.time;
Expand Down Expand Up @@ -97,12 +99,21 @@ class RetryQueue implements IQueue<QueueItemData> {
jitter: options.backoffJitter || DEFAULT_BACKOFF_JITTER,
};

// Limit the timer scale factor to the minimum value
let timerScaleFactor = Math.max(
options.timerScaleFactor ?? MIN_TIMER_SCALE_FACTOR,
MIN_TIMER_SCALE_FACTOR,
);

// Limit the timer scale factor to the maximum value
timerScaleFactor = Math.min(timerScaleFactor, MAX_TIMER_SCALE_FACTOR);

// painstakingly tuned. that's why they're not "easily" configurable
this.timeouts = {
ackTimer: DEFAULT_ACK_TIMER_MS,
reclaimTimer: DEFAULT_RECLAIM_TIMER_MS,
reclaimTimeout: DEFAULT_RECLAIM_TIMEOUT_MS,
reclaimWait: DEFAULT_RECLAIM_WAIT_MS,
ackTimer: Math.round(timerScaleFactor * DEFAULT_ACK_TIMER_MS),
reclaimTimer: Math.round(timerScaleFactor * DEFAULT_RECLAIM_TIMER_MS),
reclaimTimeout: Math.round(timerScaleFactor * DEFAULT_RECLAIM_TIMEOUT_MS),
reclaimWait: Math.round(timerScaleFactor * DEFAULT_RECLAIM_WAIT_MS),
};

this.schedule = new Schedule();
Expand Down Expand Up @@ -311,7 +322,7 @@ class RetryQueue implements IQueue<QueueItemData> {
batchQueue = batchQueue.slice(-batchQueue.length);
batchQueue.push(entry);

const batchDispatchInfo = this.getBatchDispInfo(batchQueue);
const batchDispatchInfo = this.getBatchDispatchInfo(batchQueue);
// if batch criteria is met, queue the batch events to the main queue and clear batch queue
if (batchDispatchInfo.criteriaMet || batchDispatchInfo.criteriaExceeded) {
let batchItems;
Expand Down Expand Up @@ -405,7 +416,7 @@ class RetryQueue implements IQueue<QueueItemData> {
* @param batchItems Prospective batch items
* @returns Batch dispatch info
*/
getBatchDispInfo(batchItems: QueueItem[]) {
getBatchDispatchInfo(batchItems: QueueItem[]) {
let lengthCriteriaMet = false;
let lengthCriteriaExceeded = false;
const configuredBatchMaxItems = this.batch?.maxItems as number;
Expand Down Expand Up @@ -626,7 +637,7 @@ class RetryQueue implements IQueue<QueueItemData> {
const maxAttempts = 2;
const queueEntryKeys = Object.keys(QueueStatuses);
const entry = QueueStatuses[queueEntryKeys[entryIdx] as keyof typeof QueueStatuses];

(globalThis as typeof window).setTimeout(() => {
try {
store.remove(entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const DEFAULT_ACK_TIMER_MS = 1000;
const DEFAULT_RECLAIM_TIMER_MS = 3000;
const DEFAULT_RECLAIM_TIMEOUT_MS = 10000;
const DEFAULT_RECLAIM_WAIT_MS = 500;
const MIN_TIMER_SCALE_FACTOR = 1;
const MAX_TIMER_SCALE_FACTOR = 10;

const DEFAULT_MAX_BATCH_SIZE_BYTES = 512 * 1024; // 512 KB; this is also the max size of a batch
const DEFAULT_MAX_BATCH_ITEMS = 100;
Expand All @@ -29,4 +31,6 @@ export {
DEFAULT_MAX_BATCH_SIZE_BYTES,
DEFAULT_MAX_BATCH_ITEMS,
DEFAULT_BATCH_FLUSH_INTERVAL_MS,
MIN_TIMER_SCALE_FACTOR,
MAX_TIMER_SCALE_FACTOR,
};

0 comments on commit 9af8ae0

Please sign in to comment.