优化Response.completeInIOThread里的readRegister

This commit is contained in:
redkale
2023-06-27 17:55:02 +08:00
parent 5e6b2fb86e
commit 8c08112097
2 changed files with 23 additions and 17 deletions

View File

@@ -127,6 +127,7 @@ abstract class AsyncNioConnection extends AsyncConnection {
handler.failed(new NotYetConnectedException(), null); handler.failed(new NotYetConnectedException(), null);
return; return;
} }
if (handler != protocolCodec) {
if (this.readPending) { if (this.readPending) {
handler.failed(new ReadPendingException(), null); handler.failed(new ReadPendingException(), null);
return; return;
@@ -140,16 +141,21 @@ abstract class AsyncNioConnection extends AsyncConnection {
} else { } else {
this.readCompletionHandler = handler; this.readCompletionHandler = handler;
} }
} else {
this.readCompletionHandler = handler;
this.readPending = true;
}
try { try {
if (readKey == null) { if (readKey == null) {
ioReadThread.register(selector -> { ioReadThread.register(selector -> {
if (readKey == null) {
try { try {
readKey = implRegister(selector, SelectionKey.OP_READ); readKey = implRegister(selector, SelectionKey.OP_READ);
readKey.attach(this); readKey.attach(this);
} catch (ClosedChannelException e) { } catch (ClosedChannelException e) {
handleRead(0, e); handleRead(0, e);
} }
}
}); });
} else { } else {
ioReadThread.interestOpsOr(readKey, SelectionKey.OP_READ); ioReadThread.interestOpsOr(readKey, SelectionKey.OP_READ);

View File

@@ -306,7 +306,7 @@ public abstract class Response<C extends Context, R extends Request<C>> {
AsyncConnection conn = removeChannel(); AsyncConnection conn = removeChannel();
if (conn != null && conn.protocolCodec != null) { if (conn != null && conn.protocolCodec != null) {
this.responseConsumer.accept(this); this.responseConsumer.accept(this);
conn.readRegisterInIOThreadSafe(conn.protocolCodec); conn.readRegister(conn.protocolCodec);
} else { } else {
Supplier<Response> poolSupplier = this.responseSupplier; Supplier<Response> poolSupplier = this.responseSupplier;
Consumer<Response> poolConsumer = this.responseConsumer; Consumer<Response> poolConsumer = this.responseConsumer;