Node.js Socket内存泄漏

时间:2017-09-22 07:37:13

标签: node.js sockets memory-leaks websocket

我有内存泄漏,但我在代码中找不到任何错误。

基本上我连接到3个不同的IP来接收来自它们的数据。这工作正常1-2次。但第三次,我总是得到#EC; ECONNREFUSED"或者"数据内存泄漏,11个数据监听器添加"错误。

虽然我每次都关闭套接字......

如果没有if(k == 1)逻辑,程序就不会离开socket.on(' data')函数......我也不知道为什么。

test[0] =  '10.5.0.11';
test[1] =  '10.5.0.15';
test[2] =  '10.5.0.17';
var x = -1;
var k=0;

function testIP(test) {

  x++;
  if(socket.destroyed == true) {
    socket.connect('2711', test[x], function() {
      socket.write('d\n');
      k=1;
    });
  }

  socket.on('data', function(data) {
     if(k==1) {
       data = data.toString();
       console.log(data);
         if(x<test.length-1) {
          socket.destroy();
          k=0;
          testIP(test);
         }
         else if(x == test.length-1) {
           k=0;
           x=-1;
           socket.destroy();
           setInterval(testIP, 2000, test);

         }
     }
  });
 }
testIP(test);

1 个答案:

答案 0 :(得分:0)

    Column      |            Type             |                             Modifiers
----------------+-----------------------------+-------------------------------------------------------------------
uid             | uuid                        | not null default gen_random_uuid()
date_created    | timestamp without time zone | not null default now()
sequence_id     | bigint                      | not null default nextval('documents__sequence_id__seq'::regclass)
version         | integer                     | not null default 0
source          | text                        | not null
source_id       | text                        | not null
ingestion_date  | timestamp without time zone | not null

Indexes:
    "documents__pkey" PRIMARY KEY, btree (uid)
    "documents__sequence_id__unique" UNIQUE CONSTRAINT, btree (sequence_id)
    "documents__source__source_id__deleted" UNIQUE, btree (source, source_id)
    "documents__ingestion_date__idx" btree (ingestion_date)
    "documents__source_id__source__idx" btree (source_id, source)

在函数的开头解决了问题; - )