Skip to content

Commit cedb007

Browse files
author
Matt Carroll
committed
If batchSize = 0 or flushInterval = 0, log that values are invalid and pick up default values instead
1 parent 9926334 commit cedb007

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

packages/event-processor/CHANGELOG.MD

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77
## [Unreleased]
88
Changes that have landed but are not yet released.
99

10+
### New Features
11+
- In `AbstractEventProcessor`, validate maxQueueSize and flushInterval; ignore & use default values when invalid
12+
13+
### Changed
14+
- Removed transformers, interceptors, and callbacks from `AbstractEventProcessor`
15+
1016
## [0.2.1] - June 6, 2019
1117

1218
- Wrap the `callback` in `try/catch` when implementing a custom `eventDispatcher`. This ensures invoking the `callback` will always cleanup any pending retry tasks.
@@ -18,4 +24,4 @@ events that did not send successfully due to page navigation
1824

1925
## [0.1.0] - March 1, 2019
2026

21-
Initial release
27+
Initial release

packages/event-processor/__tests__/v1EventProcessor.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,5 +340,53 @@ describe('LogTierV1EventProcessor', () => {
340340
// flushing should reset queue, at this point only has two events
341341
expect(dispatchStub).toHaveBeenCalledTimes(1)
342342
})
343+
344+
})
345+
346+
describe('invalid flushInterval or maxQueueSize', () => {
347+
it('should ignore a flushInterval of 0 and use the default', () => {
348+
const processor = new LogTierV1EventProcessor({
349+
dispatcher: stubDispatcher,
350+
flushInterval: 0,
351+
maxQueueSize: 10,
352+
})
353+
processor.start()
354+
355+
const impressionEvent1 = createImpressionEvent()
356+
processor.process(impressionEvent1)
357+
expect(dispatchStub).toHaveBeenCalledTimes(0)
358+
jest.advanceTimersByTime(30000)
359+
expect(dispatchStub).toHaveBeenCalledTimes(1)
360+
expect(dispatchStub).toHaveBeenCalledWith({
361+
url: 'https://logx.optimizely.com/v1/events',
362+
httpVerb: 'POST',
363+
params: makeBatchedEventV1([impressionEvent1]),
364+
})
365+
})
366+
367+
it('should ignore a maxQueueSize of 0 and use the default', () => {
368+
const processor = new LogTierV1EventProcessor({
369+
dispatcher: stubDispatcher,
370+
flushInterval: 30000,
371+
maxQueueSize: 0,
372+
})
373+
processor.start()
374+
375+
const impressionEvent1 = createImpressionEvent()
376+
processor.process(impressionEvent1)
377+
expect(dispatchStub).toHaveBeenCalledTimes(0)
378+
const impressionEvents = [impressionEvent1]
379+
for (let i = 0; i < 9; i++) {
380+
const evt = createImpressionEvent()
381+
processor.process(evt)
382+
impressionEvents.push(evt)
383+
}
384+
expect(dispatchStub).toHaveBeenCalledTimes(1)
385+
expect(dispatchStub).toHaveBeenCalledWith({
386+
url: 'https://logx.optimizely.com/v1/events',
387+
httpVerb: 'POST',
388+
params: makeBatchedEventV1(impressionEvents),
389+
})
390+
})
343391
})
344392
})

packages/event-processor/src/eventProcessor.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,39 @@ export interface EventProcessor extends Managed {
3333
process(event: ProcessableEvents): void
3434
}
3535

36-
const MIN_FLUSH_INTERVAL = 100
36+
const DEFAULT_FLUSH_INTERVAL = 30000
37+
const DEFAULT_MAX_QUEUE_SIZE = 10
38+
3739
export abstract class AbstractEventProcessor implements EventProcessor {
3840
protected dispatcher: EventDispatcher
3941
protected queue: EventQueue<ProcessableEvents>
4042

4143
constructor({
4244
dispatcher,
43-
flushInterval = 30000,
44-
maxQueueSize = 3000,
45+
flushInterval = DEFAULT_FLUSH_INTERVAL,
46+
maxQueueSize = DEFAULT_MAX_QUEUE_SIZE,
4547
}: {
4648
dispatcher: EventDispatcher
4749
flushInterval?: number
4850
maxQueueSize?: number
4951
}) {
5052
this.dispatcher = dispatcher
5153

54+
if (flushInterval <= 0) {
55+
logger.warn(`Invalid flushInterval ${flushInterval}, defaulting to ${DEFAULT_FLUSH_INTERVAL}`)
56+
flushInterval = DEFAULT_FLUSH_INTERVAL
57+
}
58+
59+
maxQueueSize = Math.floor(maxQueueSize)
60+
if (maxQueueSize < 1) {
61+
logger.warn(`Invalid maxQueueSize, defaulting to ${DEFAULT_MAX_QUEUE_SIZE}`)
62+
maxQueueSize = DEFAULT_MAX_QUEUE_SIZE
63+
}
64+
5265
maxQueueSize = Math.max(1, maxQueueSize)
5366
if (maxQueueSize > 1) {
5467
this.queue = new DefaultEventQueue({
55-
flushInterval: Math.max(flushInterval, MIN_FLUSH_INTERVAL),
68+
flushInterval,
5669
maxQueueSize,
5770
sink: buffer => this.drainQueue(buffer),
5871
})

0 commit comments

Comments
 (0)