feat: prepare redis subsystem for unix domain socket connections

This commit is contained in:
David Arnold 2023-06-09 10:55:54 -05:00 committed by David
parent 34f1b9cb65
commit 640e7d79b3
No known key found for this signature in database
GPG key ID: AB15A6AF1101390D
2 changed files with 24 additions and 5 deletions

View file

@ -50,13 +50,27 @@ function get_conf() {
if (process.env.FRAPPE_SOCKETIO_PORT) { if (process.env.FRAPPE_SOCKETIO_PORT) {
conf.socketio_port = process.env.FRAPPE_SOCKETIO_PORT; conf.socketio_port = process.env.FRAPPE_SOCKETIO_PORT;
} }
if (process.env.FRAPPE_SOCKETIO_UDS) {
conf.socketio_uds = process.env.FRAPPE_SOCKETIO_UDS;
}
return conf; return conf;
} }
function get_redis_subscriber(kind = "redis_queue", options = {}) { function get_redis_subscriber(kind = "redis_queue", options = {}) {
const conf = get_conf(); const conf = get_conf();
const host = conf[kind]; const connStr = conf[kind];
return redis.createClient({ url: host, ...options }); let client;
// TODO: revise after https://github.com/redis/node-redis/issues/2530
// is solved for a more elegant implementation
if (connStr && connStr.startsWith("unix://")) {
client = redis.createClient({
socket: { path: connStr.replace("unix://", "") },
...options,
});
} else {
client = redis.createClient({ url: connStr, ...options });
}
return client;
} }
module.exports = { module.exports = {

View file

@ -1,9 +1,12 @@
const { Server } = require("socket.io"); const { Server } = require("socket.io");
const http = require("node:http");
const { get_conf, get_redis_subscriber } = require("../node_utils"); const { get_conf, get_redis_subscriber } = require("../node_utils");
const conf = get_conf(); const conf = get_conf();
let io = new Server({ const server = http.createServer();
let io = new Server(server, {
cors: { cors: {
// Should be fine since we are ensuring whether hostname and origin are same before adding setting listeners for s socket // Should be fine since we are ensuring whether hostname and origin are same before adding setting listeners for s socket
origin: true, origin: true,
@ -55,6 +58,8 @@ const subscriber = get_redis_subscriber();
})(); })();
// ======================= // =======================
let uds = conf.socketio_uds;
let port = conf.socketio_port; let port = conf.socketio_port;
io.listen(port); server.listen(uds || port, () => {
console.log("Realtime service listening on: ", port); console.log("Realtime service listening on: ", uds || port);
});