From e06ab750cb5cf59fda9ee0d8ca64eab63d96609b Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Tue, 27 Oct 2009 09:27:54 +0000 Subject: [PATCH] Secondary fix for the file descriptor starvation problem --- .../channel/socket/nio/NioDatagramWorker.java | 21 ++++++++++++++----- .../netty/channel/socket/nio/NioWorker.java | 21 ++++++++++++++----- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioDatagramWorker.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioDatagramWorker.java index e64136d6b3..1f160875e0 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioDatagramWorker.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioDatagramWorker.java @@ -308,7 +308,7 @@ class NioDatagramWorker implements Runnable { * Will go through all the {@link ChannelRegistionTask}s in the * task queue and run them (registering them). */ - private void processRegisterTaskQueue() { + private void processRegisterTaskQueue() throws IOException { for (;;) { final Runnable task = registerTaskQueue.poll(); if (task == null) { @@ -316,13 +316,14 @@ class NioDatagramWorker implements Runnable { } task.run(); + cleanUpCancelledKeys(); } } /** * Will go through all the WriteTasks and run them. */ - private void processWriteTaskQueue() { + private void processWriteTaskQueue() throws IOException { for (;;) { final Runnable task = writeTaskQueue.poll(); if (task == null) { @@ -330,10 +331,11 @@ class NioDatagramWorker implements Runnable { } task.run(); + cleanUpCancelledKeys(); } } - private void processSelectedKeys(final Set selectedKeys) { + private void processSelectedKeys(final Set selectedKeys) throws IOException { for (Iterator i = selectedKeys.iterator(); i.hasNext();) { SelectionKey k = i.next(); i.remove(); @@ -352,12 +354,21 @@ class NioDatagramWorker implements Runnable { close(k); } - if (cancelledKeys >= 128) { // FIXME hardcoded value - break; + if (cleanUpCancelledKeys()) { + break; // Break the loop to avoid ConcurrentModificationException } } } + private boolean cleanUpCancelledKeys() throws IOException { + if (cancelledKeys >= 128) { // FIXME hardcoded value + cancelledKeys = 0; + selector.selectNow(); + return true; + } + return false; + } + private static void write(SelectionKey k) { write((NioDatagramChannel) k.attachment(), false); } 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 4dba0999fc..2adc2eb58b 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 @@ -242,7 +242,7 @@ class NioWorker implements Runnable { } } - private void processRegisterTaskQueue() { + private void processRegisterTaskQueue() throws IOException { for (;;) { final Runnable task = registerTaskQueue.poll(); if (task == null) { @@ -250,10 +250,11 @@ class NioWorker implements Runnable { } task.run(); + cleanUpCancelledKeys(); } } - private void processWriteTaskQueue() { + private void processWriteTaskQueue() throws IOException { for (;;) { final Runnable task = writeTaskQueue.poll(); if (task == null) { @@ -261,10 +262,11 @@ class NioWorker implements Runnable { } task.run(); + cleanUpCancelledKeys(); } } - private void processSelectedKeys(Set selectedKeys) { + private void processSelectedKeys(Set selectedKeys) throws IOException { for (Iterator i = selectedKeys.iterator(); i.hasNext();) { SelectionKey k = i.next(); i.remove(); @@ -283,11 +285,20 @@ class NioWorker implements Runnable { close(k); } - if (cancelledKeys >= 128) { // FIXME hardcoded value - break; + if (cleanUpCancelledKeys()) { + break; // break the loop to avoid ConcurrentModificationException } } } + + private boolean cleanUpCancelledKeys() throws IOException { + if (cancelledKeys >= 128) { // FIXME hardcoded value + cancelledKeys = 0; + selector.selectNow(); + return true; + } + return false; + } private static boolean read(SelectionKey k) { ScatteringByteChannel ch = (ScatteringByteChannel) k.channel();