From 738d20f6f02ac274abae0e178517c589e02533a4 Mon Sep 17 00:00:00 2001 From: Ryan Date: Mon, 10 Aug 2009 12:14:41 +0200 Subject: [PATCH] (evcom) Add fix for pausing against big buffers. discussion: http://groups.google.com/group/nodejs/browse_thread/thread/11a920da4d0ed21d --- deps/evcom/evcom.c | 6 +- .../test-tcp-throttle-kernel-buffer.js | 57 +++++++++++++++++++ test/mjsunit/test-tcp-throttle.js | 22 ++++++- 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 test/mjsunit/test-tcp-throttle-kernel-buffer.js diff --git a/deps/evcom/evcom.c b/deps/evcom/evcom.c index f3a69cf1444..5e37822ccf7 100644 --- a/deps/evcom/evcom.c +++ b/deps/evcom/evcom.c @@ -853,9 +853,9 @@ on_io_event(EV_P_ ev_io *watcher, int revents) while (have_read_event || have_write_event) { /* 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; - } else { + } else { r = stream->read_action(stream); if (r == AGAIN) { @@ -866,7 +866,7 @@ on_io_event(EV_P_ ev_io *watcher, int revents) } /* 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; } else { r = stream->write_action(stream); diff --git a/test/mjsunit/test-tcp-throttle-kernel-buffer.js b/test/mjsunit/test-tcp-throttle-kernel-buffer.js new file mode 100644 index 00000000000..5639c6aa59d --- /dev/null +++ b/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); +} diff --git a/test/mjsunit/test-tcp-throttle.js b/test/mjsunit/test-tcp-throttle.js index 51eb60a8227..b5aab9a81d7 100644 --- a/test/mjsunit/test-tcp-throttle.js +++ b/test/mjsunit/test-tcp-throttle.js @@ -31,15 +31,31 @@ function onLoad () { setTimeout(function () { chars_recved = recv.length; - puts("chars_recved: " + chars_recved); + puts("pause at: " + chars_recved); assertTrue(chars_recved > 1); client.readPause(); setTimeout(function () { - puts("chars_recved: " + chars_recved); + puts("resume at: " + chars_recved); assertEquals(chars_recved, recv.length); 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); - }, 100); + + }, 500); client.addListener("eof", function () { server.close();