diff --git a/lib/event_processor/event_processor_factory.browser.spec.ts b/lib/event_processor/event_processor_factory.browser.spec.ts index cc12507c8..ee3ca6c6f 100644 --- a/lib/event_processor/event_processor_factory.browser.spec.ts +++ b/lib/event_processor/event_processor_factory.browser.spec.ts @@ -193,12 +193,22 @@ describe('createBatchEventProcessor', () => { expect(mockGetOpaqueBatchEventProcessor.mock.calls[1][0].batchSize).toBe(undefined); }); - it('uses maxRetries value of 5', () => { + it('uses maxRetries value of 5 by default', () => { const processor = createBatchEventProcessor({ }); expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true); expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].retryOptions?.maxRetries).toBe(5); }); + it('uses the provided maxRetries value', () => { + const processor1 = createBatchEventProcessor({ maxRetries: 3 }); + expect(Object.is(processor1, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true); + expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].retryOptions?.maxRetries).toBe(3); + + const processor2 = createBatchEventProcessor({ maxRetries: 10 }); + expect(Object.is(processor2, mockGetOpaqueBatchEventProcessor.mock.results[1].value)).toBe(true); + expect(mockGetOpaqueBatchEventProcessor.mock.calls[1][0].retryOptions?.maxRetries).toBe(10); + }); + it('uses the default failedEventRetryInterval', () => { const processor = createBatchEventProcessor({ }); expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true); diff --git a/lib/event_processor/event_processor_factory.browser.ts b/lib/event_processor/event_processor_factory.browser.ts index 002d5c083..1e8b251ef 100644 --- a/lib/event_processor/event_processor_factory.browser.ts +++ b/lib/event_processor/event_processor_factory.browser.ts @@ -32,6 +32,7 @@ import { DEFAULT_MAX_EVENTS_IN_STORE, EventStore } from './event_store'; export const DEFAULT_EVENT_BATCH_SIZE = 10; export const DEFAULT_EVENT_FLUSH_INTERVAL = 1_000; +export const EVENT_MAX_RETRIES_BROWSER = 5; export const createForwardingEventProcessor = ( eventDispatcher: EventDispatcher = defaultEventDispatcher, @@ -51,14 +52,14 @@ export const createBatchEventProcessor = ( return getOpaqueBatchEventProcessor({ eventDispatcher: options.eventDispatcher || defaultEventDispatcher, - closingEventDispatcher: options.closingEventDispatcher || + closingEventDispatcher: options.closingEventDispatcher || (options.eventDispatcher ? undefined : sendBeaconEventDispatcher), flushInterval: options.flushInterval, batchSize: options.batchSize, defaultFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL, defaultBatchSize: DEFAULT_EVENT_BATCH_SIZE, retryOptions: { - maxRetries: 5, + maxRetries: options.maxRetries ?? EVENT_MAX_RETRIES_BROWSER, }, failedEventRetryInterval: FAILED_EVENT_RETRY_INTERVAL, eventStore, diff --git a/lib/event_processor/event_processor_factory.node.spec.ts b/lib/event_processor/event_processor_factory.node.spec.ts index d94ca4c3e..a8e9ff902 100644 --- a/lib/event_processor/event_processor_factory.node.spec.ts +++ b/lib/event_processor/event_processor_factory.node.spec.ts @@ -184,12 +184,22 @@ describe('createBatchEventProcessor', () => { expect(mockGetOpaqueBatchEventProcessor.mock.calls[1][0].batchSize).toBe(undefined); }); - it('uses maxRetries value of 5', () => { + it('uses maxRetries value of 5 by default', () => { const processor = createBatchEventProcessor({ }); expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true); expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].retryOptions?.maxRetries).toBe(5); }); + it('uses the provided maxRetries value', () => { + const processor1 = createBatchEventProcessor({ maxRetries: 3 }); + expect(Object.is(processor1, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true); + expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].retryOptions?.maxRetries).toBe(3); + + const processor2 = createBatchEventProcessor({ maxRetries: 10 }); + expect(Object.is(processor2, mockGetOpaqueBatchEventProcessor.mock.results[1].value)).toBe(true); + expect(mockGetOpaqueBatchEventProcessor.mock.calls[1][0].retryOptions?.maxRetries).toBe(10); + }); + it('uses no failed event retry if an eventStore is not provided', () => { const processor = createBatchEventProcessor({ }); expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true); diff --git a/lib/event_processor/event_processor_factory.node.ts b/lib/event_processor/event_processor_factory.node.ts index b0ed4ffde..cfa10feae 100644 --- a/lib/event_processor/event_processor_factory.node.ts +++ b/lib/event_processor/event_processor_factory.node.ts @@ -27,6 +27,7 @@ import { export const DEFAULT_EVENT_BATCH_SIZE = 10; export const DEFAULT_EVENT_FLUSH_INTERVAL = 30_000; +export const EVENT_MAX_RETRIES_NODE = 5; export const createForwardingEventProcessor = ( eventDispatcher: EventDispatcher = defaultEventDispatcher, @@ -38,7 +39,7 @@ export const createBatchEventProcessor = ( options: BatchEventProcessorOptions = {} ): OpaqueEventProcessor => { const eventStore = options.eventStore ? getPrefixEventStore(options.eventStore) : undefined; - + return getOpaqueBatchEventProcessor({ eventDispatcher: options.eventDispatcher || defaultEventDispatcher, closingEventDispatcher: options.closingEventDispatcher, @@ -47,8 +48,8 @@ export const createBatchEventProcessor = ( defaultFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL, defaultBatchSize: DEFAULT_EVENT_BATCH_SIZE, retryOptions: { - maxRetries: 5, - + maxRetries: options.maxRetries ?? EVENT_MAX_RETRIES_NODE, + }, failedEventRetryInterval: eventStore ? FAILED_EVENT_RETRY_INTERVAL : undefined, eventStore, diff --git a/lib/event_processor/event_processor_factory.react_native.spec.ts b/lib/event_processor/event_processor_factory.react_native.spec.ts index 6d559690a..fac03586b 100644 --- a/lib/event_processor/event_processor_factory.react_native.spec.ts +++ b/lib/event_processor/event_processor_factory.react_native.spec.ts @@ -278,12 +278,22 @@ describe('createBatchEventProcessor', () => { expect(mockGetOpaqueBatchEventProcessor.mock.calls[1][0].batchSize).toBe(undefined); }); - it('uses maxRetries value of 5', () => { + it('uses maxRetries value of 5 by default', () => { const processor = createBatchEventProcessor({}); expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true); expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].retryOptions?.maxRetries).toBe(5); }); + it('uses the provided maxRetries value', () => { + const processor1 = createBatchEventProcessor({ maxRetries: 3 }); + expect(Object.is(processor1, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true); + expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].retryOptions?.maxRetries).toBe(3); + + const processor2 = createBatchEventProcessor({ maxRetries: 10 }); + expect(Object.is(processor2, mockGetOpaqueBatchEventProcessor.mock.results[1].value)).toBe(true); + expect(mockGetOpaqueBatchEventProcessor.mock.calls[1][0].retryOptions?.maxRetries).toBe(10); + }); + it('uses the default failedEventRetryInterval', () => { const processor = createBatchEventProcessor({}); expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true); diff --git a/lib/event_processor/event_processor_factory.react_native.ts b/lib/event_processor/event_processor_factory.react_native.ts index db80ba0e8..0d2f00971 100644 --- a/lib/event_processor/event_processor_factory.react_native.ts +++ b/lib/event_processor/event_processor_factory.react_native.ts @@ -31,6 +31,7 @@ import { DEFAULT_MAX_EVENTS_IN_STORE, EventStore } from './event_store'; export const DEFAULT_EVENT_BATCH_SIZE = 10; export const DEFAULT_EVENT_FLUSH_INTERVAL = 1_000; +export const EVENT_MAX_RETRIES_REACT_NATIVE = 5; export const createForwardingEventProcessor = ( eventDispatcher: EventDispatcher = defaultEventDispatcher, @@ -47,7 +48,7 @@ export const createBatchEventProcessor = ( : DEFAULT_MAX_EVENTS_IN_STORE, ttl: options.storeTtl, }); - + return getOpaqueBatchEventProcessor( { eventDispatcher: options.eventDispatcher || defaultEventDispatcher, @@ -57,7 +58,7 @@ export const createBatchEventProcessor = ( defaultFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL, defaultBatchSize: DEFAULT_EVENT_BATCH_SIZE, retryOptions: { - maxRetries: 5, + maxRetries: options.maxRetries ?? EVENT_MAX_RETRIES_REACT_NATIVE, }, failedEventRetryInterval: FAILED_EVENT_RETRY_INTERVAL, eventStore, diff --git a/lib/event_processor/event_processor_factory.ts b/lib/event_processor/event_processor_factory.ts index 50663f664..7c7fda93d 100644 --- a/lib/event_processor/event_processor_factory.ts +++ b/lib/event_processor/event_processor_factory.ts @@ -61,6 +61,7 @@ export type BatchEventProcessorOptions = { batchSize?: number; storeTtl?: number; eventStore?: Store; + maxRetries?: number; }; export type BatchEventProcessorFactoryOptions = Omit & {