Skip to content

Refactor: add checkIfServerSide utility function #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/storages/inMemory/TelemetryCacheInMemory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ImpressionDataType, EventDataType, LastSync, HttpErrors, HttpLatencies, StreamingEvent, Method, OperationType, MethodExceptions, MethodLatencies, TelemetryUsageStatsPayload, UpdatesFromSSEEnum, UpdatesFromSSE } from '../../sync/submitters/types';
import { DEDUPED, DROPPED, LOCALHOST_MODE, QUEUED } from '../../utils/constants';
import { checkIfServerSide } from '../../utils/key';
import { findLatencyIndex } from '../findLatencyIndex';
import { ISegmentsCacheSync, ISplitsCacheSync, IStorageFactoryParams, ITelemetryCacheSync } from '../types';

Expand All @@ -20,7 +21,7 @@ const ACCEPTANCE_RANGE = 0.001;
* All factory instances track telemetry on server-side, and 0.1% on client-side.
*/
export function shouldRecordTelemetry({ settings }: IStorageFactoryParams) {
return settings.mode !== LOCALHOST_MODE && (settings.core.key === undefined || Math.random() <= ACCEPTANCE_RANGE);
return settings.mode !== LOCALHOST_MODE && (checkIfServerSide(settings) || Math.random() <= ACCEPTANCE_RANGE);
}

export class TelemetryCacheInMemory implements ITelemetryCacheSync {
Expand Down
3 changes: 2 additions & 1 deletion src/storages/pluggable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { UniqueKeysCacheInMemoryCS } from '../inMemory/UniqueKeysCacheInMemoryCS
import { metadataBuilder } from '../utils';
import { LOG_PREFIX } from '../pluggable/constants';
import { RBSegmentsCachePluggable } from './RBSegmentsCachePluggable';
import { checkIfServerSide } from '../../utils/key';

const NO_VALID_WRAPPER = 'Expecting pluggable storage `wrapper` in options, but no valid wrapper instance was provided.';
const NO_VALID_WRAPPER_INTERFACE = 'The provided wrapper instance doesn’t follow the expected interface. Check our docs.';
Expand Down Expand Up @@ -83,7 +84,7 @@ export function PluggableStorage(options: PluggableStorageOptions): IStorageAsyn
new ImpressionCountsCachePluggable(log, keys.buildImpressionsCountKey(), wrapper);

const uniqueKeysCache = isPartialConsumer ?
settings.core.key === undefined ? new UniqueKeysCacheInMemory() : new UniqueKeysCacheInMemoryCS() :
checkIfServerSide(settings) ? new UniqueKeysCacheInMemory() : new UniqueKeysCacheInMemoryCS() :
new UniqueKeysCachePluggable(log, keys.buildUniqueKeysKey(), wrapper);

// Connects to wrapper and emits SDK_READY event on main client
Expand Down
3 changes: 2 additions & 1 deletion src/sync/polling/fetchers/splitChangesFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { FLAG_SPEC_VERSION } from '../../../utils/constants';
import { base } from '../../../utils/settingsValidation';
import { ISplitChangesFetcher } from './types';
import { LOG_PREFIX_SYNC_SPLITS } from '../../../logger/constants';
import { checkIfServerSide } from '../../../utils/key';

const PROXY_CHECK_INTERVAL_MILLIS_CS = 60 * 60 * 1000; // 1 hour in Client Side
const PROXY_CHECK_INTERVAL_MILLIS_SS = 24 * PROXY_CHECK_INTERVAL_MILLIS_CS; // 24 hours in Server Side
Expand All @@ -22,7 +23,7 @@ function sdkEndpointOverriden(settings: ISettings) {
export function splitChangesFetcherFactory(fetchSplitChanges: IFetchSplitChanges, settings: ISettings, storage: Pick<IStorageBase, 'splits' | 'rbSegments'>): ISplitChangesFetcher {

const log = settings.log;
const PROXY_CHECK_INTERVAL_MILLIS = settings.core.key !== undefined ? PROXY_CHECK_INTERVAL_MILLIS_CS : PROXY_CHECK_INTERVAL_MILLIS_SS;
const PROXY_CHECK_INTERVAL_MILLIS = checkIfServerSide(settings) ? PROXY_CHECK_INTERVAL_MILLIS_SS : PROXY_CHECK_INTERVAL_MILLIS_CS;
let lastProxyCheckTimestamp: number | undefined;

return function splitChangesFetcher(
Expand Down
3 changes: 2 additions & 1 deletion src/sync/streaming/SSEClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IPlatform } from '../../../sdkFactory/types';
import { decorateHeaders } from '../../../services/decorateHeaders';
import { IEventSourceConstructor } from '../../../services/types';
import { ISettings } from '../../../types';
import { checkIfServerSide } from '../../../utils/key';
import { isString } from '../../../utils/lang';
import { objectAssign } from '../../../utils/lang/objectAssign';
import { IAuthTokenPushEnabled } from '../AuthClient/types';
Expand Down Expand Up @@ -73,7 +74,7 @@ export class SSEClient implements ISSEClient {
return encodeURIComponent(params + channel);
}).join(',');
const url = `${this.settings.urls.streaming}/sse?channels=${channelsQueryParam}&accessToken=${authToken.token}&v=${ABLY_API_VERSION}&heartbeats=true`; // same results using `&heartbeats=false`
const isServerSide = !this.settings.core.key;
const isServerSide = checkIfServerSide(this.settings);

this.connection = new this.eventSource!(
// For client-side SDKs, metadata is passed as query param to avoid CORS issues and because native EventSource implementations in browsers do not support headers
Expand Down
4 changes: 2 additions & 2 deletions src/sync/streaming/pushManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SplitsUpdateWorker } from './UpdateWorkers/SplitsUpdateWorker';
import { authenticateFactory, hashUserKey } from './AuthClient';
import { forOwn } from '../../utils/lang';
import { SSEClient } from './SSEClient';
import { getMatching } from '../../utils/key';
import { checkIfServerSide, getMatching } from '../../utils/key';
import { MEMBERSHIPS_MS_UPDATE, MEMBERSHIPS_LS_UPDATE, PUSH_NONRETRYABLE_ERROR, PUSH_SUBSYSTEM_DOWN, SECONDS_BEFORE_EXPIRATION, SEGMENT_UPDATE, SPLIT_KILL, SPLIT_UPDATE, RB_SEGMENT_UPDATE, PUSH_RETRYABLE_ERROR, PUSH_SUBSYSTEM_UP, ControlType } from './constants';
import { STREAMING_FALLBACK, STREAMING_REFRESH_TOKEN, STREAMING_CONNECTING, STREAMING_DISABLED, ERROR_STREAMING_AUTH, STREAMING_DISCONNECTING, STREAMING_RECONNECT, STREAMING_PARSING_MEMBERSHIPS_UPDATE } from '../../logger/constants';
import { IMembershipMSUpdateData, IMembershipLSUpdateData, KeyList, UpdateStrategy } from './SSEHandler/types';
Expand All @@ -34,7 +34,7 @@ export function pushManagerFactory(

// `userKey` is the matching key of main client in client-side SDK.
// It can be used to check if running on client-side or server-side SDK.
const userKey = settings.core.key ? getMatching(settings.core.key) : undefined;
const userKey = checkIfServerSide(settings) ? undefined : getMatching(settings.core.key);
const log = settings.log;

let sseClient: ISSEClient;
Expand Down
7 changes: 4 additions & 3 deletions src/sync/submitters/telemetrySubmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { timer } from '../../utils/timeTracker/timer';
import { ISdkFactoryContextSync } from '../../sdkFactory/types';
import { objectAssign } from '../../utils/lang/objectAssign';
import { ISplitFiltersValidation } from '../../dtos/types';
import { checkIfServerSide } from '../../utils/key';

const OPERATION_MODE_MAP = {
[STANDALONE_MODE]: STANDALONE_ENUM,
Expand Down Expand Up @@ -71,16 +72,16 @@ export function telemetryCacheConfigAdapter(telemetry: ITelemetryCacheSync, sett

pop(): TelemetryConfigStatsPayload {
const { urls, scheduler } = settings;
const isClientSide = settings.core.key !== undefined;
const isServerSide = checkIfServerSide(settings);

const { flagSetsTotal, flagSetsIgnored } = getTelemetryFlagSetsStats(settings.sync.__splitFiltersValidation);

return objectAssign(getTelemetryConfigStats(settings.mode, settings.storage.type), {
sE: settings.streamingEnabled,
rR: {
sp: scheduler.featuresRefreshRate / 1000,
se: isClientSide ? undefined : scheduler.segmentsRefreshRate / 1000,
ms: isClientSide ? scheduler.segmentsRefreshRate / 1000 : undefined,
se: isServerSide ? scheduler.segmentsRefreshRate / 1000 : undefined,
ms: isServerSide ? undefined : scheduler.segmentsRefreshRate / 1000,
im: scheduler.impressionsRefreshRate / 1000,
ev: scheduler.eventsPushRate / 1000,
te: scheduler.telemetryRefreshRate / 1000,
Expand Down
5 changes: 5 additions & 0 deletions src/utils/key/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SplitIO from '../../../types/splitio';
import { ISettings } from '../../types';
import { isObject } from '../lang';

// function isSplitKeyObject(key: any): key is SplitIO.SplitKeyObject {
Expand Down Expand Up @@ -32,3 +33,7 @@ export function keyParser(key: SplitIO.SplitKey): SplitIO.SplitKeyObject {
};
}
}

export function checkIfServerSide(settings: ISettings) {
return !settings.core.key;
}