代码如下: var net = require(‘net’); var conn = net.createConnection(6001, ‘127.0.0.1’); var str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
function cycle(){
var result = conn.write(str);
if(result == false){
console.log(conn);
}
}
for(var i = 1; i <= 500; i++){
cycle();
}
上述伪码是一种业务场景。 看了一下node源代码,stream的writable.write会现将数据写入buffer,等待buffer超过限定值之后,在进行真正的写入。当buffer满了之后,这时候我代码中conn.write就会写入失败。node文档上说"Writes chunk to the stream. Returns true if the data has been flushed to the underlying resource. Returns false to indicate that the buffer is full, and the data will be sent out in the future",我的问题是,conn.write就会写入失败,数据在后面会发送,具体机制是怎么样的?然后这部分数据会存放在哪儿?
然后上面的场景使用多个连接不停的发送数据,会出现系统内存不停的增长,是和conn.write的机制有关系吗?
谢谢指教!!
编辑器不会用,上面的代码有点问题,分开的。我再贴一下试试:
var net = require('net');
var conn = net.createConnection(6001, '127.0.0.1');
var str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
function cycle(){
var result = conn.write(str);
if(result == false){
console.log(conn);
}
}
for(var i = 1; i <= 500; i++){
cycle();
}
@liangjun0305 搞错了,内核默认的缓冲区node现在还控制不了,node应用层也有缓冲区,见这里http://nodejs.org/api/net.html#net_socket_buffersize