udp优化

This commit is contained in:
redkale
2023-02-07 14:55:58 +08:00
parent 7d2a9f6d94
commit 75469a49e8
2 changed files with 12 additions and 2 deletions

View File

@@ -129,6 +129,11 @@ class AsyncNioUdpConnection extends AsyncNioConnection {
}
int start = dst.position();
dst.put(buf);
if (buf.hasRemaining()) {
revbufferQueue.offerFirst(buf);
} else {
udpServerChannel.unsafeBufferPool.accept(buf);
}
return dst.position() - start;
}
}
@@ -178,7 +183,9 @@ class AsyncNioUdpConnection extends AsyncNioConnection {
public final void close() throws IOException {
super.close();
if (clientMode) {
channel.close(); //不能关闭channel
channel.close();
} else if (remoteAddress != null) {
udpServerChannel.connections.remove(remoteAddress);
}
if (this.connectKey != null) {
this.connectKey.cancel();

View File

@@ -120,6 +120,7 @@ class AsyncNioUdpProtocolServer extends ProtocolServer {
@Override
public void run() {
udpServerChannel.unsafeBufferPool = ObjectPool.createUnsafePool(Thread.currentThread(), 512, safeBufferPool);
final AsyncIOThread[] ioReadThreads = ioGroup.ioReadThreads;
final AsyncIOThread[] ioWriteThreads = ioGroup.ioWriteThreads;
final int reads = ioReadThreads.length;
@@ -129,7 +130,7 @@ class AsyncNioUdpProtocolServer extends ProtocolServer {
Set<SelectionKey> keys = null;
final Selector sel = selector;
final DatagramChannel serverChannel = udpServerChannel.serverChannel;
ObjectPool<ByteBuffer> unsafeBufferPool = ObjectPool.createUnsafePool(null, 512, safeBufferPool);
final ObjectPool<ByteBuffer> unsafeBufferPool = udpServerChannel.unsafeBufferPool;
while (!closed) {
try {
int count = sel.select();
@@ -237,6 +238,8 @@ class AsyncNioUdpProtocolServer extends ProtocolServer {
DatagramChannel serverChannel;
ObjectPool<ByteBuffer> unsafeBufferPool;
ConcurrentHashMap<SocketAddress, AsyncNioUdpConnection> connections = new ConcurrentHashMap<>();
public AsyncNioUdpServerChannel(DatagramChannel serverChannel) {