UNPKG

@duongtrungnguyen/nestro

Version:
143 lines 5.54 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __decorateClass = (decorators, target, key, kind) => { var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target; for (var i = decorators.length - 1, decorator; i >= 0; i--) if (decorator = decorators[i]) result = (kind ? decorator(target, key, result) : decorator(result)) || result; if (kind && result) __defProp(target, key, result); return result; }; var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index); var memory_storage_service_exports = {}; __export(memory_storage_service_exports, { MemoryStorage: () => MemoryStorage }); module.exports = __toCommonJS(memory_storage_service_exports); var import_common = require("@nestjs/common"); var import_common2 = require("../../common"); var import_constants = require("../constants"); let MemoryStorage = class { constructor(options) { this.options = options; this.memoryStore = /* @__PURE__ */ new Map(); } onModuleInit() { this.cleanupInterval = setInterval(() => this.cleanup(), this.options.cleanupTTL); (0, import_common2.debugLog)("RegistryService", "Using in-memory store"); } async register(key, instance) { const now = Date.now(); const instances = this.getOrCreateInstances(key); const instanceId = this.getInstanceId(instance); instances.set(instanceId, { ...instance, status: "ON", timestamp: now, lastHeartbeatAt: now, missedHeartbeats: 0 }); (0, import_common2.debugLog)("RegistryService", "Registered service in memory", { key, instanceId }); } async heartbeat(key, instance) { const now = Date.now(); const instances = this.getOrCreateInstances(key); const instanceId = this.getInstanceId(instance); const existingInstance = instances.get(instanceId); if (!existingInstance) { await this.register(key, instance); (0, import_common2.debugLog)("RegistryService", "Recovered missing instance via heartbeat", { key, instanceId }); return; } existingInstance.lastHeartbeatAt = now; existingInstance.missedHeartbeats = 0; existingInstance.status = "ON"; (0, import_common2.debugLog)("RegistryService", "Heartbeat received", { key, instanceId }); } async deregister(key, instance) { const instances = this.memoryStore.get(key); if (!instances) return; const instanceId = this.getInstanceId(instance); instances.delete(instanceId); if (instances.size === 0) { this.memoryStore.delete(key); } (0, import_common2.debugLog)("RegistryService", "Deregistered service from memory", { key, instanceId }); } async getServices(serviceName) { if (serviceName) { return { [serviceName]: Array.from(this.memoryStore.get(serviceName)?.values() || []) }; } const result = {}; this.memoryStore.forEach((instances, name) => { result[name] = Array.from(instances.values()); }); return result; } cleanup() { const now = Date.now(); const heartbeatThreshold = this.options.heartbeatInterval; for (const [key, instances] of this.memoryStore.entries()) { for (const [id, instance] of instances.entries()) { const missedDuration = now - instance.lastHeartbeatAt; if (missedDuration > heartbeatThreshold) { instance.missedHeartbeats += 1; if (instance.missedHeartbeats >= this.options.evictionThreshold) { instances.delete(id); (0, import_common2.debugLog)("RegistryService", "Evicted service after missed heartbeats", { key, id }); continue; } instance.status = "OFF"; } else { instance.status = "ON"; instance.missedHeartbeats = 0; } } if (instances.size === 0) { this.memoryStore.delete(key); (0, import_common2.debugLog)("RegistryService", "Removed empty service set", { key }); } } } async disconnect() { if (this.cleanupInterval) { clearInterval(this.cleanupInterval); this.cleanupInterval = void 0; (0, import_common2.debugLog)("RegistryService", "Stopped cleanup interval"); } } getInstanceId(instance) { return `${instance.name}:${instance.host}:${instance.port}`; } getOrCreateInstances(key) { let instances = this.memoryStore.get(key); if (!instances) { instances = /* @__PURE__ */ new Map(); this.memoryStore.set(key, instances); } return instances; } }; MemoryStorage = __decorateClass([ __decorateParam(0, (0, import_common.Inject)(import_constants.STORAGE_OPTIONS)) ], MemoryStorage); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { MemoryStorage }); //# sourceMappingURL=memory-storage.service.js.map