ProtocolCodec优化
This commit is contained in:
@@ -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<SelectionKey> keys = null;
|
||||
while (!closed) {
|
||||
try {
|
||||
int count = selector.select();
|
||||
if (count == 0) {
|
||||
continue;
|
||||
}
|
||||
Set<SelectionKey> keys = selector.selectedKeys();
|
||||
Iterator<SelectionKey> 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);
|
||||
}
|
||||
|
||||
@@ -18,14 +18,14 @@ import java.util.logging.Level;
|
||||
*/
|
||||
class ProtocolCodec implements CompletionHandler<Integer, ByteBuffer> {
|
||||
|
||||
private final AsyncConnection channel;
|
||||
|
||||
private final Context context;
|
||||
|
||||
private final Supplier<Response> responseSupplier;
|
||||
|
||||
private final Consumer<Response> responseConsumer;
|
||||
|
||||
private AsyncConnection channel;
|
||||
|
||||
private Response resp;
|
||||
|
||||
public ProtocolCodec(Context context, Supplier<Response> responseSupplier,
|
||||
@@ -36,6 +36,15 @@ class ProtocolCodec implements CompletionHandler<Integer, ByteBuffer> {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user