1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

net.socket()の再利用

1
Last updated at Posted at 2013-02-04

Issueに上がっていたので自分で試したらいろいろ分かった。

socketの再利用にはhandle(中身はpipe)の初期化に時間がかかる

これはIssacが言ってた。確かにソースを見るとC++でのwrapがされてたから重たいのだろう。でも処理の重たさよりも重要なのが2つ目。

単一のイベントループ上だとうまくreuseできない

理由がよく分からないけれど1つ目だとうまくいくけれど、2つ目だとうまくいかない

var net = require('net');

var socket = new net.Socket();

socket.on('close',function(){
	console.log('close');
	process.nextTick(function(){
		socket.connect(80, "www.google.com");
	});
});

socket.connect(80, "www.google.com");

socket.destroy();
var net = require('net');

var socket = new net.Socket();

socket.on('close',function(){
	console.log('close');
	socket.connect(80, "www.google.com");
});

socket.connect(80, "www.google.com");

socket.destroy();

process.nextTickの部分はsetTimeoutにしてもうまく言ったので多分socketが一度閉じられてからconnectするまでの間をイベントループ上でずらす必要があるんじゃないかと考えた。

処理が重いから?

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?