Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/storages/inLocalStorage/MySegmentsCacheInLocal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ILogger } from '../../logger/types';
import AbstractSegmentsCacheSync from '../AbstractSegmentsCacheSync';
import KeyBuilderCS from '../KeyBuilderCS';
import { logPrefix, DEFINED } from './constants';
import { LOG_PREFIX, DEFINED } from './constants';

export default class MySegmentsCacheInLocal extends AbstractSegmentsCacheSync {

Expand All @@ -20,7 +20,7 @@ export default class MySegmentsCacheInLocal extends AbstractSegmentsCacheSync {
* @NOTE this method is not being used at the moment.
*/
clear() {
this.log.info(logPrefix + 'Flushing MySegments data from localStorage');
this.log.info(LOG_PREFIX + 'Flushing MySegments data from localStorage');

// We cannot simply call `localStorage.clear()` since that implies removing user items from the storage
// We could optimize next sentence, since it implies iterating over all localStorage items
Expand All @@ -34,7 +34,7 @@ export default class MySegmentsCacheInLocal extends AbstractSegmentsCacheSync {
localStorage.setItem(segmentKey, DEFINED);
return true;
} catch (e) {
this.log.error(logPrefix + e);
this.log.error(LOG_PREFIX + e);
return false;
}
}
Expand All @@ -46,7 +46,7 @@ export default class MySegmentsCacheInLocal extends AbstractSegmentsCacheSync {
localStorage.removeItem(segmentKey);
return true;
} catch (e) {
this.log.error(logPrefix + e);
this.log.error(LOG_PREFIX + e);
return false;
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/storages/inLocalStorage/SplitsCacheInLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AbstractSplitsCacheSync, { usesSegments } from '../AbstractSplitsCacheSyn
import { isFiniteNumber, toNumber, isNaNNumber } from '../../utils/lang';
import KeyBuilderCS from '../KeyBuilderCS';
import { ILogger } from '../../logger/types';
import { logPrefix } from './constants';
import { LOG_PREFIX } from './constants';

/**
* ISplitsCacheSync implementation that stores split definitions in browser LocalStorage.
Expand Down Expand Up @@ -52,7 +52,7 @@ export default class SplitsCacheInLocal extends AbstractSplitsCacheSync {
}
}
} catch (e) {
this.log.error(logPrefix + e);
this.log.error(LOG_PREFIX + e);
}
}

Expand All @@ -72,7 +72,7 @@ export default class SplitsCacheInLocal extends AbstractSplitsCacheSync {
}
}
} catch (e) {
this.log.error(logPrefix + e);
this.log.error(LOG_PREFIX + e);
}
}

Expand All @@ -82,7 +82,7 @@ export default class SplitsCacheInLocal extends AbstractSplitsCacheSync {
* We cannot simply call `localStorage.clear()` since that implies removing user items from the storage.
*/
clear() {
this.log.info(logPrefix + 'Flushing Splits data from localStorage');
this.log.info(LOG_PREFIX + 'Flushing Splits data from localStorage');

// collect item keys
const len = localStorage.length;
Expand Down Expand Up @@ -114,7 +114,7 @@ export default class SplitsCacheInLocal extends AbstractSplitsCacheSync {

return true;
} catch (e) {
this.log.error(logPrefix + e);
this.log.error(LOG_PREFIX + e);
return false;
}
}
Expand All @@ -129,7 +129,7 @@ export default class SplitsCacheInLocal extends AbstractSplitsCacheSync {

return true;
} catch (e) {
this.log.error(logPrefix + e);
this.log.error(LOG_PREFIX + e);
return false;
}
}
Expand All @@ -147,14 +147,14 @@ export default class SplitsCacheInLocal extends AbstractSplitsCacheSync {

// when using a new split query, we must update it at the store
if (this.updateNewFilter) {
this.log.info(logPrefix + 'Split filter query was modified. Updating cache.');
this.log.info(LOG_PREFIX + 'Split filter query was modified. Updating cache.');
const queryKey = this.keys.buildSplitsFilterQueryKey();
const queryString = this.splitFiltersValidation.queryString;
try {
if (queryString) localStorage.setItem(queryKey, queryString);
else localStorage.removeItem(queryKey);
} catch (e) {
this.log.error(logPrefix + e);
this.log.error(LOG_PREFIX + e);
}
this.updateNewFilter = false;
}
Expand All @@ -166,7 +166,7 @@ export default class SplitsCacheInLocal extends AbstractSplitsCacheSync {
this.hasSync = true;
return true;
} catch (e) {
this.log.error(logPrefix + e);
this.log.error(LOG_PREFIX + e);
return false;
}
}
Expand Down Expand Up @@ -273,7 +273,7 @@ export default class SplitsCacheInLocal extends AbstractSplitsCacheSync {
});
}
} catch (e) {
this.log.error(logPrefix + e);
this.log.error(LOG_PREFIX + e);
}
}
// if the filter didn't change, nothing is done
Expand Down
2 changes: 1 addition & 1 deletion src/storages/inLocalStorage/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const logPrefix = 'storage:localstorage: ';
export const LOG_PREFIX = 'storage:localstorage: ';
export const DEFINED = '1';
4 changes: 2 additions & 2 deletions src/storages/inLocalStorage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import MySegmentsCacheInMemory from '../inMemory/MySegmentsCacheInMemory';
import SplitsCacheInMemory from '../inMemory/SplitsCacheInMemory';
import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../../utils/constants/browser';
import { InMemoryStorageCSFactory } from '../inMemory/InMemoryStorageCS';
import { logPrefix } from './constants';
import { LOG_PREFIX } from './constants';

export interface InLocalStorageOptions {
prefix?: string
Expand All @@ -27,7 +27,7 @@ export function InLocalStorage(options: InLocalStorageOptions = {}) {

// Fallback to InMemoryStorage if LocalStorage API is not available
if (!isLocalStorageAvailable()) {
params.log.warn(logPrefix + 'LocalStorage API is unavailable. Fallbacking into default MEMORY storage');
params.log.warn(LOG_PREFIX + 'LocalStorage API is unavailable. Fallbacking into default MEMORY storage');
return InMemoryStorageCSFactory(params);
}

Expand Down
5 changes: 2 additions & 3 deletions src/storages/inRedis/EventsCacheInRedis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import KeyBuilderSS from '../KeyBuilderSS';
import { Redis } from 'ioredis';
import { SplitIO } from '../../types';
import { ILogger } from '../../logger/types';

const logPrefix = 'storage:redis: ';
import { LOG_PREFIX } from './constants';

export default class EventsCacheInRedis implements IEventsCacheAsync {

Expand Down Expand Up @@ -33,7 +32,7 @@ export default class EventsCacheInRedis implements IEventsCacheAsync {
// We use boolean values to signal successful queueing
.then(() => true)
.catch(err => {
this.log.error(logPrefix + `Error adding event to queue: ${err}.`);
this.log.error(LOG_PREFIX + `Error adding event to queue: ${err}.`);
return false;
});
}
Expand Down
20 changes: 10 additions & 10 deletions src/storages/inRedis/RedisAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { _Set, setToArray, ISet } from '../../utils/lang/sets';
import thenable from '../../utils/promise/thenable';
import timeout from '../../utils/promise/timeout';

const logPrefix = 'storage:redis-adapter: ';
const LOG_PREFIX = 'storage:redis-adapter: ';

// If we ever decide to fully wrap every method, there's a Commander.getBuiltinCommands from ioredis.
const METHODS_TO_PROMISE_WRAP = ['set', 'exec', 'del', 'get', 'keys', 'sadd', 'srem', 'sismember', 'smembers', 'incr', 'rpush', 'pipeline', 'expire', 'mget'];
Expand Down Expand Up @@ -55,16 +55,16 @@ export default class RedisAdapter extends ioredis {
_listenToEvents() {
this.once('ready', () => {
const commandsCount = this._notReadyCommandsQueue ? this._notReadyCommandsQueue.length : 0;
this.log.info(logPrefix + `Redis connection established. Queued commands: ${commandsCount}.`);
this.log.info(LOG_PREFIX + `Redis connection established. Queued commands: ${commandsCount}.`);
this._notReadyCommandsQueue && this._notReadyCommandsQueue.forEach(queued => {
this.log.info(logPrefix + `Executing queued ${queued.name} command.`);
this.log.info(LOG_PREFIX + `Executing queued ${queued.name} command.`);
queued.command().then(queued.resolve).catch(queued.reject);
});
// After the SDK is ready for the first time we'll stop queueing commands. This is just so we can keep handling BUR for them.
this._notReadyCommandsQueue = undefined;
});
this.once('close', () => {
this.log.info(logPrefix + 'Redis connection closed.');
this.log.info(LOG_PREFIX + 'Redis connection closed.');
});
}

Expand All @@ -78,7 +78,7 @@ export default class RedisAdapter extends ioredis {
const params = arguments;

function commandWrapper() {
instance.log.debug(logPrefix + `Executing ${method}.`);
instance.log.debug(LOG_PREFIX + `Executing ${method}.`);
// Return original method
const result = originalMethod.apply(instance, params);

Expand All @@ -93,7 +93,7 @@ export default class RedisAdapter extends ioredis {
result.then(cleanUpRunningCommandsCb, cleanUpRunningCommandsCb);

return timeout(instance._options.operationTimeout, result).catch(err => {
instance.log.error(logPrefix + `${method} operation threw an error or exceeded configured timeout of ${instance._options.operationTimeout}ms. Message: ${err}`);
instance.log.error(LOG_PREFIX + `${method} operation threw an error or exceeded configured timeout of ${instance._options.operationTimeout}ms. Message: ${err}`);
// Handling is not the adapter responsibility.
throw err;
});
Expand Down Expand Up @@ -126,19 +126,19 @@ export default class RedisAdapter extends ioredis {

setTimeout(function deferedDisconnect() {
if (instance._runningCommands.size > 0) {
instance.log.info(logPrefix + `Attempting to disconnect but there are ${instance._runningCommands.size} commands still waiting for resolution. Defering disconnection until those finish.`);
instance.log.info(LOG_PREFIX + `Attempting to disconnect but there are ${instance._runningCommands.size} commands still waiting for resolution. Defering disconnection until those finish.`);

Promise.all(setToArray(instance._runningCommands))
.then(() => {
instance.log.debug(logPrefix + 'Pending commands finished successfully, disconnecting.');
instance.log.debug(LOG_PREFIX + 'Pending commands finished successfully, disconnecting.');
originalMethod.apply(instance, params);
})
.catch(e => {
instance.log.warn(logPrefix + `Pending commands finished with error: ${e}. Proceeding with disconnection.`);
instance.log.warn(LOG_PREFIX + `Pending commands finished with error: ${e}. Proceeding with disconnection.`);
originalMethod.apply(instance, params);
});
} else {
instance.log.debug(logPrefix + 'No commands pending execution, disconnect.');
instance.log.debug(LOG_PREFIX + 'No commands pending execution, disconnect.');
// Nothing pending, just proceed.
originalMethod.apply(instance, params);
}
Expand Down
20 changes: 9 additions & 11 deletions src/storages/inRedis/SplitsCacheInRedis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import KeyBuilderSS from '../KeyBuilderSS';
import { ISplitsCacheAsync } from '../types';
import { Redis } from 'ioredis';
import { ILogger } from '../../logger/types';
import { SplitError } from '../../utils/lang/errors';

const logPrefix = 'storage:redis: ';
import { LOG_PREFIX } from './constants';

/**
* Discard errors for an answer of multiple operations.
Expand Down Expand Up @@ -90,11 +88,11 @@ export default class SplitsCacheInRedis implements ISplitsCacheAsync {

/**
* Get split definition or null if it's not defined.
* Returned promise is Rejected with an SplitError if redis operation fails.
* Returned promise is rejected if redis operation fails.
*/
getSplit(name: string): Promise<string | null> {
if (this.redisError) {
this.log.error(logPrefix + this.redisError);
this.log.error(LOG_PREFIX + this.redisError);

return Promise.reject(this.redisError); // no need to wrap as an SplitError
}
Expand Down Expand Up @@ -150,14 +148,14 @@ export default class SplitsCacheInRedis implements ISplitsCacheAsync {

ttCount = parseInt(ttCount as string, 10);
if (!isFiniteNumber(ttCount) || ttCount < 0) {
this.log.info(logPrefix + `Could not validate traffic type existence of ${trafficType} due to data corruption of some sorts.`);
this.log.info(LOG_PREFIX + `Could not validate traffic type existance of ${trafficType} due to data corruption of some sorts.`);
return false;
}

return ttCount > 0;
})
.catch(e => {
this.log.error(logPrefix + `Could not validate traffic type existence of ${trafficType} due to an error: ${e}.`);
this.log.error(LOG_PREFIX + `Could not validate traffic type existance of ${trafficType} due to an error: ${e}.`);
// If there is an error, bypass the validation so the event can get tracked.
return true;
});
Expand All @@ -179,11 +177,11 @@ export default class SplitsCacheInRedis implements ISplitsCacheAsync {

/**
* Fetches multiple splits definitions.
* Returned promise is Rejected with an SplitError if redis operation fails.
* Returned promise is rejected if redis operation fails.
*/
getSplits(names: string[]): Promise<Record<string, string | null>> {
if (this.redisError) {
this.log.error(logPrefix + this.redisError);
this.log.error(LOG_PREFIX + this.redisError);

return Promise.reject(this.redisError); // no need to wrap as an SplitError
}
Expand All @@ -198,8 +196,8 @@ export default class SplitsCacheInRedis implements ISplitsCacheAsync {
return Promise.resolve(splits);
})
.catch(e => {
this.log.error(logPrefix + `Could not grab splits due to an error: ${e}.`);
return Promise.reject(new SplitError(e));
this.log.error(LOG_PREFIX + `Could not grab splits due to an error: ${e}.`);
return Promise.reject(e);
});
}

Expand Down
Loading