hamok
Version:
Lightweight Distributed Object Storage on RAFT consensus algorithm
97 lines (96 loc) • 3.66 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OngoingRequestsNotifier = void 0;
const Collections = __importStar(require("../common/Collections"));
const logger_1 = require("../common/logger");
const OngoingRequests_1 = require("./messagetypes/OngoingRequests");
const logger = (0, logger_1.createLogger)('OngoingRequests');
class OngoingRequestsNotifier {
timeoutInMs;
_sender;
_activeOngoingRequests = new Map();
_timer;
constructor(timeoutInMs, _sender) {
this.timeoutInMs = timeoutInMs;
this._sender = _sender;
}
get activeOngoingRequests() {
return this._activeOngoingRequests;
}
has(requestId) {
if (!requestId)
return false;
return this._activeOngoingRequests.has(requestId);
}
add(activeOngoingRequest) {
if (this._activeOngoingRequests.has(activeOngoingRequest.requestId)) {
return;
}
this._activeOngoingRequests.set(activeOngoingRequest.requestId, activeOngoingRequest);
logger.trace('Added ongoing request %o', activeOngoingRequest);
if (!this._timer) {
this._startTimer();
}
}
remove(requestId) {
if (!this._activeOngoingRequests.has(requestId)) {
return false;
}
this._activeOngoingRequests.delete(requestId);
logger.trace('Removed ongoing request %s', requestId);
if (this._activeOngoingRequests.size < 1) {
this._stopTimer();
}
return true;
}
clear() {
this._activeOngoingRequests.clear();
this._stopTimer();
}
_startTimer() {
if (this._timer) {
return logger.warn('Attempted to start a timer twice');
}
this._timer = setInterval(() => {
if (this._activeOngoingRequests.size < 1) {
return (this._timer = undefined);
}
const remotePeerActiveRequests = Collections.groupArrayBy([...this._activeOngoingRequests.values()], (item) => item.remotePeerId);
for (const [remotePeerId, ongoingRequests] of remotePeerActiveRequests) {
const notification = new OngoingRequests_1.OngoingRequestsNotification(new Set(ongoingRequests.map((item) => item.requestId)), remotePeerId);
this._sender(notification);
}
}, this.timeoutInMs);
}
_stopTimer() {
if (!this._timer) {
return;
}
clearInterval(this._timer);
this._timer = undefined;
}
}
exports.OngoingRequestsNotifier = OngoingRequestsNotifier;