token-bucket-calculator
Version:
Token Bucket Calculator
100 lines • 4.39 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenBucketCalculator = void 0;
const getMSec_1 = __importDefault(require("./getMSec"));
const wait_1 = __importDefault(require("./wait"));
class TokenBucketCalculator {
constructor({ bucketSize, fillAmount, fillIntervalUnit = 'sec', startBucketFirstFill = false, }) {
this.isRunningRemovingTokensTask = undefined;
this.bucketSize = bucketSize;
this.fillAmount = fillAmount;
this.fillIntervalMSec = this.calcFillIntervalMSec(fillIntervalUnit);
this.content = startBucketFirstFill ? this.fillIntervalMSec : 0;
this.lastFillMSec = (0, getMSec_1.default)();
}
calcFillIntervalMSec(fillIntervalUnit = 'ms') {
if (fillIntervalUnit === 'sec') {
return 1000;
}
else if (fillIntervalUnit === 'min') {
return 1000 * 60;
}
else if (fillIntervalUnit === 'hr') {
return 1000 * 60 * 60;
}
else if (fillIntervalUnit === 'day') {
return 1000 * 60 * 60 * 24;
}
else {
return 1;
}
}
changeFillAmount(fillAmount, fillIntervalUnit = 'ms') {
this.fillAmount = fillAmount;
this.fillIntervalMSec = this.calcFillIntervalMSec(fillIntervalUnit);
}
removeTokens(tokens) {
return __awaiter(this, void 0, void 0, function* () {
if (this.isRunningRemovingTokensTask === undefined) {
return (this.isRunningRemovingTokensTask = this.removeTokensTask(tokens));
}
else {
return (this.isRunningRemovingTokensTask =
this.isRunningRemovingTokensTask.then(() => this.removeTokensTask(tokens)));
}
});
}
removeTokensTask(tokens) {
return __awaiter(this, void 0, void 0, function* () {
const nowFillAmout = this.fillAmount;
const nowFillIntervalMSec = this.fillIntervalMSec;
this.fillTokens(nowFillAmout, nowFillIntervalMSec);
const waitTime = this.calcTimeForRemovingTokens(tokens);
if (waitTime !== 0) {
yield (0, wait_1.default)(waitTime);
this.fillTokens(nowFillAmout, nowFillIntervalMSec);
}
return this.subContent(tokens);
});
}
calcTimeForRemovingTokens(tokens) {
if (this.content < tokens) {
const willRemoveTokens = tokens - this.content;
const msecPerAmount = this.fillIntervalMSec / this.fillAmount;
return Math.ceil(willRemoveTokens * msecPerAmount);
}
else {
return 0;
}
}
fillTokens(fillAmount = this.fillAmount, fillIntervalMSec = this.fillIntervalMSec) {
const nowMSec = (0, getMSec_1.default)();
const timeForAfterFillingMSec = nowMSec - this.lastFillMSec;
this.setLastFillMSec(nowMSec);
const tokenToFillAmount = timeForAfterFillingMSec * (fillAmount / fillIntervalMSec);
this.addContent(tokenToFillAmount);
}
setLastFillMSec(ms) {
this.lastFillMSec = ms;
}
addContent(tokens) {
return (this.content = Math.min(this.bucketSize, this.content + tokens));
}
subContent(tokens) {
return (this.content = Math.max(0, this.content - tokens));
}
}
exports.TokenBucketCalculator = TokenBucketCalculator;
//# sourceMappingURL=index.js.map