2016-11-07 3 views
0

WebSocket Server를 호스팅하는 NodeJS가 있습니다. WebSocket은 Redis에서 메시지를 재배포합니다.NodeJS + Redis + WebSocket 메모리 관리?

전체 라인은 Redis에서 일부 데이터를 푸시하는 Python 스크립트가 있고 NodeJS가 연결된 클라이언트에 Redis 새로 입력 된 데이터를 읽는 WebSocket입니다. 내 문제는 NodeJ가 항상 메모리를 차지하고 잠시 후에 그냥 버스트하고 중지한다는 것입니다.

제 코드가 꽤 간단하기 때문에 제 문제는 무엇인지 모르겠습니다.

연결된 클라이언트에서 메시지를 받기 위해 WebSocket이 필요하지 않습니다. 데이터를 많이 보내고 데이터를 많이 보내야하기 때문에입니다.

var server = require('websocket').server, 
    http = require('http'); 
var redis = require("redis"), 
    client = redis.createClient(); 

var socket = new server({ 
httpServer: http.createServer().listen(443), 
keepalive: false 
}); 

client.subscribe("attack-map-production"); 
socket.on('request', function(request) { 
var connection = request.accept(null, request.origin); 

connection.on('message', function(message) { 
    console.log(message); 
      client.on("message", function(channel, message){ 
        connection.send(message); 
      }); 
}); 

connection.on('close', function(connection) { 
    console.log('connection closed'); 
}); 
}); 

나는이 훨씬 더 빠르게 만들 가능성이 내 서버에있는 모든 메모리를 먹는없이이 일을하고 찾고 있어요,하지만 난 충분히 빠른 것 같아요.

아마도 NodeJS는 이러한 종류의 작업을위한 것이 아닙니다.

도움을 주시면 감사하겠습니다. 감사합니다. . 아래 제공된 정보에

업데이트 2016년 11월 8일

, 난 내 코드를 "업데이트"했다. 문제는 여전히 거기에있다, 나는 대답을 찾기 위해 주변을 둘러 볼 것이다. .. 그러나 나는 이것을 실제로 얻지 않고있다.

var server = require('websocket').server, 
    http = require('http'); 
var redis = require("redis"), 
    client = redis.createClient(); 


var socket = new server({ 
httpServer: http.createServer().listen(443), 
keepalive: false 
}); 

client.subscribe("attack-map-production"); 

socket.on('request', function(request) { 
var connection = request.accept(null, request.origin); 

    client.on("message", function(channel, message){ 
      connection.send(message); 
    }); 

connection.on('close', function(connection) { 
    console.log('connection closed'); 
}); 
}); 

업데이트 그래서 여기 2016년 11월 16일

나의 새로운 코드 :

var io = require('socket.io').listen(443); 
var sub = require('redis').createClient(); 

io.sockets.on('connection', function (sockets) { 
    sockets.emit('message',{Hello: 'World!'}); 
    sub.subscribe('attack-map-production'); // Could be any patterni 

    sockets.on('disconnect', function() { 
      sub.unsubscribe('attack-map-production'); 
    }); 
}); 

sub.on('message', function(channel, message) { 
    io.sockets.json.send(message); 
}); 

에도이 코드, nodejs 더욱 100 %의 CPU에 가서하게하고, 모든 것이 멈출 때까지 정말로 느리게 시작됩니다.

내 데이터의 흐름은 파이썬 스크립트가 데이터를 Redis로 푸시하고 내 구독을 통해 webSocket 및 Socket.io를 통해 내 데이터를 브라우저로 다시 푸시하는 것입니다.

간단합니다. 어떻게 느려질 수 있습니까? 나는 단지 그것을 얻지 않는다.

+0

입력 해 주셔서 감사합니다. 작동 여부를 확인한 후 다시 연락 드리겠습니다. – user1861854

답변

0

아마도 NodeJS는 이러한 종류의 작업을위한 것이 아닙니다.

노드가 무엇인가를위한 것이라면. I/O 스트림과 읽기/쓰기는 노드 비동기의 주요 이점입니다.

어떤 서버에서 실행하고 있습니까? EC2 인스턴스가 너무 작 으면 메모리 문제가 발생할 수 있습니다.

기타 누수입니다. 그것은 추적하기가 어렵습니다.

코드는 작습니다.

나는 모든 경우에 console.log를 제거합니다.

connection.on('message', function(message) { 
    console.log(message); 
      client.on("message", function(channel, message){ 
        connection.send(message); 
      }); 
}); 

이 부분은, 같은 이름을 가진 두 개의 변수를 의심스러운 느낌이, 사용되지 않는 변수, 그것은 문제가 요구, 당신은 레디 스의 메시지를 기다리는하기 위해 연결 메시지를 수신 할 필요가 난 정말 왜하지 않습니다 .

+0

정보를 제공해 주셔서 감사합니다. 나는 내 코드를 정리했다. .. 나는 생각한다. 그러나 같은 문제가 발생한다. 나는 왜 그런지 모르겠다. 내 코드에는 아무것도 없다. 내 "새로운"코드는 다음과 같습니다. socket.on ('request', function (request) { var connection = request.accept (null, request.origin); client.on ("message", function 메시지) { connection.send (메시지) }) connection.on ('확대', 함수 (연결) { CONSOLE.LOG ('연결 폐쇄');} ) }); – user1861854

0

변수 클라이언트를 호출 할 때마다 노드 내부에 redis 클라이언트의 인스턴스를 만들고 결코 닫지 마십시오. 그래서 10000 소켓 'request'를받는다면 10000 개의 redis 인스턴스가 생길 것입니다.

당신은() 쓰기 나 레디 스에 읽기 일단

var server = require('websocket').server, 
    http = require('http'); 
var redis = require("redis"), 
    client = redis.createClient(); 


var socket = new server({ 
httpServer: http.createServer().listen(443), 
keepalive: false 
}); 

client.subscribe("attack-map-production"); 

socket.on('request', function(request) { 
var connection = request.accept(null, request.origin); 

    client.on("message", function(channel, message){ 
      connection.send(message); 
    }); 

client.quit(); // MISSING LINE 

connection.on('close', function(connection) { 
    console.log('connection closed'); 
}); 
}); 

일을하고 나는 또한 코드

httpServer: http.createServer().listen(443) 

포트의이 작품을 발견하는 명령 client.quit를 호출 할 필요가 443 https에 대한 것입니다! 보안 연결을 사용하는 경우 http : // http가 아닌 https를 호출해야합니다.

var socket = new server({ 
httpServer: https.createServer().listen(443), 
keepalive: false 
}); 

희망이 도움이 되길 바랍니다.