UNPKG

@forge-ml/rag

Version:

A RAG (Retrieval-Augmented Generation) package for Forge ML

84 lines (83 loc) 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DynamicThrottler = exports.Throttler = exports.Semaphore = void 0; class Semaphore { capacity; count; queue = []; constructor(capacity = 1) { this.capacity = capacity; this.count = 0; } async acquire() { this.count++; if (this.count <= this.capacity) { return; } const promise = new Promise((resolve, _reject) => { this.queue.push(resolve); }); return promise; } async release() { this.count--; const resolver = this.queue.shift(); if (resolver) { resolver(); } } } exports.Semaphore = Semaphore; class Throttler extends Semaphore { interval; constructor(capacity = 1) { super(capacity); this.interval = setInterval(this.releaseInterval.bind(this), 1000); } async execute(fn) { await this.acquire(); return fn(); } async releaseInterval() { Array.from({ length: this.capacity }).forEach(() => { this.release(); }); } destroy() { clearInterval(this.interval); } } exports.Throttler = Throttler; class DynamicThrottler extends Semaphore { interval; valueProvider; limit; constructor(valueProvider, limit) { super(1); // Start with capacity 1, will be updated dynamically this.valueProvider = valueProvider; this.limit = limit; this.interval = setInterval(this.updateCapacity.bind(this), 1000); } updateCapacity() { const currentValue = this.valueProvider(); const newCapacity = Math.max(1, Math.floor(this.limit - currentValue)); this.capacity = newCapacity; // Release up to the new capacity for (let i = 0; i < newCapacity; i++) { this.release(); } } async execute(fn) { await this.acquire(); try { return await fn(); } finally { this.release(); } } destroy() { clearInterval(this.interval); } } exports.DynamicThrottler = DynamicThrottler;