[server] express server also listens on unix socket

This commit is contained in:
nymkappa
2024-04-07 18:39:37 +09:00
parent 501b79fdce
commit bed00fbd41
7 changed files with 93 additions and 32 deletions

View File

@@ -48,7 +48,9 @@ import aboutRoutes from './api/about.routes';
class Server {
private wss: WebSocket.Server | undefined;
private wssUnixSocket: WebSocket.Server | undefined;
private server: http.Server | undefined;
private serverUnixSocket: http.Server | undefined;
private app: Application;
private currentBackendRetryInterval = 1;
private backendRetryCount = 0;
@@ -136,7 +138,9 @@ class Server {
}
this.server = http.createServer(this.app);
this.serverUnixSocket = http.createServer(this.app);
this.wss = new WebSocket.Server({ server: this.server });
this.wssUnixSocket = new WebSocket.Server({ server: this.serverUnixSocket });
this.setUpWebsocketHandling();
@@ -192,6 +196,14 @@ class Server {
logger.notice(`Mempool Server is running on port ${config.MEMPOOL.HTTP_PORT}`);
}
});
this.serverUnixSocket.listen(config.MEMPOOL.UNIX_SOCKET_PATH, () => {
if (worker) {
logger.info(`Mempool Server worker #${process.pid} started`);
} else {
logger.notice(`Mempool Server is listening on ${config.MEMPOOL.UNIX_SOCKET_PATH}`);
}
});
}
async runMainUpdateLoop(): Promise<void> {
@@ -265,8 +277,12 @@ class Server {
setUpWebsocketHandling(): void {
if (this.wss) {
websocketHandler.setWebsocketServer(this.wss);
websocketHandler.addWebsocketServer(this.wss);
}
if (this.wssUnixSocket) {
websocketHandler.addWebsocketServer(this.wssUnixSocket);
}
if (Common.isLiquid() && config.DATABASE.ENABLED) {
blocks.setNewBlockCallback(async () => {
try {
@@ -338,6 +354,10 @@ class Server {
if (config.DATABASE.ENABLED) {
DB.releasePidLock();
}
this.server?.close();
this.serverUnixSocket?.close();
this.wss?.close();
this.wssUnixSocket?.close();
process.exit(code);
}