TcpAioAsyncConnection去掉队列写

This commit is contained in:
Redkale
2020-01-17 13:57:41 +08:00
parent f5f3c48f38
commit 0f52d32424

View File

@@ -24,7 +24,7 @@ import javax.net.ssl.SSLContext;
*/ */
public class TcpAioAsyncConnection extends AsyncConnection { public class TcpAioAsyncConnection extends AsyncConnection {
private final Semaphore semaphore = new Semaphore(1); //private final Semaphore semaphore = new Semaphore(1);
private int readTimeoutSeconds; private int readTimeoutSeconds;
@@ -103,35 +103,35 @@ public class TcpAioAsyncConnection extends AsyncConnection {
} }
} }
private <A> void nextWrite(Throwable exc, A attachment) { // private <A> void nextWrite(Throwable exc, A attachment) {
BlockingQueue<WriteEntry> queue = this.writeQueue; // BlockingQueue<WriteEntry> queue = this.writeQueue;
if (queue != null && exc != null && !isOpen()) { // if (queue != null && exc != null && !isOpen()) {
WriteEntry entry; // WriteEntry entry;
while ((entry = queue.poll()) != null) { // while ((entry = queue.poll()) != null) {
try { // try {
entry.writeHandler.failed(exc, entry.writeAttachment); // entry.writeHandler.failed(exc, entry.writeAttachment);
} catch (Throwable e) { // } catch (Throwable e) {
e.printStackTrace(System.err); // e.printStackTrace(System.err);
} // }
} // }
return; // return;
} // }
WriteEntry entry = queue == null ? null : queue.poll(); // WriteEntry entry = queue == null ? null : queue.poll();
//
if (entry != null) { // if (entry != null) {
try { // try {
if (entry.writeOneBuffer == null) { // if (entry.writeOneBuffer == null) {
write(false, entry.writeBuffers, entry.writeOffset, entry.writeLength, entry.writeAttachment, entry.writeHandler); // write(false, entry.writeBuffers, entry.writeOffset, entry.writeLength, entry.writeAttachment, entry.writeHandler);
} else { // } else {
write(false, entry.writeOneBuffer, entry.writeAttachment, entry.writeHandler); // write(false, entry.writeOneBuffer, entry.writeAttachment, entry.writeHandler);
} // }
} catch (Exception e) { // } catch (Exception e) {
entry.writeHandler.failed(e, entry.writeAttachment); // entry.writeHandler.failed(e, entry.writeAttachment);
} // }
} else { // } else {
semaphore.release(); // semaphore.release();
} // }
} // }
@Override @Override
public <A> void write(ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler) { public <A> void write(ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler) {
@@ -139,17 +139,17 @@ public class TcpAioAsyncConnection extends AsyncConnection {
} }
private <A> void write(boolean acquire, ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler) { private <A> void write(boolean acquire, ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler) {
if (acquire && !semaphore.tryAcquire()) { // if (acquire && !semaphore.tryAcquire()) {
if (this.writeQueue == null) { // if (this.writeQueue == null) {
synchronized (semaphore) { // synchronized (semaphore) {
if (this.writeQueue == null) { // if (this.writeQueue == null) {
this.writeQueue = new LinkedBlockingDeque<>(); // this.writeQueue = new LinkedBlockingDeque<>();
} // }
} // }
} // }
this.writeQueue.add(new WriteEntry(src, attachment, handler)); // this.writeQueue.add(new WriteEntry(src, attachment, handler));
return; // return;
} // }
WriteOneCompletionHandler newHandler = new WriteOneCompletionHandler(src, handler); WriteOneCompletionHandler newHandler = new WriteOneCompletionHandler(src, handler);
if (!channel.isOpen()) { if (!channel.isOpen()) {
newHandler.failed(new ClosedChannelException(), attachment); newHandler.failed(new ClosedChannelException(), attachment);
@@ -173,17 +173,17 @@ public class TcpAioAsyncConnection extends AsyncConnection {
} }
private <A> void write(boolean acquire, ByteBuffer[] srcs, int offset, int length, A attachment, final CompletionHandler<Integer, ? super A> handler) { private <A> void write(boolean acquire, ByteBuffer[] srcs, int offset, int length, A attachment, final CompletionHandler<Integer, ? super A> handler) {
if (acquire && !semaphore.tryAcquire()) { // if (acquire && !semaphore.tryAcquire()) {
if (this.writeQueue == null) { // if (this.writeQueue == null) {
synchronized (semaphore) { // synchronized (semaphore) {
if (this.writeQueue == null) { // if (this.writeQueue == null) {
this.writeQueue = new LinkedBlockingDeque<>(); // this.writeQueue = new LinkedBlockingDeque<>();
} // }
} // }
} // }
this.writeQueue.add(new WriteEntry(srcs, offset, length, attachment, handler)); // this.writeQueue.add(new WriteEntry(srcs, offset, length, attachment, handler));
return; // return;
} // }
WriteMoreCompletionHandler newHandler = new WriteMoreCompletionHandler(srcs, offset, length, handler); WriteMoreCompletionHandler newHandler = new WriteMoreCompletionHandler(srcs, offset, length, handler);
if (!channel.isOpen()) { if (!channel.isOpen()) {
newHandler.failed(new ClosedChannelException(), attachment); newHandler.failed(new ClosedChannelException(), attachment);
@@ -317,27 +317,27 @@ public class TcpAioAsyncConnection extends AsyncConnection {
failed(e, attachment); failed(e, attachment);
return; return;
} }
try { // try {
writeHandler.completed(writeCount, attachment); writeHandler.completed(writeCount, attachment);
} finally { // } finally {
nextWrite(null, attachment); // nextWrite(null, attachment);
} // }
} else { } else {
try { // try {
writeHandler.completed(result.intValue(), attachment); writeHandler.completed(result.intValue(), attachment);
} finally { // } finally {
nextWrite(null, attachment); // nextWrite(null, attachment);
} // }
} }
} }
@Override @Override
public void failed(Throwable exc, A attachment) { public void failed(Throwable exc, A attachment) {
try { // try {
writeHandler.failed(exc, attachment); writeHandler.failed(exc, attachment);
} finally { // } finally {
nextWrite(exc, attachment); // nextWrite(exc, attachment);
} // }
} }
} }
@@ -364,21 +364,21 @@ public class TcpAioAsyncConnection extends AsyncConnection {
failed(e, attachment); failed(e, attachment);
return; return;
} }
try { // try {
writeHandler.completed(result, attachment); writeHandler.completed(result, attachment);
} finally { // } finally {
nextWrite(null, attachment); // nextWrite(null, attachment);
} // }
} }
@Override @Override
public void failed(Throwable exc, A attachment) { public void failed(Throwable exc, A attachment) {
try { // try {
writeHandler.failed(exc, attachment); writeHandler.failed(exc, attachment);
} finally { // } finally {
nextWrite(exc, attachment); // nextWrite(exc, attachment);
} // }
} }
} }