udp优化

This commit is contained in:
redkale
2023-02-07 17:34:33 +08:00
parent 848f33bd2b
commit 530bf8078e
2 changed files with 18 additions and 9 deletions

View File

@@ -150,24 +150,30 @@ class AsyncNioUdpConnection extends AsyncNioConnection {
@Override
protected int implWrite(ByteBuffer src) throws IOException {
long now = System.currentTimeMillis();
//发送过频会丢包
if (clientMode) {
long now = System.currentTimeMillis();
if (this.writeTime + 1 > now) {
Utility.sleep(1);
Utility.sleep(1); //发送过频会丢包
this.writeTime = System.currentTimeMillis();
} else {
this.writeTime = now;
}
return this.channel.send(src, remoteAddress);
} else {
if (udpServerChannel.writeTime + 1 > now) {
Utility.sleep(1);
udpServerChannel.writeTime = System.currentTimeMillis();
} else {
udpServerChannel.writeTime = now;
udpServerChannel.writeLock.lock();
try {
long now = System.currentTimeMillis();
if (udpServerChannel.writeTime + 1 > now) {
Utility.sleep(1); //发送过频会丢包
udpServerChannel.writeTime = System.currentTimeMillis();
} else {
udpServerChannel.writeTime = now;
}
return this.channel.send(src, remoteAddress);
} finally {
udpServerChannel.writeLock.unlock();
}
return this.channel.send(src, remoteAddress);
}
}

View File

@@ -12,6 +12,7 @@ import java.nio.channels.*;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.LongAdder;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.*;
import java.util.logging.Level;
import org.redkale.boot.Application;
@@ -240,6 +241,8 @@ class AsyncNioUdpProtocolServer extends ProtocolServer {
ByteBufferPool unsafeBufferPool;
final ReentrantLock writeLock = new ReentrantLock();
volatile long writeTime;
ConcurrentHashMap<SocketAddress, AsyncNioUdpConnection> connections = new ConcurrentHashMap<>();