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
2 changes: 1 addition & 1 deletion packages/sdk/react-native/example/src/welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function Welcome() {

const onIdentify = () => {
ldc
.identify({ kind: 'user', key: userKey })
.identify({ kind: 'user', key: userKey }, { timeout: 5 })
.catch((e: any) => console.error(`error identifying ${userKey}: ${e}`));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('ReactNativeLDClient', () => {
});

test('constructing a new client', () => {
expect(ldc.highTimeoutThreshold).toEqual(15);
expect(ldc.sdkKey).toEqual('mobile-key');
expect(ldc.config.serviceEndpoints).toEqual({
analyticsEventPath: '/mobile',
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/react-native/src/ReactNativeLDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default class ReactNativeLDClient extends LDClientImpl {
const internalOptions: internal.LDInternalOptions = {
analyticsEventPath: `/mobile`,
diagnosticEventPath: `/mobile/events/diagnostic`,
highTimeoutThreshold: 15,
};

super(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ export type LDInternalOptions = {
analyticsEventPath?: string;
diagnosticEventPath?: string;
includeAuthorizationHeader?: boolean;

/**
* In seconds. Log a warning if identifyTimeout is greater than this value.
*
* Mobile - 15s.
* Browser - 5s.
* Server - 60s.
*/
highTimeoutThreshold?: number;
};
2 changes: 2 additions & 0 deletions packages/shared/common/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fastDeepEqual from './fast-deep-equal';
import { base64UrlEncode, defaultHeaders, httpErrorMessage, LDHeaders, shouldRetry } from './http';
import noop from './noop';
import sleep from './sleep';
import timedPromise from './timedPromise';
import { VoidFunction } from './VoidFunction';

export {
Expand All @@ -21,5 +22,6 @@ export {
shouldRetry,
secondsToMillis,
sleep,
timedPromise,
VoidFunction,
};
19 changes: 19 additions & 0 deletions packages/shared/common/src/utils/timedPromise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { LDLogger } from '../api';

/**
* Returns a promise which errors after t seconds.
*
* @param t Timeout in seconds.
* @param taskName Name of task being timed for logging and error reporting.
* @param logger {@link LDLogger} object.
*/
const timedPromise = (t: number, taskName: string, logger?: LDLogger) =>
new Promise<void>((_res, reject) => {
setTimeout(() => {
const e = `${taskName} timed out after ${t} seconds.`;
logger?.error(e);
reject(new Error(e));
}, t * 1000);
});

export default timedPromise;
3 changes: 2 additions & 1 deletion packages/shared/mocks/src/streamingProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const setupMockStreamingProcessor = (
putResponseJson: any = { data: { flags: {}, segments: {} } },
patchResponseJson?: any,
deleteResponseJson?: any,
errorTimeoutSeconds: number = 0,
) => {
MockStreamingProcessor.mockImplementation(
(
Expand All @@ -33,7 +34,7 @@ export const setupMockStreamingProcessor = (
message: 'test-error',
};
errorHandler(unauthorized);
});
}, errorTimeoutSeconds * 1000);
} else {
// execute put which will resolve the identify promise
setTimeout(() => listeners.get('put')?.processJson(putResponseJson));
Expand Down
148 changes: 148 additions & 0 deletions packages/shared/sdk-client/src/LDClientImpl.timeout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { AutoEnvAttributes, clone, LDContext } from '@launchdarkly/js-sdk-common';
import { basicPlatform, logger, setupMockStreamingProcessor } from '@launchdarkly/private-js-mocks';

import * as mockResponseJson from './evaluation/mockResponse.json';
import LDClientImpl from './LDClientImpl';
import { Flags } from './types';
import { toMulti } from './utils/addAutoEnv';

jest.mock('@launchdarkly/js-sdk-common', () => {
const actual = jest.requireActual('@launchdarkly/js-sdk-common');
const m = jest.requireActual('@launchdarkly/private-js-mocks');
return {
...actual,
...{
internal: {
...actual.internal,
StreamingProcessor: m.MockStreamingProcessor,
},
},
};
});

const testSdkKey = 'test-sdk-key';
const carContext: LDContext = { kind: 'car', key: 'test-car' };

let ldc: LDClientImpl;
let defaultPutResponse: Flags;

describe('sdk-client identify timeout', () => {
beforeAll(() => {
jest.useFakeTimers();
});

beforeEach(() => {
defaultPutResponse = clone<Flags>(mockResponseJson);

// simulate streamer error after a long timeout
setupMockStreamingProcessor(true, defaultPutResponse, undefined, undefined, 30);

ldc = new LDClientImpl(testSdkKey, AutoEnvAttributes.Enabled, basicPlatform, {
logger,
sendEvents: false,
});
jest
.spyOn(LDClientImpl.prototype as any, 'createStreamUriPath')
.mockReturnValue('/stream/path');
});

afterEach(() => {
jest.resetAllMocks();
});

// streamer is setup to error in beforeEach to cause a timeout
test('rejects with default timeout of 5s', async () => {
jest.advanceTimersByTimeAsync(ldc.identifyTimeout * 1000).then();
await expect(ldc.identify(carContext)).rejects.toThrow(/identify timed out/);
expect(logger.error).toHaveBeenCalledWith(expect.stringMatching(/identify timed out/));
});

// streamer is setup to error in beforeEach to cause a timeout
test('rejects with custom timeout', async () => {
const timeout = 15;
jest.advanceTimersByTimeAsync(timeout * 1000).then();

await expect(ldc.identify(carContext, { timeout })).rejects.toThrow(/identify timed out/);
});

test('resolves with default timeout', async () => {
setupMockStreamingProcessor(false, defaultPutResponse);
jest.advanceTimersByTimeAsync(ldc.identifyTimeout * 1000).then();

await expect(ldc.identify(carContext)).resolves.toBeUndefined();

expect(ldc.getContext()).toEqual(expect.objectContaining(toMulti(carContext)));
expect(ldc.allFlags()).toEqual({
'dev-test-flag': true,
'easter-i-tunes-special': false,
'easter-specials': 'no specials',
fdsafdsafdsafdsa: true,
'log-level': 'warn',
'moonshot-demo': true,
test1: 's1',
'this-is-a-test': true,
});
});

test('resolves with custom timeout', async () => {
const timeout = 15;
setupMockStreamingProcessor(false, defaultPutResponse);
jest.advanceTimersByTimeAsync(timeout).then();

await expect(ldc.identify(carContext, { timeout })).resolves.toBeUndefined();

expect(ldc.getContext()).toEqual(expect.objectContaining(toMulti(carContext)));
expect(ldc.allFlags()).toEqual({
'dev-test-flag': true,
'easter-i-tunes-special': false,
'easter-specials': 'no specials',
fdsafdsafdsafdsa: true,
'log-level': 'warn',
'moonshot-demo': true,
test1: 's1',
'this-is-a-test': true,
});
});

test('setting high timeout threshold with internalOptions', async () => {
const highTimeoutThreshold = 20;
setupMockStreamingProcessor(false, defaultPutResponse);
ldc = new LDClientImpl(
testSdkKey,
AutoEnvAttributes.Enabled,
basicPlatform,
{
logger,
sendEvents: false,
},
{ highTimeoutThreshold },
);
const customTimeout = 10;
jest.advanceTimersByTimeAsync(customTimeout * 1000).then();
await ldc.identify(carContext, { timeout: customTimeout });

expect(ldc.identifyTimeout).toEqual(10);
expect(logger.warn).not.toHaveBeenCalledWith(expect.stringMatching(/high timeout/));
});

test('warning when timeout is too high', async () => {
const highTimeout = 60;
setupMockStreamingProcessor(false, defaultPutResponse);
jest.advanceTimersByTimeAsync(highTimeout * 1000).then();

await ldc.identify(carContext, { timeout: highTimeout });

expect(logger.warn).toHaveBeenCalledWith(expect.stringMatching(/high timeout/));
});

test('safe timeout should not warn', async () => {
const { identifyTimeout } = ldc;
const safeTimeout = identifyTimeout;
setupMockStreamingProcessor(false, defaultPutResponse);
jest.advanceTimersByTimeAsync(identifyTimeout * 1000).then();

await ldc.identify(carContext, { timeout: safeTimeout });

expect(logger.warn).not.toHaveBeenCalledWith(expect.stringMatching(/high timeout/));
});
});
40 changes: 32 additions & 8 deletions packages/shared/sdk-client/src/LDClientImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import {
Platform,
ProcessStreamResponse,
EventName as StreamEventName,
timedPromise,
TypeValidators,
} from '@launchdarkly/js-sdk-common';

import { ConnectionMode, LDClient, type LDOptions } from './api';
import LDEmitter, { EventName } from './api/LDEmitter';
import { LDIdentifyOptions } from './api/LDIdentifyOptions';
import Configuration from './configuration';
import createDiagnosticsManager from './diagnostics/createDiagnosticsManager';
import createEventProcessor from './events/createEventProcessor';
Expand All @@ -34,9 +36,12 @@ export default class LDClientImpl implements LDClient {
context?: LDContext;
diagnosticsManager?: internal.DiagnosticsManager;
eventProcessor?: internal.EventProcessor;
identifyTimeout: number = 5;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Harcoded to 5s by default. This will be set in identify if provided.

logger: LDLogger;
streamer?: internal.StreamingProcessor;

readonly highTimeoutThreshold: number = 15;

private eventFactoryDefault = new EventFactory(false);
private eventFactoryWithReasons = new EventFactory(true);
private emitter: LDEmitter;
Expand Down Expand Up @@ -100,7 +105,7 @@ export default class LDClientImpl implements LDClient {

if (this.context) {
// identify will start streamer
return this.identify(this.context);
return this.identify(this.context, { timeout: this.identifyTimeout });
}
break;
default:
Expand Down Expand Up @@ -233,13 +238,13 @@ export default class LDClientImpl implements LDClient {
*/
protected createStreamUriPath(_context: LDContext): string {
throw new Error(
'createStreamUriPath not implemented. Client sdks must implement createStreamUriPath for streamer to work',
'createStreamUriPath not implemented. Client sdks must implement createStreamUriPath for streamer to work.',
);
}

private createIdentifyPromise() {
private createIdentifyPromise(timeout: number) {
let res: any;
const p = new Promise<void>((resolve, reject) => {
const slow = new Promise<void>((resolve, reject) => {
res = resolve;

if (this.identifyChangeListener) {
Expand All @@ -252,7 +257,7 @@ export default class LDClientImpl implements LDClient {
this.identifyChangeListener = (c: LDContext, changedKeys: string[]) => {
this.logger.debug(`change: context: ${JSON.stringify(c)}, flags: ${changedKeys}`);
};
this.identifyErrorListener = (c: LDContext, err: any) => {
this.identifyErrorListener = (c: LDContext, err: Error) => {
this.logger.debug(`error: ${err}, context: ${JSON.stringify(c)}`);
reject(err);
};
Expand All @@ -261,15 +266,34 @@ export default class LDClientImpl implements LDClient {
this.emitter.on('error', this.identifyErrorListener);
});

return { identifyPromise: p, identifyResolve: res };
const timed = timedPromise(timeout, 'identify', this.logger);
const raced = Promise.race([timed, slow]);

return { identifyPromise: raced, identifyResolve: res };
}

private async getFlagsFromStorage(canonicalKey: string): Promise<Flags | undefined> {
const f = await this.platform.storage?.get(canonicalKey);
return f ? JSON.parse(f) : undefined;
}

async identify(pristineContext: LDContext): Promise<void> {
/**
* Identifies a context to LaunchDarkly. See {@link LDClient.identify}.
*
* @param pristineContext The LDContext object to be identified.
* @param identifyOptions Optional configuration. See {@link LDIdentifyOptions}.
*
* @returns {Promise<void>}.
*/
async identify(pristineContext: LDContext, identifyOptions?: LDIdentifyOptions): Promise<void> {
if (identifyOptions?.timeout) {
this.identifyTimeout = identifyOptions.timeout;
}

if (this.identifyTimeout > this.highTimeoutThreshold) {
this.logger.warn('identify called with high timeout parameter.');
}
Comment on lines +288 to +295
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shout: This is the core change.


let context = await ensureKey(pristineContext, this.platform);

if (this.autoEnvAttributes === AutoEnvAttributes.Enabled) {
Expand All @@ -285,7 +309,7 @@ export default class LDClientImpl implements LDClient {
}

this.eventProcessor?.sendEvent(this.eventFactoryDefault.identifyEvent(checkedContext));
const { identifyPromise, identifyResolve } = this.createIdentifyPromise();
const { identifyPromise, identifyResolve } = this.createIdentifyPromise(this.identifyTimeout);
this.logger.debug(`Identifying ${JSON.stringify(context)}`);

const flagsStorage = await this.getFlagsFromStorage(checkedContext.canonicalKey);
Expand Down
7 changes: 5 additions & 2 deletions packages/shared/sdk-client/src/api/LDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@launchdarkly/js-sdk-common';

import ConnectionMode from './ConnectionMode';
import { LDIdentifyOptions } from './LDIdentifyOptions';

/**
* The basic interface for the LaunchDarkly client. Platform-specific SDKs may add some methods of their own.
Expand Down Expand Up @@ -107,11 +108,13 @@ export interface LDClient {
* await the Promise to determine when the new flag values are available.
*
* @param context
* The LDContext object.
* The LDContext object.
* @param identifyOptions
* Optional configuration. Please see {@link LDIdentifyOptions}.
* @returns
* A Promise which resolves when the flag values for the specified context are available.
*/
identify(context: LDContext): Promise<void>;
identify(context: LDContext, identifyOptions?: LDIdentifyOptions): Promise<void>;

/**
* Determines the json variation of a feature flag.
Expand Down
11 changes: 11 additions & 0 deletions packages/shared/sdk-client/src/api/LDIdentifyOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface LDIdentifyOptions {
/**
* In seconds. Determines when the identify promise resolves if no flags have been
* returned from the network. If you use a large timeout and await it, then
* any network delays will cause your application to wait a long time before
* continuing execution.
*
* Defaults to 5 seconds.
*/
timeout: number;
}