-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathevent_processor_factory.ts
More file actions
180 lines (155 loc) · 5.87 KB
/
event_processor_factory.ts
File metadata and controls
180 lines (155 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { LogLevel } from "../logging/logger";
import { StartupLog } from "../service";
import { AsyncPrefixStore, Store, SyncPrefixStore } from "../utils/cache/store";
import { validateStore } from "../utils/cache/store_validator";
import { ExponentialBackoff, IntervalRepeater } from "../utils/repeater/repeater";
import { Maybe } from "../utils/type";
import { BatchEventProcessor, DEFAULT_MAX_BACKOFF, DEFAULT_MIN_BACKOFF, EventWithId, RetryConfig } from "./batch_event_processor";
import { EventDispatcher } from "./event_dispatcher/event_dispatcher";
import { EventProcessor } from "./event_processor";
import { EVENT_STORE_PREFIX } from "./event_store";
import { ForwardingEventProcessor } from "./forwarding_event_processor";
import { Platform } from '../platform_support';
export const INVALID_EVENT_DISPATCHER = 'Invalid event dispatcher';
export const FAILED_EVENT_RETRY_INTERVAL = 20 * 1000;
export const getPrefixEventStore = (store: Store<string>): Store<EventWithId> => {
if (store.operation === 'async') {
return new AsyncPrefixStore<string, EventWithId>(
store,
EVENT_STORE_PREFIX,
JSON.parse,
JSON.stringify,
);
} else {
return new SyncPrefixStore<string, EventWithId>(
store,
EVENT_STORE_PREFIX,
JSON.parse,
JSON.stringify,
);
}
};
const eventProcessorSymbol: unique symbol = Symbol();
export type OpaqueEventProcessor = {
[eventProcessorSymbol]: unknown;
};
export type BatchEventProcessorOptions = {
eventDispatcher?: EventDispatcher;
closingEventDispatcher?: EventDispatcher;
flushInterval?: number;
batchSize?: number;
storeTtl?: number;
eventStore?: Store<string>;
maxRetries?: number;
};
export type BatchEventProcessorFactoryOptions = Omit<BatchEventProcessorOptions, 'eventDispatcher' | 'eventStore' > & {
eventDispatcher: EventDispatcher;
closingEventDispatcher?: EventDispatcher;
failedEventRetryInterval?: number;
defaultFlushInterval: number;
defaultBatchSize: number;
eventStore?: Store<EventWithId>;
retryOptions?: {
maxRetries: number;
minBackoff?: number;
maxBackoff?: number;
};
}
export const validateEventDispatcher = (eventDispatcher: EventDispatcher): void => {
if (!eventDispatcher || typeof eventDispatcher !== 'object' || typeof eventDispatcher.dispatchEvent !== 'function') {
throw new Error(INVALID_EVENT_DISPATCHER);
}
}
export const getBatchEventProcessor = (
options: BatchEventProcessorFactoryOptions,
EventProcessorConstructor: typeof BatchEventProcessor = BatchEventProcessor
): EventProcessor => {
const { eventDispatcher, closingEventDispatcher, retryOptions, eventStore } = options;
validateEventDispatcher(eventDispatcher);
if (closingEventDispatcher) {
validateEventDispatcher(closingEventDispatcher);
}
if (eventStore) {
validateStore(eventStore);
}
const retryConfig: RetryConfig | undefined = retryOptions ? {
maxRetries: retryOptions.maxRetries,
backoffProvider: () => {
const minBackoff = retryOptions?.minBackoff ?? DEFAULT_MIN_BACKOFF;
const maxBackoff = retryOptions?.maxBackoff ?? DEFAULT_MAX_BACKOFF;
return new ExponentialBackoff(minBackoff, maxBackoff, 50);
}
} : undefined;
const startupLogs: StartupLog[] = [];
const { defaultFlushInterval, defaultBatchSize } = options;
let flushInterval = defaultFlushInterval;
if (options.flushInterval === undefined || options.flushInterval <= 0) {
startupLogs.push({
level: LogLevel.Warn,
message: 'Invalid flushInterval %s, defaulting to %s',
params: [options.flushInterval, defaultFlushInterval],
});
} else {
flushInterval = options.flushInterval;
}
let batchSize = defaultBatchSize;
if (options.batchSize === undefined || options.batchSize <= 0) {
startupLogs.push({
level: LogLevel.Warn,
message: 'Invalid batchSize %s, defaulting to %s',
params: [options.batchSize, defaultBatchSize],
});
} else {
batchSize = options.batchSize;
}
const dispatchRepeater = new IntervalRepeater(flushInterval);
const failedEventRepeater = options.failedEventRetryInterval ?
new IntervalRepeater(options.failedEventRetryInterval) : undefined;
return new EventProcessorConstructor({
eventDispatcher,
closingEventDispatcher,
dispatchRepeater,
failedEventRepeater,
retryConfig,
batchSize,
eventStore,
startupLogs,
});
}
export const wrapEventProcessor = (eventProcessor: EventProcessor): OpaqueEventProcessor => {
return {
[eventProcessorSymbol]: eventProcessor,
};
}
export const getOpaqueBatchEventProcessor = (
options: BatchEventProcessorFactoryOptions,
EventProcessorConstructor: typeof BatchEventProcessor = BatchEventProcessor
): OpaqueEventProcessor => {
return wrapEventProcessor(getBatchEventProcessor(options, EventProcessorConstructor));
}
export const extractEventProcessor = (eventProcessor: Maybe<OpaqueEventProcessor>): Maybe<EventProcessor> => {
if (!eventProcessor || typeof eventProcessor !== 'object') {
return undefined;
}
return eventProcessor[eventProcessorSymbol] as Maybe<EventProcessor>;
}
export function getForwardingEventProcessor(dispatcher: EventDispatcher): EventProcessor {
validateEventDispatcher(dispatcher);
return new ForwardingEventProcessor(dispatcher);
}
export const __platforms: Platform[] = ['__universal__'];