From 126c24d5f3147e8234def7954507d372ab123c8f Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 26 May 2023 11:39:02 +0200 Subject: [PATCH 1/6] Attemp to clusterize client --- src/clientMain.ts | 48 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/src/clientMain.ts b/src/clientMain.ts index 42d2b37..44e7ede 100644 --- a/src/clientMain.ts +++ b/src/clientMain.ts @@ -1,4 +1,6 @@ import * as dotenv from 'dotenv' +import cluster from 'cluster' +import os from 'os' dotenv.config() import express from 'express' import cors from 'cors' @@ -7,25 +9,43 @@ import { dbReady } from './db/db' import processAll from './process/process' import listenKills from './process/onKill' -const app = express() +const cCPUs = os.cpus().length; + const port = 3000 -app.use(express.json()) +if (cluster.isPrimary) { + // Create a worker for each CPU + for (let i = 0; i < cCPUs; i++) { + cluster.fork(); + } + cluster.on('online', function (worker) { + console.log('Worker ' + worker.process.pid + ' is online.'); + }); + cluster.on('exit', function (worker, code, signal) { + console.log('worker ' + worker.process.pid + ' died.'); + }); +} -app.use(cors()) -app.get('/', (req, res) => { - res.send('Tone client API online') -}) -app.use('/', client) export default new Promise(async (resolve, reject) => { - await dbReady() - await processAll() - await listenKills() - const listenServer = app.listen(port, '0.0.0.0', () => { - console.log(`Tone client api listening on port ${port}`) - resolve(listenServer) - }) + if (!cluster.isPrimary) { + const app = express() + app.use(express.json()) + app.use(cors()) + app.get('/', (req, res) => { + res.send('Tone client API online') + }) + + app.use('/', client) + + await dbReady() + await processAll() + await listenKills() + const listenServer = app.listen(port, '0.0.0.0', () => { + console.log(`Tone client api listening on port ${port}`) + resolve(listenServer) + }) + } }) \ No newline at end of file From f0f824235d00d16a40f43e418fc6838f8d940896 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 26 May 2023 14:01:20 +0200 Subject: [PATCH 2/6] add x-file-size header --- src/client/client.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/client/client.ts b/src/client/client.ts index 261c4ab..9631d51 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -139,7 +139,10 @@ router.get( ',' + Math.abs(new Date().getTime() - timeStart.getTime()) / 1000 + ',' + req.originalUrl ) - res.status(200).send(data) + const dataString = JSON.stringify(data) + const buffer = Buffer.from(dataString) + const size = buffer.length + res.status(200).setHeader('x-file-size', size).send(buffer) } ) From a2a83f85bdd21facdaa1802e7d7ad1e7f942342a Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 26 May 2023 14:12:06 +0200 Subject: [PATCH 3/6] fix xfilesize header --- src/client/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/client.ts b/src/client/client.ts index 9631d51..719b172 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -142,7 +142,7 @@ router.get( const dataString = JSON.stringify(data) const buffer = Buffer.from(dataString) const size = buffer.length - res.status(200).setHeader('x-file-size', size).send(buffer) + res.status(200).setHeader('X-File-Size', size).send(buffer) } ) From 7fbb068e4295941e1d7280c00d31ffae9e314246 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 26 May 2023 14:16:32 +0200 Subject: [PATCH 4/6] fix content type --- src/client/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/client.ts b/src/client/client.ts index 719b172..0f5fdb9 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -142,7 +142,7 @@ router.get( const dataString = JSON.stringify(data) const buffer = Buffer.from(dataString) const size = buffer.length - res.status(200).setHeader('X-File-Size', size).send(buffer) + res.status(200).setHeader('X-File-Size', size).setHeader('Content-Type', 'application/json').send(buffer) } ) From d49e9232f55f2cab79b86c529a35e847b45a076c Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 26 May 2023 14:25:09 +0200 Subject: [PATCH 5/6] fix cors for file size and encoding --- src/clientMain.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/clientMain.ts b/src/clientMain.ts index 44e7ede..4a0635c 100644 --- a/src/clientMain.ts +++ b/src/clientMain.ts @@ -33,7 +33,7 @@ export default if (!cluster.isPrimary) { const app = express() app.use(express.json()) - app.use(cors()) + app.use(cors({ allowedHeaders: ['X-File-Size', 'Content-Encoding'] })) app.get('/', (req, res) => { res.send('Tone client API online') }) From fe4fad401615e152105e29a1872d48cea6eb830a Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 26 May 2023 14:27:55 +0200 Subject: [PATCH 6/6] fix cors expose headers --- src/clientMain.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/clientMain.ts b/src/clientMain.ts index 4a0635c..8c1ef86 100644 --- a/src/clientMain.ts +++ b/src/clientMain.ts @@ -33,7 +33,7 @@ export default if (!cluster.isPrimary) { const app = express() app.use(express.json()) - app.use(cors({ allowedHeaders: ['X-File-Size', 'Content-Encoding'] })) + app.use(cors({ exposedHeaders: ['X-File-Size', 'Content-Encoding'] })) app.get('/', (req, res) => { res.send('Tone client API online') })