UNPKG

enhanced-adot-node-autoinstrumentation

Version:

This package provides Amazon Web Services distribution of the OpenTelemetry Node Instrumentation, which allows for auto-instrumentation of NodeJS applications.

38 lines 1.71 kB
"use strict"; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.RateLimiter = void 0; /* * The RateLimiter keeps track of the current reservoir quota balance available (measured via available time) * If enough time has elapsed, the RateLimiter will allow quota balance to be consumed/taken (decrease available time) * A RateLimitingSampler uses this RateLimiter to determine if it should sample or not based on the quota balance available. */ class RateLimiter { constructor(quota, maxBalanceInSeconds = 1) { this.MAX_BALANCE_MILLIS = maxBalanceInSeconds * 1000.0; this.quota = quota; this.walletFloorMillis = Date.now(); // current "balance" would be `ceiling - floor` } take(cost = 1) { if (this.quota === 0) { return false; } const quotaPerMillis = this.quota / 1000.0; // assume divide by zero not possible const costInMillis = cost / quotaPerMillis; const walletCeilingMillis = Date.now(); let currentBalanceMillis = walletCeilingMillis - this.walletFloorMillis; currentBalanceMillis = Math.min(currentBalanceMillis, this.MAX_BALANCE_MILLIS); const pendingRemainingBalanceMillis = currentBalanceMillis - costInMillis; if (pendingRemainingBalanceMillis >= 0) { this.walletFloorMillis = walletCeilingMillis - pendingRemainingBalanceMillis; return true; } // No changes to the wallet state return false; } } exports.RateLimiter = RateLimiter; //# sourceMappingURL=rate-limiter.js.map