Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use cluster in server-ipc #5

Open
jpolstre opened this issue Mar 25, 2020 · 1 comment
Open

use cluster in server-ipc #5

jpolstre opened this issue Mar 25, 2020 · 1 comment

Comments

@jpolstre
Copy link

Is it possible and recommended to use cluster in childprocess (ipc-server.js) to improve server performance with more than 1 cpu?
try the following implementation according to the ipc-node documentation:

`const ipc = require('node-ipc')
const fs = require('fs')
const cpuCount = require('os').cpus().length
const cluster = require('cluster')
const socketPath = '/tmp/ipc.sock'

function init(socketName, handlers) {
ipc.config.unlink = false
if (cluster.isMaster) {
if (fs.existsSync(socketPath)) {
fs.unlinkSync(socketPath)
}
for (let i = 0; i < cpuCount; i++) {
cluster.fork()
}
} else {

	ipc.serve(socketPath, () => {
		ipc.server.on('message', (data, socket) => {
			let msg = JSON.parse(data)
			let { id, name, args } = msg

			if (handlers[name]) {
				handlers[name](args).then(
					(result) => {
						ipc.server.emit(socket, 'message', JSON.stringify({ type: 'reply', id, result }))
					},
					(error) => {
						// Up to you how to handle errors, if you want to forward
						// them, etc
						ipc.server.emit(socket, 'message', JSON.stringify({ type: 'error', id }))
						throw error
					}
				)
			} else {
				console.warn('Unknown method: ' + name)
				ipc.server.emit(socket, 'message', JSON.stringify({ type: 'reply', id, result: null }))
			}
		})
	})

ipc.server.start()
console.log(`pid ${process.pid} listening on ${socketPath}`);
}

}

function send(name, args) {
ipc.server.broadcast('message', JSON.stringify({ type: 'push', name, args }))
}

module.exports = { init, send }`

But I get an error:
TypeError: handle.setSimultaneousAccepts is not a function at ChildProcess.target._send (internal/child_process.js:761:16) at ChildProcess.target.send (internal/child_process.js:676:19) at sendHelper (internal/cluster/utils.js:22:15) at send (internal/cluster/master.js:351:10) at internal/cluster/master.js:317:5 at SharedHandle.add (internal/cluster/shared_handle.js:29:3) at queryServer (internal/cluster/master.js:311:10) at Worker.onmessage (internal/cluster/master.js:246:5) at ChildProcess.onInternalMessage (internal/cluster/utils.js:43:8) at ChildProcess.emit (events.js:215:7)

Any help is welcome thanks.

@aperkaz
Copy link

aperkaz commented May 3, 2021

@jpolstre any updates on this? I am also interested in it 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants