Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/experiments/configcat-server.ts
2500 views
1
/**
2
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { Attributes, Client } from "./types";
8
import * as configcat from "configcat-node";
9
import { LogLevel } from "configcat-common";
10
import { ConfigCatClient } from "./configcat";
11
import { newAlwaysReturningDefaultValueClient } from "./always-default";
12
13
let client: Client | undefined;
14
15
export namespace Experiments {
16
export function configureTestingClient(config: Record<string, any>): void {
17
client = {
18
getValueAsync<T>(experimentName: string, defaultValue: T, attributes: Attributes): Promise<T> {
19
if (config.hasOwnProperty(experimentName)) {
20
return Promise.resolve(config[experimentName] as T);
21
}
22
return Promise.resolve(defaultValue);
23
},
24
25
dispose(): void {
26
// there is nothing to dispose, no-op.
27
},
28
};
29
}
30
}
31
32
export function getExperimentsClientForBackend(): Client {
33
// We have already instantiated a client, we can just re-use it.
34
if (client !== undefined) {
35
return client;
36
}
37
38
// Retrieve SDK key from ENV Variable
39
const sdkKey = process.env.CONFIGCAT_SDK_KEY;
40
41
// Self-hosted installations do not set the ConfigCat SDK key, so always use a client which returns the default value.
42
if (sdkKey === undefined || sdkKey === "") {
43
client = newAlwaysReturningDefaultValueClient();
44
return client;
45
}
46
47
const configCatClient = configcat.createClient(sdkKey, {
48
pollIntervalSeconds: 3 * 60, // 3 minutes
49
requestTimeoutMs: 2000,
50
logger: configcat.createConsoleLogger(LogLevel.Error),
51
maxInitWaitTimeSeconds: 0,
52
baseUrl: process.env.CONFIGCAT_BASE_URL,
53
});
54
55
client = new ConfigCatClient(configCatClient, process.env.HOST_URL);
56
return client;
57
}
58
59