mosca icon indicating copy to clipboard operation
mosca copied to clipboard

Mosca take so much CPU usage on server side. Is it a problem?

Open Emiya0306 opened this issue 7 years ago • 7 comments

When I start my app with mosca, I always hear the Mac fan running loudly and hot. I see the memory watcher, node process will take 68% cpu for running. I just write a sample demo which will take so much CPU.

Is it something wrong on my code? The mosca version is 2.5.1.

const mosca = require('mosca')
const http = require('http')
const express = require('express')

const settings = {
  backend: {
    type: 'mongo',
    url: `mongodb://127.0.0.1:27017/mosca`,
    pubsubCollection: 'chatChannel',
    mongo: {}
  }
}

const server = express()

const httpServer = http.createServer()

const mqtt = new mosca.Server(settings)

mqtt.attachHttpServer(httpServer)

mqtt.on('clientConnected', function (client) {
  console.log('client connected', client.id)
})

mqtt.on('published', function (packet, client) {
  console.log('Published', packet.payload)
})

mqtt.on('ready', () => {
  console.log('Chatting server is up and running.')
})

httpServer
  .on('request', server)
  .listen('3000', '127.0.0.1')

Emiya0306 avatar Jun 10 '17 11:06 Emiya0306

Which version of MongoDB are you using? Are you sending data to the broker?

mcollina avatar Jun 12 '17 07:06 mcollina

Mongo version: v3.2.3. No, I just run the server and listen on the port.

Emiya0306 avatar Jun 13 '17 12:06 Emiya0306

@Emiya0306 can you send just some messages through?

And use Mongo 3.4?

mcollina avatar Jun 13 '17 12:06 mcollina

I change Mongo version to v3.4.4, it still takes 59.5% CPU usage only listening. ~~I'm trying mqtt client send some messages, I think the result may be the same.~~

Emiya0306 avatar Jun 13 '17 13:06 Emiya0306

Oh, it's interesting! When I send some messages to server, the CPU usage is 0%. Restart the server is still 0% CPU usage. Then I delete the DB and start the server again, it will become high CPU usage. So how to avoid the first time high CPU usage?

Emiya0306 avatar Jun 13 '17 13:06 Emiya0306

This is a known pattern, given how little impacts production performance I never looked into it. Please send a PR if you find out.

mcollina avatar Jun 13 '17 14:06 mcollina

same issue for me, the thing is, mosca is taking down the whole server. The clients must call a different http service before connecting to mqtt, but they can't since mosca is blocking the machine

zontafil avatar May 31 '18 13:05 zontafil