Browse Source

(evcom) Add fix for pausing against big buffers.

discussion:
http://groups.google.com/group/nodejs/browse_thread/thread/11a920da4d0ed21d
v0.7.4-release
Ryan 16 years ago
parent
commit
738d20f6f0
  1. 4
      deps/evcom/evcom.c
  2. 57
      test/mjsunit/test-tcp-throttle-kernel-buffer.js
  3. 22
      test/mjsunit/test-tcp-throttle.js

4
deps/evcom/evcom.c

@ -853,7 +853,7 @@ on_io_event(EV_P_ ev_io *watcher, int revents)
while (have_read_event || have_write_event) { while (have_read_event || have_write_event) {
/* RECV LOOP - TRY TO CLEAR THE BUFFER */ /* RECV LOOP - TRY TO CLEAR THE BUFFER */
if (stream->read_action == NULL) { if (stream->read_action == NULL || !ev_is_active(&stream->read_watcher)) {
have_read_event = FALSE; have_read_event = FALSE;
} else { } else {
r = stream->read_action(stream); r = stream->read_action(stream);
@ -866,7 +866,7 @@ on_io_event(EV_P_ ev_io *watcher, int revents)
} }
/* SEND LOOP - TRY TO CLEAR THE BUFFER */ /* SEND LOOP - TRY TO CLEAR THE BUFFER */
if (stream->write_action == NULL) { if (stream->write_action == NULL || !ev_is_active(&stream->write_watcher)) {
have_write_event = FALSE; have_write_event = FALSE;
} else { } else {
r = stream->write_action(stream); r = stream->write_action(stream);

57
test/mjsunit/test-tcp-throttle-kernel-buffer.js

@ -0,0 +1,57 @@
include("mjsunit.js");
PORT = 20444;
N = 30*1024; // 500kb
puts("build big string");
var body = "";
for (var i = 0; i < N; i++) {
body += "C";
}
puts("start server on port " + PORT);
server = node.tcp.createServer(function (connection) {
connection.addListener("connect", function () {
connection.send(body);
connection.fullClose();
});
});
server.listen(PORT);
chars_recved = 0;
npauses = 0;
function onLoad () {
var paused = false;
client = node.tcp.createConnection(PORT);
client.setEncoding("ascii");
client.addListener("receive", function (d) {
chars_recved += d.length;
puts("got " + chars_recved);
if (!paused) {
client.readPause();
npauses += 1;
paused = true;
puts("pause");
x = chars_recved;
setTimeout(function () {
assertEquals(chars_recved, x);
client.readResume();
puts("resume");
paused = false;
}, 100);
}
});
client.addListener("eof", function () {
server.close();
client.close();
});
}
function onExit () {
assertEquals(N, chars_recved);
assertTrue(npauses > 2);
}

22
test/mjsunit/test-tcp-throttle.js

@ -31,15 +31,31 @@ function onLoad () {
setTimeout(function () { setTimeout(function () {
chars_recved = recv.length; chars_recved = recv.length;
puts("chars_recved: " + chars_recved); puts("pause at: " + chars_recved);
assertTrue(chars_recved > 1); assertTrue(chars_recved > 1);
client.readPause(); client.readPause();
setTimeout(function () { setTimeout(function () {
puts("chars_recved: " + chars_recved); puts("resume at: " + chars_recved);
assertEquals(chars_recved, recv.length); assertEquals(chars_recved, recv.length);
client.readResume(); client.readResume();
setTimeout(function () {
chars_recved = recv.length;
puts("pause at: " + chars_recved);
client.readPause();
setTimeout(function () {
puts("resume at: " + chars_recved);
assertEquals(chars_recved, recv.length);
client.readResume();
}, 500);
}, 500);
}, 500); }, 500);
}, 100);
}, 500);
client.addListener("eof", function () { client.addListener("eof", function () {
server.close(); server.close();

Loading…
Cancel
Save