UNPKG

@duongtrungnguyen/nestro

Version:
122 lines 4.38 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; 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); import { Inject } from "@nestjs/common"; import { debugLog } from "../../common"; import { STORAGE_OPTIONS } from "../constants"; let MemoryStorage = class { constructor(options) { this.options = options; this.memoryStore = /* @__PURE__ */ new Map(); } onModuleInit() { this.cleanupInterval = setInterval(() => this.cleanup(), this.options.cleanupTTL); 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 }); 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); debugLog("RegistryService", "Recovered missing instance via heartbeat", { key, instanceId }); return; } existingInstance.lastHeartbeatAt = now; existingInstance.missedHeartbeats = 0; existingInstance.status = "ON"; 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); } 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); 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); debugLog("RegistryService", "Removed empty service set", { key }); } } } async disconnect() { if (this.cleanupInterval) { clearInterval(this.cleanupInterval); this.cleanupInterval = void 0; 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, Inject(STORAGE_OPTIONS)) ], MemoryStorage); export { MemoryStorage }; //# sourceMappingURL=memory-storage.service.js.map