vulcain-corejs
Version:
Vulcain micro-service framework
49 lines (47 loc) • 1.49 kB
JavaScript
class Semaphore {
constructor(maxExecution, maxFallback) {
this.maxExecution = maxExecution;
this.maxFallback = maxFallback;
this.currentExecution = 0;
this.currentFallback = 0;
}
canExecuteCommand() {
if (this.maxExecution !== 0 && this.currentExecution > this.maxExecution) {
return false;
}
this.currentExecution++;
return true;
}
releaseExecutionCommand() {
this.currentExecution--;
}
canExecuteFallback() {
if (this.maxFallback !== 0 && this.currentFallback > this.maxFallback) {
return false;
}
this.currentFallback++;
return true;
}
releaseFallback() {
this.currentFallback--;
}
}
exports.Semaphore = Semaphore;
class SemaphoreFactory {
static getOrCreate(info) {
let previouslyCached = SemaphoreFactory.semaphores.get(info.commandName);
if (previouslyCached) {
return previouslyCached;
}
let semaphore = new Semaphore(info.executionIsolationSemaphoreMaxConcurrentRequests.value, info.fallbackIsolationSemaphoreMaxConcurrentRequests.value);
SemaphoreFactory.semaphores.set(info.commandName, semaphore);
return semaphore;
}
static resetCache() {
SemaphoreFactory.semaphores.clear();
}
}
SemaphoreFactory.semaphores = new Map();
exports.SemaphoreFactory = SemaphoreFactory;
//# sourceMappingURL=semaphore.js.map
;