/** * Copyright (c) 2025 Databricks Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import IClientContext from '../contracts/IClientContext'; import IAuthentication from '../connection/contracts/IAuthentication'; export interface FeatureFlagContext { telemetryEnabled?: boolean; lastFetched?: Date; refCount: number; cacheDuration: number; } /** * Per-host feature-flag cache used to gate telemetry emission. Responsibilities: * - dedupe in-flight fetches (thundering-herd protection); * - ref-count so context goes away when the last consumer closes; * - clamp server-provided TTL into a safe band. * * Shares HTTP plumbing (agent, user agent) with DatabricksTelemetryExporter. * Consumer wiring lands in a later PR in this stack (see PR description). */ export default class FeatureFlagCache { private context; private authProvider?; private contexts; private fetchPromises; private readonly userAgent; private readonly CACHE_DURATION_MS; private readonly MIN_CACHE_DURATION_S; private readonly MAX_CACHE_DURATION_S; private readonly FEATURE_FLAG_NAME; constructor(context: IClientContext, authProvider?: IAuthentication | undefined); getOrCreateContext(host: string): FeatureFlagContext; releaseContext(host: string): void; isTelemetryEnabled(host: string): Promise; /** * Strips the `-oss` suffix the feature-flag API does not accept. The server * keys off the SemVer triplet only, so anything appended would 404. */ private getDriverVersion; private fetchFeatureFlag; /** * Retries transient network errors once before giving up. Without a retry * a single hiccup would leave telemetry disabled for the full cache TTL * (15 min). One retry gives an ephemeral DNS / connection-reset failure * a second chance without pushing sustained load at a broken endpoint. */ private fetchWithRetry; private getAuthHeaders; }