diff --git a/src/main/java/org/redkale/net/AsyncNioTcpProtocolServer.java b/src/main/java/org/redkale/net/AsyncNioTcpProtocolServer.java index 778287f15..5bb9122de 100644 --- a/src/main/java/org/redkale/net/AsyncNioTcpProtocolServer.java +++ b/src/main/java/org/redkale/net/AsyncNioTcpProtocolServer.java @@ -9,7 +9,7 @@ import java.io.IOException; import java.net.*; import java.nio.ByteBuffer; import java.nio.channels.*; -import java.util.*; +import java.util.Set; import java.util.concurrent.atomic.LongAdder; import java.util.function.*; import java.util.logging.Level; @@ -133,17 +133,17 @@ class AsyncNioTcpProtocolServer extends ProtocolServer { final AsyncIOThread[] ioWriteThreads = ioGroup.ioWriteThreads; int threads = ioReadThreads.length; int threadIndex = -1; + Set keys = null; while (!closed) { try { int count = selector.select(); if (count == 0) { continue; } - Set keys = selector.selectedKeys(); - Iterator it = keys.iterator(); - while (it.hasNext()) { - SelectionKey key = it.next(); - it.remove(); + if (keys == null) { + keys = selector.selectedKeys(); + } + for (SelectionKey key : keys) { if (key.isAcceptable()) { if (++threadIndex >= threads) { threadIndex = 0; @@ -151,6 +151,7 @@ class AsyncNioTcpProtocolServer extends ProtocolServer { accept(key, ioReadThreads[threadIndex], ioWriteThreads[threadIndex]); } } + keys.clear(); } catch (Throwable t) { server.logger.log(Level.SEVERE, "server accept error", t); } diff --git a/src/main/java/org/redkale/net/ProtocolCodec.java b/src/main/java/org/redkale/net/ProtocolCodec.java index 6d1494fb7..fecbee54a 100644 --- a/src/main/java/org/redkale/net/ProtocolCodec.java +++ b/src/main/java/org/redkale/net/ProtocolCodec.java @@ -18,14 +18,14 @@ import java.util.logging.Level; */ class ProtocolCodec implements CompletionHandler { - private final AsyncConnection channel; - private final Context context; private final Supplier responseSupplier; private final Consumer responseConsumer; + private AsyncConnection channel; + private Response resp; public ProtocolCodec(Context context, Supplier responseSupplier, @@ -36,6 +36,15 @@ class ProtocolCodec implements CompletionHandler { this.responseConsumer = responseConsumer; } + public void prepare() { + } + + protected boolean recycle() { + this.channel = null; + this.resp = null; + return true; + } + public ProtocolCodec response(Response resp) { this.resp = resp; return this;