Secondary fix for the file descriptor starvation problem

This commit is contained in:
Trustin Lee 2009-10-27 09:27:54 +00:00
parent 692c278cf9
commit e06ab750cb
2 changed files with 32 additions and 10 deletions

View File

@ -308,7 +308,7 @@ class NioDatagramWorker implements Runnable {
* Will go through all the {@link ChannelRegistionTask}s in the * Will go through all the {@link ChannelRegistionTask}s in the
* task queue and run them (registering them). * task queue and run them (registering them).
*/ */
private void processRegisterTaskQueue() { private void processRegisterTaskQueue() throws IOException {
for (;;) { for (;;) {
final Runnable task = registerTaskQueue.poll(); final Runnable task = registerTaskQueue.poll();
if (task == null) { if (task == null) {
@ -316,13 +316,14 @@ class NioDatagramWorker implements Runnable {
} }
task.run(); task.run();
cleanUpCancelledKeys();
} }
} }
/** /**
* Will go through all the WriteTasks and run them. * Will go through all the WriteTasks and run them.
*/ */
private void processWriteTaskQueue() { private void processWriteTaskQueue() throws IOException {
for (;;) { for (;;) {
final Runnable task = writeTaskQueue.poll(); final Runnable task = writeTaskQueue.poll();
if (task == null) { if (task == null) {
@ -330,10 +331,11 @@ class NioDatagramWorker implements Runnable {
} }
task.run(); task.run();
cleanUpCancelledKeys();
} }
} }
private void processSelectedKeys(final Set<SelectionKey> selectedKeys) { private void processSelectedKeys(final Set<SelectionKey> selectedKeys) throws IOException {
for (Iterator<SelectionKey> i = selectedKeys.iterator(); i.hasNext();) { for (Iterator<SelectionKey> i = selectedKeys.iterator(); i.hasNext();) {
SelectionKey k = i.next(); SelectionKey k = i.next();
i.remove(); i.remove();
@ -352,12 +354,21 @@ class NioDatagramWorker implements Runnable {
close(k); close(k);
} }
if (cancelledKeys >= 128) { // FIXME hardcoded value if (cleanUpCancelledKeys()) {
break; 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) { private static void write(SelectionKey k) {
write((NioDatagramChannel) k.attachment(), false); write((NioDatagramChannel) k.attachment(), false);
} }

View File

@ -242,7 +242,7 @@ class NioWorker implements Runnable {
} }
} }
private void processRegisterTaskQueue() { private void processRegisterTaskQueue() throws IOException {
for (;;) { for (;;) {
final Runnable task = registerTaskQueue.poll(); final Runnable task = registerTaskQueue.poll();
if (task == null) { if (task == null) {
@ -250,10 +250,11 @@ class NioWorker implements Runnable {
} }
task.run(); task.run();
cleanUpCancelledKeys();
} }
} }
private void processWriteTaskQueue() { private void processWriteTaskQueue() throws IOException {
for (;;) { for (;;) {
final Runnable task = writeTaskQueue.poll(); final Runnable task = writeTaskQueue.poll();
if (task == null) { if (task == null) {
@ -261,10 +262,11 @@ class NioWorker implements Runnable {
} }
task.run(); task.run();
cleanUpCancelledKeys();
} }
} }
private void processSelectedKeys(Set<SelectionKey> selectedKeys) { private void processSelectedKeys(Set<SelectionKey> selectedKeys) throws IOException {
for (Iterator<SelectionKey> i = selectedKeys.iterator(); i.hasNext();) { for (Iterator<SelectionKey> i = selectedKeys.iterator(); i.hasNext();) {
SelectionKey k = i.next(); SelectionKey k = i.next();
i.remove(); i.remove();
@ -283,11 +285,20 @@ class NioWorker implements Runnable {
close(k); close(k);
} }
if (cancelledKeys >= 128) { // FIXME hardcoded value if (cleanUpCancelledKeys()) {
break; 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) { private static boolean read(SelectionKey k) {
ScatteringByteChannel ch = (ScatteringByteChannel) k.channel(); ScatteringByteChannel ch = (ScatteringByteChannel) k.channel();