Skip to content

use cluster in server-ipc #5

@jpolstre

Description

@jpolstre

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions