This commit is contained in:
NoLogicAlan committed 2026-05-06 17:57:30 +03:00
1 parent 33ff2632f9
commit 77b6a40d69
5 files changed
+316 -123

No files matched your search

+113 -33
View File
@@ -1,59 +1,126 @@
import { createClient } from "redis";
/**
* Default retry strategy: exponential backoff starting at 500ms, max 5s, max 20 retries.
* @param {Object} options
* @returns {number} Delay in ms before next retry, or an Error to stop retrying
*/
const DEFAULT_RETRY_STRATEGY = (options) => {
if (options.totalRetryTime > 60_000) {
// Stop retrying after 60s total — something is fundamentally wrong
return new Error("Redis reconnection exhausted after 60s");
}
return Math.min(options.attempt * 500, 5_000);
};
export class RedisHandler {
platform = "fluxer";
/** @type {import("redis").RedisClientType|null} */
client = null;
/** @type {import("redis").RedisClientType|null} */
subscriber = null;
/** Whether the handler has been explicitly destroyed */
_destroyed = false;
/**
*
* @param {Object} opts
* @param {import("redis").RedisClientOptions} opts
* @param {import("redis").RedisClientOptions} opts Redis client options
* @param {string} [opts.platform] Platform identifier for channel namespacing
*/
constructor(opts) {
this.client = createClient(opts);
constructor(opts = {}) {
this.platform = opts.platform ?? "fluxer";
const clientOpts = {
...opts,
socket: {
...(opts.socket ?? {}),
reconnectStrategy: DEFAULT_RETRY_STRATEGY,
},
};
this.client = createClient(clientOpts);
this.client.on("error", (err) => {
console.log("[Redis/Main] Error: ", err);
});
this.client.connect().then(() => {
console.log("[RedisMain] Connected");
this.readyMessage();
console.log("[Redis/Main] Error:", err.message);
});
this.subscriber = this.client.duplicate();
this.subscriber.on("error", (err) => {
console.log("[Redis/Subscriber] Error: ", err);
});
this.subscriber.connect().then(() => {
console.log("[Redis/Subscriber] Connected");
this.subscriber.subscribe("request", async (m) => {
const payload = JSON.parse(m);
if (payload.platform !== this.platform) return;
const result = await this.handleRequest(payload.content);
this.send("response", JSON.stringify({
id: payload.id,
content: result
}));
});
this.subscriber.subscribe("info", (m) => {
const data = JSON.parse(m);
if (data.platform !== "backend") return;
if (data.type !== "requestConnected") return;
this.readyMessage();
});
console.log("[Redis/Subscriber] Error:", err.message);
});
this._connect();
}
/**
* Connect both publisher and subscriber with reconnection support.
* Wrapped in a method so it can be called once from the constructor.
*/
async _connect() {
try {
await this.client.connect();
console.log("[Redis/Main] Connected");
this.readyMessage();
} catch (e) {
console.log("[Redis/Main] Initial connection failed:", e.message);
}
try {
await this.subscriber.connect();
console.log("[Redis/Subscriber] Connected");
this.subscriber.subscribe("request", async (m) => {
if (this._destroyed) return;
try {
const payload = JSON.parse(m);
if (payload.platform !== this.platform) return;
if (typeof this.handleRequest !== "function") return;
const result = await this.handleRequest(payload.content);
this.send("response", JSON.stringify({
id: payload.id,
content: result,
}));
} catch (e) {
console.log("[Redis/Subscriber] Request handler error:", e.message);
}
});
this.subscriber.subscribe("info", (m) => {
if (this._destroyed) return;
try {
const data = JSON.parse(m);
if (data.platform !== "backend") return;
if (data.type !== "requestConnected") return;
this.readyMessage();
} catch (e) {
console.log("[Redis/Subscriber] Info handler error:", e.message);
}
});
} catch (e) {
console.log("[Redis/Subscriber] Initial connection failed:", e.message);
}
}
readyMessage() {
if (this._destroyed) return;
this.send("info", JSON.stringify({
platform: "fluxer",
type: "connected"
platform: this.platform,
type: "connected",
}));
}
/**
*
* Publish a message to a Redis channel.
* @param {string} channel
* @param {string} message
* @returns {Promise<number>}
* @returns {Promise<number>} Number of subscribers that received the message
*/
send(channel, message) {
return this.client.publish(channel, message);
if (this._destroyed || !this.client?.isReady) return Promise.resolve(0);
return this.client.publish(channel, message).catch((e) => {
console.log("[Redis/Main] Publish error:", e.message);
return 0;
});
}
/**
@@ -63,10 +130,23 @@ export class RedisHandler {
* @returns {Promise<Object>}
*/
handleRequest;
/**
* @param {RequestCallback} handler
*/
setRequestHandler(handler) {
this.handleRequest = handler;
}
/**
* Gracefully close both Redis connections.
* Should be called on process shutdown to avoid abrupt connection drops.
* @returns {Promise<void>}
*/
async destroy() {
this._destroyed = true;
try { await this.subscriber?.quit(); } catch (_) {}
try { await this.client?.quit(); } catch (_) {}
console.log("[Redis] Connections closed gracefully");
}
}