Reduce VPN control packet latency

This commit is contained in:
2026-05-15 18:19:55 +03:00
parent ceda460d09
commit 52dfce316d
7 changed files with 194 additions and 15 deletions
@@ -51,6 +51,7 @@ const (
vpnGatewayBatchMaxPackets = 2048
vpnGatewayBatchMaxBytes = 8 * 1024 * 1024
vpnGatewayBatchFlushTimeout = 3 * time.Millisecond
vpnGatewayPriorityBatchWait = 20 * time.Millisecond
)
type readWriteCloser interface {
@@ -350,9 +351,34 @@ func (g *Gateway) uploadGatewayPackets(ctx context.Context, priorityPackets <-ch
}
flushPriority := func(packet []byte) {
flush()
if addPacket(packet) {
flush()
if !addPacket(packet) {
return
}
deadline := time.Now().Add(vpnGatewayPriorityBatchWait)
for len(batch) < vpnGatewayBatchMaxPackets && batchBytes < vpnGatewayBatchMaxBytes {
wait := time.Until(deadline)
if wait <= 0 {
break
}
timer := time.NewTimer(wait)
select {
case next := <-priorityPackets:
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
if !addPacket(next) {
flush()
_ = addPacket(next)
}
case <-timer.C:
flush()
return
}
}
flush()
}
for {
if len(batch) == 0 && timerActive {