mowgli has asked for the wisdom of the Perl Monks concerning the following question:
Hello, fellow monks,
here's a question that came up while I was talking to a friend earlier tonight and that I couldn't really answer: what's the best/easiest/most portable way of disabling Nagle's algorithm on a TCP socket? I checked my paper copy of the Perl Cookbook, but unfortunately, the solution it gives requires you to use h2xs to process a standard header file, which really is overkill for a short script (not to mention that it's not really portable to platforms like windows which don't come with C library headers, anyway).
Thanks for your help. ^^
Re: Portably disabling Nagle's algorithm for TCP
by thospel (Hermit) on Jan 02, 2005 at 04:24 UTC
|
If your system actually has the constant, it will nowadays
be in Socket.pm.
Query, set, unset:
use Socket qw(IPPROTO_TCP TCP_NODELAY);
sub nagle(*;$) {
my $fh = shift;
if (!@_) {
return unpack("I", getsockopt($fh, IPPROTO_TCP, TCP_NODELAY) |
+|
croak "Could not get Nagle state: $!") ? 0 : 1;
}
if (shift) {
setsockopt($fh, IPPROTO_TCP, TCP_NODELAY, 0) ||
croak "Couldn't enable Nagle's algorithm: $!";
} else {
setsockopt($fh, IPPROTO_TCP, TCP_NODELAY,1) ||
croak "Couldn't disable Nagle's algorithm: $!";
}
}
(A lot of perl code floating on the internet incorrectly uses SOL_SOCKET as level, possibly because the cookbook
gets it wrong).
This functionality should only be used if it makes sense though. | [reply] [d/l] |
|
Thanks. How can I make sure that every system the script possibly runs on actually has that constant, though?
| [reply] |
|
# untested code
use Socket qw(IPPROTO_TCP)
my $has_nagle = eval "Socket::TCP_NODELAY(); 1";
...
if ($has_nagle) {
setsockopt($fh, IPPROTO_TCP,
Socket::TCP_NODELAY(), 1) ||
croak "Couldn't disable Nagle's algorithm: $!";
} else {
warn("Your system doesn't seem to support turning of Nagle's algor
+ithm\n");
}
(I also didn't try to import it in case you use a Socket.pm that doesn't even have the constant) | [reply] [d/l] |
|
Re: Portably disabling Nagle's algorithm for TCP
by The Mad Hatter (Priest) on Jan 02, 2005 at 03:02 UTC
|
You usually turn off Nagle's algorithm with the TCP_NODELAY flag. Net::TCP supports this.
(I might be misunderstanding your question. If I am, feel free to correct me.)
| [reply] [d/l] |
|
Thanks. Does that work on all platforms, though? The CPAN tests indicate that it fails on win32 due to undeclared identifiers.
| [reply] |
•Re: Portably disabling Nagle's algorithm for TCP
by merlyn (Sage) on Jan 02, 2005 at 04:09 UTC
|
| [reply] |
|
You seem to think that Nagle's algorithm isn't a tradeoff. It is: it increases bandwidth, but also increases latency. Fairly often, you don't care about bandwidth one way or the other, but want to decrease latency.
It's like saying "automatic is picking the wrong gear for this application, how do I put this car into second?".
Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).
| [reply] |
|
Because the hotel parking attendants drive at 80 mph and you had to sue it to recoup the repair costs. :)
--
$Stalag99{"URL"}="http://stalag99.keenspace.com";
| [reply] |
|
In order to get rid of latency (however small) in a case where bandwidth isn't really a concern. "efficiently" can mean different things, depending on what you want to accomplish. :)
| [reply] |
|
Randal,
While your question is a valid one to ask, your reply is disappointing, and IMHO, misplaced.
The fact that the OP knows what Nagle's Algorithm is, has sought and found a (less than satisfactory) solution in the Cookbook (which also describes a reason to disable it), makes me think the OP might have some idea as to "Why?".
I'm not trying to pick on you, I'm just pointing out that this is an example of what I'll call the Bunjee Reply, ala Dilbert. Sort of a That's a not-so-brilliant-question see-ya later kind of response. It makes me wonder if someone has posted the corollary to How (Not) To Ask A Question, namely, How (Not) to Answer a Question?
If I'm off base here, someone let me know.
-QM
--
Quantum Mechanics: The dreams stuff is made of
| [reply] |
|
|