优化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,28 +127,34 @@ abstract class AsyncNioConnection extends AsyncConnection {
handler.failed(new NotYetConnectedException(), null); handler.failed(new NotYetConnectedException(), null);
return; return;
} }
if (this.readPending) { if (handler != protocolCodec) {
handler.failed(new ReadPendingException(), null); if (this.readPending) {
return; handler.failed(new ReadPendingException(), null);
} return;
this.readPending = true; }
if (this.readTimeoutSeconds > 0) { this.readPending = true;
AsyncNioCompletionHandler newHandler = this.readTimeoutCompletionHandler; if (this.readTimeoutSeconds > 0) {
newHandler.handler(handler, this.readByteBuffer); // new AsyncNioCompletionHandler(handler, this.readByteBuffer); AsyncNioCompletionHandler newHandler = this.readTimeoutCompletionHandler;
this.readCompletionHandler = newHandler; newHandler.handler(handler, this.readByteBuffer); // new AsyncNioCompletionHandler(handler, this.readByteBuffer);
newHandler.timeoutFuture = ioGroup.scheduleTimeout(newHandler, this.readTimeoutSeconds, TimeUnit.SECONDS); this.readCompletionHandler = newHandler;
newHandler.timeoutFuture = ioGroup.scheduleTimeout(newHandler, this.readTimeoutSeconds, TimeUnit.SECONDS);
} else {
this.readCompletionHandler = handler;
}
} else { } else {
this.readCompletionHandler = handler; this.readCompletionHandler = handler;
this.readPending = true;
} }
try { try {
if (readKey == null) { if (readKey == null) {
ioReadThread.register(selector -> { ioReadThread.register(selector -> {
try { if (readKey == null) {
readKey = implRegister(selector, SelectionKey.OP_READ); try {
readKey.attach(this); readKey = implRegister(selector, SelectionKey.OP_READ);
} catch (ClosedChannelException e) { readKey.attach(this);
handleRead(0, e); } catch (ClosedChannelException e) {
handleRead(0, e);
}
} }
}); });
} else { } else {

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;