Skip to content

Commit

Permalink
removed extraneous configs
Browse files Browse the repository at this point in the history
  • Loading branch information
zhukaihan committed Mar 14, 2024
1 parent bdef24b commit 332a8df
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 54 deletions.
14 changes: 9 additions & 5 deletions packages/node/src/local/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ import { FlagConfigPoller } from './poller';
import { FlagConfigStreamer } from './streamer';
import { FlagConfigUpdater } from './updater';

const STREAM_RETRY_DELAY_MILLIS = 15000; // The base delay to retry stream after fallback to poller.
const STREAM_RETRY_JITTER_MAX_MILLIS = 2000; // The jitter to add to delay after fallbacked to poller.
const STREAM_ATTEMPTS = 1; // Number of attempts before fallback to poller.
const STREAM_TRY_DELAY_MILLIS = 1000; // The delay between attempts.

/**
* Experiment client for evaluating variants for a user locally.
* @category Core Usage
Expand Down Expand Up @@ -80,12 +85,11 @@ export class LocalEvaluationClient {
this.cache,
streamEventSourceFactory,
this.config.flagConfigPollingIntervalMillis,
this.config.streamConnTimeoutMillis,
this.config.streamFlagConnTimeoutMillis,
this.config.streamFlagTryAttempts,
this.config.streamFlagTryDelayMillis,
this.config.streamFlagRetryDelayMillis +
Math.floor(Math.random() * this.config.streamFlagRetryJitterMillis),
STREAM_ATTEMPTS,
STREAM_TRY_DELAY_MILLIS,
STREAM_RETRY_DELAY_MILLIS +
Math.floor(Math.random() * STREAM_RETRY_JITTER_MAX_MILLIS),
this.config.streamServerUrl,
this.config.debug,
)
Expand Down
8 changes: 4 additions & 4 deletions packages/node/src/local/stream-flag-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class SdkStreamFlagApi implements StreamFlagApi {

// Do try up to 2 times. If any of error is fatal, stop any further tries.
// If trials times reached, fatal error.
public async connect(options?: StreamFlagOptions) {
public async connect(options?: StreamFlagOptions): Promise<void> {
// Makes sure there is no other connect running.
if (!this.isClosedAndNotTrying) {
return;
Expand Down Expand Up @@ -187,17 +187,17 @@ export class SdkStreamFlagApi implements StreamFlagApi {
}

// Close stream.
public close() {
public close(): void {
this.closeForRetry();
this.isClosedAndNotTrying = true;
}

// Close stream, but we know there will be another try happening very soon.
public closeForRetry() {
private closeForRetry(): void {
this.api.close();
}

get isClosed() {
get isClosed(): boolean {
return this.isClosedAndNotTrying;
}

Expand Down
9 changes: 4 additions & 5 deletions packages/node/src/local/streamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ export class FlagConfigStreamer implements FlagConfigUpdater {
cache: FlagConfigCache,
streamEventSourceFactory: StreamEventSourceFactory,
pollingIntervalMillis = LocalEvaluationDefaults.flagConfigPollingIntervalMillis,
streamConnTimeoutMillis = LocalEvaluationDefaults.streamConnTimeoutMillis,
streamFlagConnTimeoutMillis = LocalEvaluationDefaults.streamFlagConnTimeoutMillis,
streamFlagTryAttempts = LocalEvaluationDefaults.streamFlagTryAttempts,
streamFlagTryDelayMillis = LocalEvaluationDefaults.streamFlagTryDelayMillis,
streamFlagRetryDelayMillis = LocalEvaluationDefaults.streamFlagRetryDelayMillis,
streamFlagTryAttempts: number,
streamFlagTryDelayMillis: number,
streamFlagRetryDelayMillis: number,
serverUrl: string = LocalEvaluationDefaults.serverUrl,
debug = false,
) {
Expand All @@ -51,7 +50,7 @@ export class FlagConfigStreamer implements FlagConfigUpdater {
apiKey,
serverUrl,
streamEventSourceFactory,
streamConnTimeoutMillis,
streamFlagConnTimeoutMillis,
streamFlagConnTimeoutMillis,
streamFlagTryAttempts,
streamFlagTryDelayMillis,
Expand Down
40 changes: 2 additions & 38 deletions packages/node/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,44 +162,13 @@ export type LocalEvaluationConfig = {
*/
streamServerUrl?: string;

/**
* To use with streaming. The timeout for connecting an server-side event
* stream. Aka, the timeout for http connection.
*/
streamConnTimeoutMillis?: number;

/**
* To use with streaming. The timeout for a single attempt of establishing
* a valid stream of flag configs.
* This includes streamConnTimeoutMillis and time for receiving initial
* The time starts at making request and ends when received the initial
* flag configs.
*/
streamFlagConnTimeoutMillis?: number;

/**
* To use with streaming. The number attempts to connect before declaring
* streaming fatal error.
*/
streamFlagTryAttempts?: number;

/**
* To use with streaming. The delay between attempts to connect.
*/
streamFlagTryDelayMillis?: number;

/**
* To use with streaming. The base delay to retry streaming after stream
* fatal error and fallbacked to poller.
*/
streamFlagRetryDelayMillis?: number;

/**
* To use with streaming. The jitter to add to the delay for retry streaming
* after stream fatal error and fallbacked to poller.
* A random number between 0 and streamFlagRetryJitterMillis will be added to
* streamFlagRetryDelayMillis as the delay.
*/
streamFlagRetryJitterMillis?: number;
};

export type AssignmentConfig = {
Expand Down Expand Up @@ -235,12 +204,7 @@ export const LocalEvaluationDefaults: LocalEvaluationConfig = {
httpAgent: null,
streamUpdates: false,
streamServerUrl: 'https://stream.lab.amplitude.com',
streamConnTimeoutMillis: 1000,
streamFlagConnTimeoutMillis: 1000,
streamFlagTryAttempts: 2,
streamFlagTryDelayMillis: 1000,
streamFlagRetryDelayMillis: 15000,
streamFlagRetryJitterMillis: 2000,
streamFlagConnTimeoutMillis: 1500,
};

export const AssignmentConfigDefaults: Omit<AssignmentConfig, 'apiKey'> = {
Expand Down
2 changes: 0 additions & 2 deletions packages/node/test/local/flagConfigStreamer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { getNewClient } from './util/mockStreamEventSource';

const getTestObjs = ({
pollingIntervalMillis = 1000,
streamConnTimeoutMillis = 1000,
streamFlagConnTimeoutMillis = 1000,
streamFlagTryAttempts = 2,
streamFlagTryDelayMillis = 1000,
Expand Down Expand Up @@ -42,7 +41,6 @@ const getTestObjs = ({
cache,
mockClient.clientFactory,
pollingIntervalMillis,
streamConnTimeoutMillis,
streamFlagConnTimeoutMillis,
streamFlagTryAttempts,
streamFlagTryDelayMillis,
Expand Down

0 comments on commit 332a8df

Please sign in to comment.