From 165e323fab58eba59dd45e9eb4d53aa74a937460 Mon Sep 17 00:00:00 2001 From: v2ray Date: Mon, 4 Jul 2016 13:37:42 +0200 Subject: [PATCH] refine sending window size --- transport/internet/kcp/config.go | 6 +++++- transport/internet/kcp/sending.go | 11 +++++++++-- transport/internet/kcp/sending_test.go | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/transport/internet/kcp/config.go b/transport/internet/kcp/config.go index 5e8345dd..e64674c3 100644 --- a/transport/internet/kcp/config.go +++ b/transport/internet/kcp/config.go @@ -14,7 +14,7 @@ func (this *Config) Apply() { effectiveConfig = *this } -func (this *Config) GetSendingWindowSize() uint32 { +func (this *Config) GetSendingInFlightSize() uint32 { size := this.UplinkCapacity * 1024 * 1024 / this.Mtu / (1000 / this.Tti) / 2 if size == 0 { size = 8 @@ -22,6 +22,10 @@ func (this *Config) GetSendingWindowSize() uint32 { return size } +func (this *Config) GetSendingWindowSize() uint32 { + return this.GetSendingInFlightSize() * 4 +} + func (this *Config) GetSendingQueueSize() uint32 { return this.WriteBuffer / this.Mtu } diff --git a/transport/internet/kcp/sending.go b/transport/internet/kcp/sending.go index e1c6fcb7..97cb3fbe 100644 --- a/transport/internet/kcp/sending.go +++ b/transport/internet/kcp/sending.go @@ -16,11 +16,12 @@ type SendingWindow struct { prev []uint32 next []uint32 + inFlightSize uint32 writer SegmentWriter onPacketLoss func(bool) } -func NewSendingWindow(size uint32, writer SegmentWriter, onPacketLoss func(bool)) *SendingWindow { +func NewSendingWindow(size uint32, inFlightSize uint32, writer SegmentWriter, onPacketLoss func(bool)) *SendingWindow { window := &SendingWindow{ start: 0, cap: size, @@ -31,6 +32,7 @@ func NewSendingWindow(size uint32, writer SegmentWriter, onPacketLoss func(bool) next: make([]uint32, size), writer: writer, onPacketLoss: onPacketLoss, + inFlightSize: inFlightSize, } return window } @@ -116,6 +118,7 @@ func (this *SendingWindow) Flush(current uint32, resend uint32, rto uint32) { } lost := false + var inFlightSize uint32 for i := this.start; ; i = this.next[i] { segment := this.data[i] @@ -139,6 +142,10 @@ func (this *SendingWindow) Flush(current uint32, resend uint32, rto uint32) { if needsend { this.writer.Write(segment) + inFlightSize++ + if inFlightSize >= this.inFlightSize { + break + } } if i == this.last { break @@ -230,7 +237,7 @@ func NewSendingWorker(kcp *KCP) *SendingWorker { windowSize: effectiveConfig.GetSendingWindowSize(), controlWindow: effectiveConfig.GetSendingWindowSize(), } - worker.window = NewSendingWindow(effectiveConfig.GetSendingWindowSize(), worker, worker.OnPacketLoss) + worker.window = NewSendingWindow(effectiveConfig.GetSendingWindowSize(), effectiveConfig.GetSendingInFlightSize(), worker, worker.OnPacketLoss) return worker } diff --git a/transport/internet/kcp/sending_test.go b/transport/internet/kcp/sending_test.go index 6e8709ad..39e9ea10 100644 --- a/transport/internet/kcp/sending_test.go +++ b/transport/internet/kcp/sending_test.go @@ -66,7 +66,7 @@ func TestSendingQueueClear(t *testing.T) { func TestSendingWindow(t *testing.T) { assert := assert.On(t) - window := NewSendingWindow(5, nil, nil) + window := NewSendingWindow(5, 5, nil, nil) window.Push(&DataSegment{ Number: 0, })