Path: blob/main/components/gitpod-protocol/src/experiments/configcat-server.ts
2500 views
/**1* Copyright (c) 2022 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { Attributes, Client } from "./types";7import * as configcat from "configcat-node";8import { LogLevel } from "configcat-common";9import { ConfigCatClient } from "./configcat";10import { newAlwaysReturningDefaultValueClient } from "./always-default";1112let client: Client | undefined;1314export namespace Experiments {15export function configureTestingClient(config: Record<string, any>): void {16client = {17getValueAsync<T>(experimentName: string, defaultValue: T, attributes: Attributes): Promise<T> {18if (config.hasOwnProperty(experimentName)) {19return Promise.resolve(config[experimentName] as T);20}21return Promise.resolve(defaultValue);22},2324dispose(): void {25// there is nothing to dispose, no-op.26},27};28}29}3031export function getExperimentsClientForBackend(): Client {32// We have already instantiated a client, we can just re-use it.33if (client !== undefined) {34return client;35}3637// Retrieve SDK key from ENV Variable38const sdkKey = process.env.CONFIGCAT_SDK_KEY;3940// Self-hosted installations do not set the ConfigCat SDK key, so always use a client which returns the default value.41if (sdkKey === undefined || sdkKey === "") {42client = newAlwaysReturningDefaultValueClient();43return client;44}4546const configCatClient = configcat.createClient(sdkKey, {47pollIntervalSeconds: 3 * 60, // 3 minutes48requestTimeoutMs: 2000,49logger: configcat.createConsoleLogger(LogLevel.Error),50maxInitWaitTimeSeconds: 0,51baseUrl: process.env.CONFIGCAT_BASE_URL,52});5354client = new ConfigCatClient(configCatClient, process.env.HOST_URL);55return client;56}575859