Skip to content

Commit 2d25fd5

Browse files
committed
update
1 parent 3755d24 commit 2d25fd5

8 files changed

+123
-26
lines changed

lib/project_config/config.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2022-2023, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const DEFAULT_UPDATE_INTERVAL_MINUTES = 5;
18+
/** Standard interval (5 minutes in milliseconds) for polling datafile updates.; */
19+
export const DEFAULT_UPDATE_INTERVAL = DEFAULT_UPDATE_INTERVAL_MINUTES * 60 * 1000;
20+
21+
const MIN_UPDATE_INTERVAL_SECONDS = 30;
22+
/** Minimum allowed interval (30 seconds in milliseconds) for polling datafile updates. */
23+
export const MIN_UPDATE_INTERVAL = MIN_UPDATE_INTERVAL_SECONDS * 1000;
24+
25+
export const UPDATE_INTERVAL_BELOW_MINIMUM_MESSAGE = `Polling intervals below ${MIN_UPDATE_INTERVAL_SECONDS} seconds are not recommended.`;
26+
27+
export const DEFAULT_URL_TEMPLATE = `https://cdn.optimizely.com/datafiles/%s.json`;
28+
29+
export const DEFAULT_AUTHENTICATED_URL_TEMPLATE = `https://config.optimizely.com/datafiles/auth/%s.json`;
30+
31+
export const BACKOFF_BASE_WAIT_SECONDS_BY_ERROR_COUNT = [0, 8, 16, 32, 64, 128, 256, 512];
32+
33+
export const REQUEST_TIMEOUT_MS = 60 * 1000; // 1 minute
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Transformer } from "../utils/type";
2+
import { ProjectConfigManagerImpl, ProjectConfigManager } from "./project_config_manager";
3+
import { DatafileManagerConfig } from "./datafileManager";
4+
import { PollingConfigManagerConfig, PollingConfigManagerFactoryOptions } from "./config_manager_factory";
5+
import { createPollingConfigManager as createPollingConfigManagerInternal } from "./config_manager_factory";
6+
import { BrowserRequestHandler } from "../utils/http_request_handler/browser_request_handler";
7+
8+
export const createPollingConfigManager = (config: PollingConfigManagerConfig): ProjectConfigManager => {
9+
const options: PollingConfigManagerFactoryOptions = {
10+
autoUpdate: false,
11+
requestHandler: new BrowserRequestHandler(),
12+
...config,
13+
}
14+
return createPollingConfigManagerInternal(options);
15+
}

lib/project_config/config_manager_factory.node.ts

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { HttpPollingDatafileManager } from "./httpPollingDatafileManager";
2+
import { ProjectConfigManagerImpl, ProjectConfigManager } from "./project_config_manager";
3+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { RequestHandler } from "../utils/http_request_handler/http";
2+
import { Transformer } from "../utils/type";
3+
import { DatafileManagerConfig } from "./datafileManager";
4+
import { ProjectConfigManagerImpl, ProjectConfigManager } from "./project_config_manager";
5+
import { HttpPollingDatafileManager } from "./httpPollingDatafileManager";
6+
7+
export const createStaticProjectConfigManager = (
8+
datafile: string,
9+
jsonSchemaValidator?: Transformer<unknown, boolean>
10+
): ProjectConfigManager => {
11+
const config = {
12+
datafile,
13+
jsonSchemaValidator,
14+
};
15+
return new ProjectConfigManagerImpl(config);
16+
};
17+
18+
export type PollingConfigManagerConfig = {
19+
datafile?: string,
20+
sdkKey: string,
21+
jsonSchemaValidator?: Transformer<unknown, boolean>,
22+
autoUpdate?: boolean;
23+
updateInterval?: number;
24+
urlTemplate?: string;
25+
datafileAccessToken?: string;
26+
};
27+
28+
export type PollingConfigManagerFactoryOptions = PollingConfigManagerConfig & { requestHandler: RequestHandler };
29+
30+
export const createPollingConfigManager = (
31+
opt: PollingConfigManagerFactoryOptions
32+
): ProjectConfigManager => {
33+
34+
const datafileManagerConfig: DatafileManagerConfig = {
35+
sdkKey: opt.sdkKey,
36+
autoUpdate: opt.autoUpdate,
37+
updateInterval: opt.updateInterval,
38+
urlTemplate: opt.urlTemplate,
39+
datafileAccessToken: opt.datafileAccessToken,
40+
requestHandler: opt.requestHandler,
41+
};
42+
43+
const datafileManager = new HttpPollingDatafileManager(datafileManagerConfig);
44+
45+
return new ProjectConfigManagerImpl({
46+
datafile: opt.datafile,
47+
datafileManager,
48+
jsonSchemaValidator: opt.jsonSchemaValidator,
49+
});
50+
};

lib/modules/datafile-manager/datafileManager.ts renamed to lib/project_config/datafileManager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { Service } from '../../service';
17-
import PersistentKeyValueCache from '../../plugins/key_value_cache/persistentKeyValueCache';
18-
import { RequestHandler } from '../../utils/http_request_handler/http';
19-
import { Fn, Consumer } from '../../utils/type';
16+
import { Service } from '../service';
17+
import PersistentKeyValueCache from '../plugins/key_value_cache/persistentKeyValueCache';
18+
import { RequestHandler } from '../utils/http_request_handler/http';
19+
import { Fn, Consumer } from '../utils/type';
2020

2121
export interface DatafileUpdate {
2222
datafile: string;

lib/modules/datafile-manager/httpPollingDatafileManager.ts renamed to lib/project_config/httpPollingDatafileManager.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { getLogger } from '../logging';
18-
import { sprintf } from '../../utils/fns';
19-
import { DatafileManager, DatafileManagerConfig, DatafileUpdate } from './datafileManager';
20-
import { EventEmitter } from '../../utils/event_emitter/eventEmitter';
17+
import { getLogger } from '../modules/logging';
18+
import { sprintf } from '../utils/fns';
19+
import { DatafileManager, DatafileManagerConfig } from './datafileManager';
20+
import { EventEmitter } from '../utils/event_emitter/eventEmitter';
2121
import { DEFAULT_UPDATE_INTERVAL, MIN_UPDATE_INTERVAL, DEFAULT_URL_TEMPLATE, UPDATE_INTERVAL_BELOW_MINIMUM_MESSAGE } from './config';
22-
import BackoffController from './backoffController';
23-
import PersistentKeyValueCache from '../../plugins/key_value_cache/persistentKeyValueCache';
24-
25-
import { NotificationRegistry } from './../../core/notification_center/notification_registry';
26-
import { NOTIFICATION_TYPES } from '../../utils/enums';
27-
import { ServiceState } from '../../service';
28-
import { RequestHandler, AbortableRequest, Headers, Response } from '../../utils/http_request_handler/http';
29-
import { resolvablePromise, ResolvablePromise } from '../../utils/promise/resolvablePromise';
30-
import { Ticker, ExponentialBackoff, IntervalTicker } from '../../utils/ticker/ticker';
31-
import { promises } from 'dns';
32-
import { Consumer, Fn } from '../../utils/type';
22+
import PersistentKeyValueCache from '../plugins/key_value_cache/persistentKeyValueCache';
23+
24+
import { ServiceState } from '../service';
25+
import { RequestHandler, AbortableRequest, Headers, Response } from '../utils/http_request_handler/http';
26+
import { resolvablePromise, ResolvablePromise } from '../utils/promise/resolvablePromise';
27+
import { Ticker, ExponentialBackoff, IntervalTicker } from '../utils/ticker/ticker';
28+
import { Consumer, Fn } from '../utils/type';
3329

3430
const logger = getLogger('DatafileManager');
3531

lib/utils/http_request_handler/browser_request_handler.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import { REQUEST_TIMEOUT_MS } from '../enums';
2222
* Handles sending requests and receiving responses over HTTP via XMLHttpRequest
2323
*/
2424
export class BrowserRequestHandler implements RequestHandler {
25-
private readonly logger: LogHandler;
26-
private readonly timeout: number;
25+
private logger?: LogHandler;
26+
private timeout: number;
2727

28-
public constructor(logger: LogHandler, timeout: number = REQUEST_TIMEOUT_MS) {
29-
this.logger = logger;
30-
this.timeout = timeout;
28+
public constructor(opt: { logger?: LogHandler, timeout?: number } = {}) {
29+
this.logger = opt.logger;
30+
this.timeout = opt.timeout ?? REQUEST_TIMEOUT_MS;
3131
}
3232

3333
/**
@@ -67,7 +67,7 @@ export class BrowserRequestHandler implements RequestHandler {
6767
request.timeout = this.timeout;
6868

6969
request.ontimeout = (): void => {
70-
this.logger.log(LogLevel.WARNING, 'Request timed out');
70+
this.logger?.log(LogLevel.WARNING, 'Request timed out');
7171
};
7272

7373
request.send(data);
@@ -122,7 +122,7 @@ export class BrowserRequestHandler implements RequestHandler {
122122
}
123123
}
124124
} catch {
125-
this.logger.log(LogLevel.WARNING, `Unable to parse & skipped header item '${headerLine}'`);
125+
this.logger?.log(LogLevel.WARNING, `Unable to parse & skipped header item '${headerLine}'`);
126126
}
127127
});
128128
return headers;

0 commit comments

Comments
 (0)