Skip to content

Commit 5255f06

Browse files
Merge pull request #373 from splitio/impressions_toggle_storage_refactors
[Impressions toggle] Storage refactors
2 parents e462bba + a771764 commit 5255f06

File tree

10 files changed

+54
-62
lines changed

10 files changed

+54
-62
lines changed

src/listeners/browser.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { IResponse, ISplitApi } from '../services/types';
88
import { ISettings } from '../types';
99
import SplitIO from '../../types/splitio';
1010
import { ImpressionsPayload } from '../sync/submitters/types';
11-
import { OPTIMIZED, DEBUG, NONE } from '../utils/constants';
1211
import { objectAssign } from '../utils/lang/objectAssign';
1312
import { CLEANUP_REGISTERING, CLEANUP_DEREGISTERING } from '../logger/constants';
1413
import { ISyncManager } from '../sync/types';
@@ -78,10 +77,9 @@ export class BrowserSignalListener implements ISignalListener {
7877

7978
// Flush impressions & events data if there is user consent
8079
if (isConsentGranted(this.settings)) {
81-
const sim = this.settings.sync.impressionsMode;
8280
const extraMetadata = {
8381
// sim stands for Sync/Split Impressions Mode
84-
sim: sim === OPTIMIZED ? OPTIMIZED : sim === DEBUG ? DEBUG : NONE
82+
sim: this.settings.sync.impressionsMode
8583
};
8684

8785
this._flushData(events + '/testImpressions/beacon', this.storage.impressions, this.serviceApi.postTestImpressionsBulk, this.fromImpressionsCollector, extraMetadata);

src/storages/inLocalStorage/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal';
1010
import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../../utils/constants/browser';
1111
import { InMemoryStorageCSFactory } from '../inMemory/InMemoryStorageCS';
1212
import { LOG_PREFIX } from './constants';
13-
import { DEBUG, NONE, STORAGE_LOCALSTORAGE } from '../../utils/constants';
13+
import { STORAGE_LOCALSTORAGE } from '../../utils/constants';
1414
import { shouldRecordTelemetry, TelemetryCacheInMemory } from '../inMemory/TelemetryCacheInMemory';
1515
import { UniqueKeysCacheInMemoryCS } from '../inMemory/UniqueKeysCacheInMemoryCS';
1616
import { getMatching } from '../../utils/key';
@@ -34,7 +34,7 @@ export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyn
3434
return InMemoryStorageCSFactory(params);
3535
}
3636

37-
const { settings, settings: { log, scheduler: { impressionsQueueSize, eventsQueueSize, }, sync: { impressionsMode } } } = params;
37+
const { settings, settings: { log, scheduler: { impressionsQueueSize, eventsQueueSize } } } = params;
3838
const matchingKey = getMatching(settings.core.key);
3939
const keys = new KeyBuilderCS(prefix, matchingKey);
4040
const expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS;
@@ -48,10 +48,10 @@ export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyn
4848
segments,
4949
largeSegments,
5050
impressions: new ImpressionsCacheInMemory(impressionsQueueSize),
51-
impressionCounts: impressionsMode !== DEBUG ? new ImpressionCountsCacheInMemory() : undefined,
51+
impressionCounts: new ImpressionCountsCacheInMemory(),
5252
events: new EventsCacheInMemory(eventsQueueSize),
5353
telemetry: shouldRecordTelemetry(params) ? new TelemetryCacheInMemory(splits, segments) : undefined,
54-
uniqueKeys: impressionsMode === NONE ? new UniqueKeysCacheInMemoryCS() : undefined,
54+
uniqueKeys: new UniqueKeysCacheInMemoryCS(),
5555

5656
destroy() { },
5757

@@ -66,6 +66,7 @@ export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyn
6666
impressionCounts: this.impressionCounts,
6767
events: this.events,
6868
telemetry: this.telemetry,
69+
uniqueKeys: this.uniqueKeys,
6970

7071
destroy() { }
7172
};

src/storages/inMemory/InMemoryStorage.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ImpressionsCacheInMemory } from './ImpressionsCacheInMemory';
44
import { EventsCacheInMemory } from './EventsCacheInMemory';
55
import { IStorageFactoryParams, IStorageSync } from '../types';
66
import { ImpressionCountsCacheInMemory } from './ImpressionCountsCacheInMemory';
7-
import { DEBUG, LOCALHOST_MODE, NONE, STORAGE_MEMORY } from '../../utils/constants';
7+
import { LOCALHOST_MODE, STORAGE_MEMORY } from '../../utils/constants';
88
import { shouldRecordTelemetry, TelemetryCacheInMemory } from './TelemetryCacheInMemory';
99
import { UniqueKeysCacheInMemory } from './UniqueKeysCacheInMemory';
1010

@@ -14,7 +14,7 @@ import { UniqueKeysCacheInMemory } from './UniqueKeysCacheInMemory';
1414
* @param params - parameters required by EventsCacheSync
1515
*/
1616
export function InMemoryStorageFactory(params: IStorageFactoryParams): IStorageSync {
17-
const { settings: { scheduler: { impressionsQueueSize, eventsQueueSize, }, sync: { impressionsMode, __splitFiltersValidation } } } = params;
17+
const { settings: { scheduler: { impressionsQueueSize, eventsQueueSize, }, sync: { __splitFiltersValidation } } } = params;
1818

1919
const splits = new SplitsCacheInMemory(__splitFiltersValidation);
2020
const segments = new SegmentsCacheInMemory();
@@ -23,10 +23,10 @@ export function InMemoryStorageFactory(params: IStorageFactoryParams): IStorageS
2323
splits,
2424
segments,
2525
impressions: new ImpressionsCacheInMemory(impressionsQueueSize),
26-
impressionCounts: impressionsMode !== DEBUG ? new ImpressionCountsCacheInMemory() : undefined,
26+
impressionCounts: new ImpressionCountsCacheInMemory(),
2727
events: new EventsCacheInMemory(eventsQueueSize),
2828
telemetry: shouldRecordTelemetry(params) ? new TelemetryCacheInMemory(splits, segments) : undefined,
29-
uniqueKeys: impressionsMode === NONE ? new UniqueKeysCacheInMemory() : undefined,
29+
uniqueKeys: new UniqueKeysCacheInMemory(),
3030

3131
destroy() { }
3232
};
@@ -37,8 +37,8 @@ export function InMemoryStorageFactory(params: IStorageFactoryParams): IStorageS
3737
const noopTrack = () => true;
3838
storage.impressions.track = noopTrack;
3939
storage.events.track = noopTrack;
40-
if (storage.impressionCounts) storage.impressionCounts.track = noopTrack;
41-
if (storage.uniqueKeys) storage.uniqueKeys.track = noopTrack;
40+
storage.impressionCounts.track = noopTrack;
41+
storage.uniqueKeys.track = noopTrack;
4242
}
4343

4444
return storage;

src/storages/inMemory/InMemoryStorageCS.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ImpressionsCacheInMemory } from './ImpressionsCacheInMemory';
44
import { EventsCacheInMemory } from './EventsCacheInMemory';
55
import { IStorageSync, IStorageFactoryParams } from '../types';
66
import { ImpressionCountsCacheInMemory } from './ImpressionCountsCacheInMemory';
7-
import { DEBUG, LOCALHOST_MODE, NONE, STORAGE_MEMORY } from '../../utils/constants';
7+
import { LOCALHOST_MODE, STORAGE_MEMORY } from '../../utils/constants';
88
import { shouldRecordTelemetry, TelemetryCacheInMemory } from './TelemetryCacheInMemory';
99
import { UniqueKeysCacheInMemoryCS } from './UniqueKeysCacheInMemoryCS';
1010

@@ -14,7 +14,7 @@ import { UniqueKeysCacheInMemoryCS } from './UniqueKeysCacheInMemoryCS';
1414
* @param params - parameters required by EventsCacheSync
1515
*/
1616
export function InMemoryStorageCSFactory(params: IStorageFactoryParams): IStorageSync {
17-
const { settings: { scheduler: { impressionsQueueSize, eventsQueueSize, }, sync: { impressionsMode, __splitFiltersValidation } } } = params;
17+
const { settings: { scheduler: { impressionsQueueSize, eventsQueueSize }, sync: { __splitFiltersValidation } } } = params;
1818

1919
const splits = new SplitsCacheInMemory(__splitFiltersValidation);
2020
const segments = new MySegmentsCacheInMemory();
@@ -25,10 +25,10 @@ export function InMemoryStorageCSFactory(params: IStorageFactoryParams): IStorag
2525
segments,
2626
largeSegments,
2727
impressions: new ImpressionsCacheInMemory(impressionsQueueSize),
28-
impressionCounts: impressionsMode !== DEBUG ? new ImpressionCountsCacheInMemory() : undefined,
28+
impressionCounts: new ImpressionCountsCacheInMemory(),
2929
events: new EventsCacheInMemory(eventsQueueSize),
3030
telemetry: shouldRecordTelemetry(params) ? new TelemetryCacheInMemory(splits, segments) : undefined,
31-
uniqueKeys: impressionsMode === NONE ? new UniqueKeysCacheInMemoryCS() : undefined,
31+
uniqueKeys: new UniqueKeysCacheInMemoryCS(),
3232

3333
destroy() { },
3434

@@ -42,6 +42,7 @@ export function InMemoryStorageCSFactory(params: IStorageFactoryParams): IStorag
4242
impressionCounts: this.impressionCounts,
4343
events: this.events,
4444
telemetry: this.telemetry,
45+
uniqueKeys: this.uniqueKeys,
4546

4647
destroy() { }
4748
};
@@ -54,8 +55,8 @@ export function InMemoryStorageCSFactory(params: IStorageFactoryParams): IStorag
5455
const noopTrack = () => true;
5556
storage.impressions.track = noopTrack;
5657
storage.events.track = noopTrack;
57-
if (storage.impressionCounts) storage.impressionCounts.track = noopTrack;
58-
if (storage.uniqueKeys) storage.uniqueKeys.track = noopTrack;
58+
storage.impressionCounts.track = noopTrack;
59+
storage.uniqueKeys.track = noopTrack;
5960
}
6061

6162
return storage;

src/storages/inRedis/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { SplitsCacheInRedis } from './SplitsCacheInRedis';
66
import { SegmentsCacheInRedis } from './SegmentsCacheInRedis';
77
import { ImpressionsCacheInRedis } from './ImpressionsCacheInRedis';
88
import { EventsCacheInRedis } from './EventsCacheInRedis';
9-
import { DEBUG, NONE, STORAGE_REDIS } from '../../utils/constants';
9+
import { STORAGE_REDIS } from '../../utils/constants';
1010
import { TelemetryCacheInRedis } from './TelemetryCacheInRedis';
1111
import { UniqueKeysCacheInRedis } from './UniqueKeysCacheInRedis';
1212
import { ImpressionCountsCacheInRedis } from './ImpressionCountsCacheInRedis';
@@ -30,19 +30,19 @@ export function InRedisStorage(options: InRedisStorageOptions = {}): IStorageAsy
3030
const prefix = validatePrefix(options.prefix);
3131

3232
function InRedisStorageFactory(params: IStorageFactoryParams): IStorageAsync {
33-
const { onReadyCb, settings, settings: { log, sync: { impressionsMode } } } = params;
33+
const { onReadyCb, settings, settings: { log } } = params;
3434
const metadata = metadataBuilder(settings);
3535
const keys = new KeyBuilderSS(prefix, metadata);
3636
const redisClient: RedisAdapter = new RD(log, options.options || {});
3737
const telemetry = new TelemetryCacheInRedis(log, keys, redisClient);
38-
const impressionCountsCache = impressionsMode !== DEBUG ? new ImpressionCountsCacheInRedis(log, keys.buildImpressionsCountKey(), redisClient) : undefined;
39-
const uniqueKeysCache = impressionsMode === NONE ? new UniqueKeysCacheInRedis(log, keys.buildUniqueKeysKey(), redisClient) : undefined;
38+
const impressionCountsCache = new ImpressionCountsCacheInRedis(log, keys.buildImpressionsCountKey(), redisClient);
39+
const uniqueKeysCache = new UniqueKeysCacheInRedis(log, keys.buildUniqueKeysKey(), redisClient);
4040

4141
// subscription to Redis connect event in order to emit SDK_READY event on consumer mode
4242
redisClient.on('connect', () => {
4343
onReadyCb();
44-
if (impressionCountsCache) impressionCountsCache.start();
45-
if (uniqueKeysCache) uniqueKeysCache.start();
44+
impressionCountsCache.start();
45+
uniqueKeysCache.start();
4646

4747
// Synchronize config
4848
telemetry.recordConfig();
@@ -60,10 +60,10 @@ export function InRedisStorage(options: InRedisStorageOptions = {}): IStorageAsy
6060
// When using REDIS we should:
6161
// 1- Disconnect from the storage
6262
destroy(): Promise<void> {
63-
let promises = [];
64-
if (impressionCountsCache) promises.push(impressionCountsCache.stop());
65-
if (uniqueKeysCache) promises.push(uniqueKeysCache.stop());
66-
return Promise.all(promises).then(() => { redisClient.disconnect(); });
63+
return Promise.all([
64+
impressionCountsCache.stop(),
65+
uniqueKeysCache.stop()
66+
]).then(() => { redisClient.disconnect(); });
6767
// @TODO check that caches works as expected when redisClient is disconnected
6868
}
6969
};

src/storages/pluggable/index.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { EventsCachePluggable } from './EventsCachePluggable';
88
import { wrapperAdapter, METHODS_TO_PROMISE_WRAP } from './wrapperAdapter';
99
import { isObject } from '../../utils/lang';
1010
import { getStorageHash, validatePrefix } from '../KeyBuilder';
11-
import { CONSUMER_PARTIAL_MODE, DEBUG, NONE, STORAGE_PLUGGABLE } from '../../utils/constants';
11+
import { CONSUMER_PARTIAL_MODE, STORAGE_PLUGGABLE } from '../../utils/constants';
1212
import { ImpressionsCacheInMemory } from '../inMemory/ImpressionsCacheInMemory';
1313
import { EventsCacheInMemory } from '../inMemory/EventsCacheInMemory';
1414
import { ImpressionCountsCacheInMemory } from '../inMemory/ImpressionCountsCacheInMemory';
@@ -63,35 +63,31 @@ export function PluggableStorage(options: PluggableStorageOptions): IStorageAsyn
6363
const prefix = validatePrefix(options.prefix);
6464

6565
function PluggableStorageFactory(params: IStorageFactoryParams): IStorageAsync {
66-
const { onReadyCb, settings, settings: { log, mode, sync: { impressionsMode }, scheduler: { impressionsQueueSize, eventsQueueSize } } } = params;
66+
const { onReadyCb, settings, settings: { log, mode, scheduler: { impressionsQueueSize, eventsQueueSize } } } = params;
6767
const metadata = metadataBuilder(settings);
6868
const keys = new KeyBuilderSS(prefix, metadata);
6969
const wrapper = wrapperAdapter(log, options.wrapper);
7070

71-
const isSyncronizer = mode === undefined; // If mode is not defined, the synchronizer is running
71+
const isSynchronizer = mode === undefined; // If mode is not defined, the synchronizer is running
7272
const isPartialConsumer = mode === CONSUMER_PARTIAL_MODE;
7373

74-
const telemetry = shouldRecordTelemetry(params) || isSyncronizer ?
74+
const telemetry = shouldRecordTelemetry(params) || isSynchronizer ?
7575
isPartialConsumer ?
7676
new TelemetryCacheInMemory() :
7777
new TelemetryCachePluggable(log, keys, wrapper) :
7878
undefined;
7979

80-
const impressionCountsCache = impressionsMode !== DEBUG || isSyncronizer ?
81-
isPartialConsumer ?
82-
new ImpressionCountsCacheInMemory() :
83-
new ImpressionCountsCachePluggable(log, keys.buildImpressionsCountKey(), wrapper) :
84-
undefined;
80+
const impressionCountsCache = isPartialConsumer ?
81+
new ImpressionCountsCacheInMemory() :
82+
new ImpressionCountsCachePluggable(log, keys.buildImpressionsCountKey(), wrapper);
8583

86-
const uniqueKeysCache = impressionsMode === NONE || isSyncronizer ?
87-
isPartialConsumer ?
88-
settings.core.key === undefined ? new UniqueKeysCacheInMemory() : new UniqueKeysCacheInMemoryCS() :
89-
new UniqueKeysCachePluggable(log, keys.buildUniqueKeysKey(), wrapper) :
90-
undefined;
84+
const uniqueKeysCache = isPartialConsumer ?
85+
settings.core.key === undefined ? new UniqueKeysCacheInMemory() : new UniqueKeysCacheInMemoryCS() :
86+
new UniqueKeysCachePluggable(log, keys.buildUniqueKeysKey(), wrapper);
9187

9288
// Connects to wrapper and emits SDK_READY event on main client
9389
const connectPromise = wrapper.connect().then(() => {
94-
if (isSyncronizer) {
90+
if (isSynchronizer) {
9591
// In standalone or producer mode, clear storage if SDK key or feature flag filter has changed
9692
return wrapper.get(keys.buildHashKey()).then((hash) => {
9793
const currentHash = getStorageHash(settings);
@@ -106,8 +102,8 @@ export function PluggableStorage(options: PluggableStorageOptions): IStorageAsyn
106102
});
107103
} else {
108104
// Start periodic flush of async storages if not running synchronizer (producer mode)
109-
if (impressionCountsCache && (impressionCountsCache as ImpressionCountsCachePluggable).start) (impressionCountsCache as ImpressionCountsCachePluggable).start();
110-
if (uniqueKeysCache && (uniqueKeysCache as UniqueKeysCachePluggable).start) (uniqueKeysCache as UniqueKeysCachePluggable).start();
105+
if ((impressionCountsCache as ImpressionCountsCachePluggable).start) (impressionCountsCache as ImpressionCountsCachePluggable).start();
106+
if ((uniqueKeysCache as UniqueKeysCachePluggable).start) (uniqueKeysCache as UniqueKeysCachePluggable).start();
111107
if (telemetry && (telemetry as ITelemetryCacheAsync).recordConfig) (telemetry as ITelemetryCacheAsync).recordConfig();
112108

113109
onReadyCb();
@@ -129,9 +125,9 @@ export function PluggableStorage(options: PluggableStorageOptions): IStorageAsyn
129125

130126
// Stop periodic flush and disconnect the underlying storage
131127
destroy() {
132-
return Promise.all(isSyncronizer ? [] : [
133-
impressionCountsCache && (impressionCountsCache as ImpressionCountsCachePluggable).stop && (impressionCountsCache as ImpressionCountsCachePluggable).stop(),
134-
uniqueKeysCache && (uniqueKeysCache as UniqueKeysCachePluggable).stop && (uniqueKeysCache as UniqueKeysCachePluggable).stop(),
128+
return Promise.all(isSynchronizer ? [] : [
129+
(impressionCountsCache as ImpressionCountsCachePluggable).stop && (impressionCountsCache as ImpressionCountsCachePluggable).stop(),
130+
(uniqueKeysCache as UniqueKeysCachePluggable).stop && (uniqueKeysCache as UniqueKeysCachePluggable).stop(),
135131
]).then(() => wrapper.disconnect());
136132
},
137133

src/storages/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,10 @@ export interface IStorageBase<
439439
splits: TSplitsCache,
440440
segments: TSegmentsCache,
441441
impressions: TImpressionsCache,
442-
impressionCounts?: TImpressionsCountCache,
442+
impressionCounts: TImpressionsCountCache,
443443
events: TEventsCache,
444444
telemetry?: TTelemetryCache,
445-
uniqueKeys?: TUniqueKeysCache,
445+
uniqueKeys: TUniqueKeysCache,
446446
destroy(): void | Promise<void>,
447447
shared?: (matchingKey: string, onReadyCb: (error?: any) => void) => this
448448
}

src/sync/submitters/impressionCountsSubmitter.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ export function impressionCountsSubmitterFactory(params: ISdkFactoryContextSync)
3939
storage: { impressionCounts }
4040
} = params;
4141

42-
if (impressionCounts) {
43-
// retry impressions counts only once.
44-
return submitterFactory(log, postTestImpressionsCount, impressionCounts, IMPRESSIONS_COUNT_RATE, 'impression counts', fromImpressionCountsCollector, 1);
45-
}
42+
// retry impressions counts only once.
43+
return submitterFactory(log, postTestImpressionsCount, impressionCounts, IMPRESSIONS_COUNT_RATE, 'impression counts', fromImpressionCountsCollector, 1);
4644
}

src/sync/submitters/submitterManager.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ export function submitterManagerFactory(params: ISdkFactoryContextSync): ISubmit
1010

1111
const submitters = [
1212
impressionsSubmitterFactory(params),
13-
eventsSubmitterFactory(params)
13+
eventsSubmitterFactory(params),
14+
impressionCountsSubmitterFactory(params),
15+
uniqueKeysSubmitterFactory(params)
1416
];
1517

16-
const impressionCountsSubmitter = impressionCountsSubmitterFactory(params);
17-
if (impressionCountsSubmitter) submitters.push(impressionCountsSubmitter);
1818
const telemetrySubmitter = telemetrySubmitterFactory(params);
19-
if (params.storage.uniqueKeys) submitters.push(uniqueKeysSubmitterFactory(params));
2019

2120
return {
2221
// `onlyTelemetry` true if SDK is created with userConsent not GRANTED

src/sync/submitters/uniqueKeysSubmitter.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ export function uniqueKeysSubmitterFactory(params: ISdkFactoryContextSync) {
1919
const isClientSide = key !== undefined;
2020
const postUniqueKeysBulk = isClientSide ? postUniqueKeysBulkCs : postUniqueKeysBulkSs;
2121

22-
const syncTask = submitterFactory(log, postUniqueKeysBulk, uniqueKeys!, UNIQUE_KEYS_RATE, DATA_NAME);
22+
const syncTask = submitterFactory(log, postUniqueKeysBulk, uniqueKeys, UNIQUE_KEYS_RATE, DATA_NAME);
2323

2424
// register unique keys submitter to be executed when uniqueKeys cache is full
25-
uniqueKeys!.setOnFullQueueCb(() => {
25+
uniqueKeys.setOnFullQueueCb(() => {
2626
if (syncTask.isRunning()) {
2727
log.info(SUBMITTERS_PUSH_FULL_QUEUE, [DATA_NAME]);
2828
syncTask.execute();
@@ -33,4 +33,3 @@ export function uniqueKeysSubmitterFactory(params: ISdkFactoryContextSync) {
3333

3434
return syncTask;
3535
}
36-

0 commit comments

Comments
 (0)