From 77742f5cf9f2580682b27536ed911621e7bc082d Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 21 May 2009 10:28:05 +0000 Subject: [PATCH] Resolved issue: NETTY-156 Improve NIO worker performance by removing unnecessary SelectionKey access * Applied patch by Wade Poziombka * Reduced the call to SelectionKey.readyOps() even more --- .../jboss/netty/channel/socket/nio/NioWorker.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java index 8a7257e6d0..3549d33000 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java @@ -250,10 +250,14 @@ class NioWorker implements Runnable { SelectionKey k = i.next(); i.remove(); try { - if (k.isReadable()) { - read(k); + int readyOps = k.readyOps(); + if ((readyOps & SelectionKey.OP_READ) != 0) { + if (!read(k)) { + // Connection already closed - no need to handle write. + continue; + } } - if (k.isWritable()) { + if ((readyOps & SelectionKey.OP_WRITE) != 0) { write(k); } } catch (CancelledKeyException e) { @@ -262,7 +266,7 @@ class NioWorker implements Runnable { } } - private static void read(SelectionKey k) { + private static boolean read(SelectionKey k) { ScatteringByteChannel ch = (ScatteringByteChannel) k.channel(); NioSocketChannel channel = (NioSocketChannel) k.attachment(); @@ -301,7 +305,10 @@ class NioWorker implements Runnable { if (ret < 0 || failure) { close(k); + return false; } + + return true; } private static void write(SelectionKey k) {