* Fixed a potential issue where ClosedChannelException is thrown unnecessarilly

* Fixed a potential performance issue where an unused exception is created when a channel is closed
This commit is contained in:
Trustin Lee 2009-04-26 04:04:05 +00:00
parent 120db66aca
commit cd35c1d130

View File

@ -399,6 +399,7 @@ class NioWorker implements Runnable {
if (bufIdx == buf.writerIndex()) { if (bufIdx == buf.writerIndex()) {
// Successful write - proceed to the next message. // Successful write - proceed to the next message.
channel.currentWriteEvent = null;
evt.getFuture().setSuccess(); evt.getFuture().setSuccess();
evt = null; evt = null;
} else { } else {
@ -527,14 +528,7 @@ class NioWorker implements Runnable {
} }
private static void cleanUpWriteBuffer(NioSocketChannel channel) { private static void cleanUpWriteBuffer(NioSocketChannel channel) {
// Create the exception only once to avoid the excessive overhead Exception cause = null;
// caused by fillStackTrace.
Exception cause;
if (channel.isOpen()) {
cause = new NotYetConnectedException();
} else {
cause = new ClosedChannelException();
}
// Clean up the stale messages in the write buffer. // Clean up the stale messages in the write buffer.
synchronized (channel.writeLock) { synchronized (channel.writeLock) {
@ -542,18 +536,39 @@ class NioWorker implements Runnable {
if (evt != null) { if (evt != null) {
channel.currentWriteEvent = null; channel.currentWriteEvent = null;
channel.currentWriteIndex = 0; channel.currentWriteIndex = 0;
// Create the exception only once to avoid the excessive overhead
// caused by fillStackTrace.
if (channel.isOpen()) {
cause = new NotYetConnectedException();
} else {
cause = new ClosedChannelException();
}
evt.getFuture().setFailure(cause); evt.getFuture().setFailure(cause);
fireExceptionCaught(channel, cause); fireExceptionCaught(channel, cause);
} }
Queue<MessageEvent> writeBuffer = channel.writeBuffer; Queue<MessageEvent> writeBuffer = channel.writeBuffer;
for (;;) { if (!writeBuffer.isEmpty()) {
evt = writeBuffer.poll(); // Create the exception only once to avoid the excessive overhead
if (evt == null) { // caused by fillStackTrace.
break; if (cause == null) {
if (channel.isOpen()) {
cause = new NotYetConnectedException();
} else {
cause = new ClosedChannelException();
}
}
for (;;) {
evt = writeBuffer.poll();
if (evt == null) {
break;
}
evt.getFuture().setFailure(cause);
fireExceptionCaught(channel, cause);
} }
evt.getFuture().setFailure(cause);
fireExceptionCaught(channel, cause);
} }
} }
} }