From 967a5c401cc3d4ee9fa1e8caf25c3519a0f30947 Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Sat, 14 May 2016 13:28:18 +0200 Subject: [PATCH] merge protocol.MinMax into utils.MinMax --- congestion/cubic.go | 3 ++- congestion/cubic_sender.go | 6 +++--- protocol/minmax.go | 17 ----------------- protocol/minmax_test.go | 20 -------------------- utils/minmax.go | 22 +++++++++++++++++++++- utils/minmax_test.go | 11 +++++++++++ 6 files changed, 37 insertions(+), 42 deletions(-) delete mode 100644 protocol/minmax.go delete mode 100644 protocol/minmax_test.go diff --git a/congestion/cubic.go b/congestion/cubic.go index c13b9855..e6e8bf6e 100644 --- a/congestion/cubic.go +++ b/congestion/cubic.go @@ -5,6 +5,7 @@ import ( "time" "github.com/lucas-clemente/quic-go/protocol" + "github.com/lucas-clemente/quic-go/utils" ) // This cubic implementation is based on the one found in Chromiums's QUIC @@ -148,7 +149,7 @@ func (c *Cubic) CongestionWindowAfterAck(currentCongestionWindow protocol.Packet // Cubic is "independent" of RTT, the update is limited by the time elapsed. if c.lastCongestionWindow == currentCongestionWindow && (currentTime.Sub(c.lastUpdateTime) <= maxCubicTimeInterval) { - return protocol.MaxPacketNumber(c.lastTargetCongestionWindow, c.estimatedTCPcongestionWindow) + return utils.MaxPacketNumber(c.lastTargetCongestionWindow, c.estimatedTCPcongestionWindow) } c.lastCongestionWindow = currentCongestionWindow c.lastUpdateTime = currentTime diff --git a/congestion/cubic_sender.go b/congestion/cubic_sender.go index b25a83b9..8e8285c1 100644 --- a/congestion/cubic_sender.go +++ b/congestion/cubic_sender.go @@ -144,7 +144,7 @@ func (c *cubicSender) OnCongestionEvent(rttUpdated bool, bytesInFlight protocol. } func (c *cubicSender) onPacketAcked(ackedPacketNumber protocol.PacketNumber, ackedBytes protocol.ByteCount, bytesInFlight protocol.ByteCount) { - c.largestAckedPacketNumber = protocol.MaxPacketNumber(ackedPacketNumber, c.largestAckedPacketNumber) + c.largestAckedPacketNumber = utils.MaxPacketNumber(ackedPacketNumber, c.largestAckedPacketNumber) if c.InRecovery() { // PRR is used when in recovery. c.prr.OnPacketAcked(ackedBytes) @@ -166,7 +166,7 @@ func (c *cubicSender) onPacketLost(packetNumber protocol.PacketNumber, lostBytes if c.slowStartLargeReduction { if c.stats.slowstartPacketsLost == 1 || (c.stats.slowstartBytesLost/protocol.DefaultTCPMSS) > (c.stats.slowstartBytesLost-lostBytes)/protocol.DefaultTCPMSS { // Reduce congestion window by 1 for every mss of bytes lost. - c.congestionWindow = protocol.MaxPacketNumber(c.congestionWindow-1, c.minCongestionWindow) + c.congestionWindow = utils.MaxPacketNumber(c.congestionWindow-1, c.minCongestionWindow) } c.slowstartThreshold = c.congestionWindow } @@ -234,7 +234,7 @@ func (c *cubicSender) maybeIncreaseCwnd(ackedPacketNumber protocol.PacketNumber, c.congestionWindowCount = 0 } } else { - c.congestionWindow = protocol.MinPacketNumber(c.maxTCPCongestionWindow, c.cubic.CongestionWindowAfterAck(c.congestionWindow, c.rttStats.MinRTT())) + c.congestionWindow = utils.MinPacketNumber(c.maxTCPCongestionWindow, c.cubic.CongestionWindowAfterAck(c.congestionWindow, c.rttStats.MinRTT())) } } diff --git a/protocol/minmax.go b/protocol/minmax.go deleted file mode 100644 index 46c26292..00000000 --- a/protocol/minmax.go +++ /dev/null @@ -1,17 +0,0 @@ -package protocol - -// MaxPacketNumber returns the max packet number -func MaxPacketNumber(a, b PacketNumber) PacketNumber { - if a > b { - return a - } - return b -} - -// MinPacketNumber returns the min packet number -func MinPacketNumber(a, b PacketNumber) PacketNumber { - if a < b { - return a - } - return b -} diff --git a/protocol/minmax_test.go b/protocol/minmax_test.go deleted file mode 100644 index c9fe6880..00000000 --- a/protocol/minmax_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package protocol_test - -import ( - "github.com/lucas-clemente/quic-go/protocol" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = Describe("minmax", func() { - It("calculates packet number max", func() { - Expect(protocol.MaxPacketNumber(1, 2)).To(Equal(protocol.PacketNumber(2))) - Expect(protocol.MaxPacketNumber(2, 1)).To(Equal(protocol.PacketNumber(2))) - }) - - It("calculates packet number min", func() { - Expect(protocol.MinPacketNumber(1, 2)).To(Equal(protocol.PacketNumber(1))) - Expect(protocol.MinPacketNumber(2, 1)).To(Equal(protocol.PacketNumber(1))) - }) -}) diff --git a/utils/minmax.go b/utils/minmax.go index 370cb192..1e9093f2 100644 --- a/utils/minmax.go +++ b/utils/minmax.go @@ -1,6 +1,10 @@ package utils -import "time" +import ( + "time" + + "github.com/lucas-clemente/quic-go/protocol" +) // Max returns the maximum of two Ints func Max(a, b int) int { @@ -81,3 +85,19 @@ func AbsDuration(d time.Duration) time.Duration { } return -d } + +// MaxPacketNumber returns the max packet number +func MaxPacketNumber(a, b protocol.PacketNumber) protocol.PacketNumber { + if a > b { + return a + } + return b +} + +// MinPacketNumber returns the min packet number +func MinPacketNumber(a, b protocol.PacketNumber) protocol.PacketNumber { + if a < b { + return a + } + return b +} diff --git a/utils/minmax_test.go b/utils/minmax_test.go index bbd3225c..5ca7b60e 100644 --- a/utils/minmax_test.go +++ b/utils/minmax_test.go @@ -3,6 +3,7 @@ package utils import ( "time" + "github.com/lucas-clemente/quic-go/protocol" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) @@ -38,6 +39,11 @@ var _ = Describe("Min / Max", func() { Expect(MinDuration(time.Microsecond, time.Nanosecond)).To(Equal(time.Nanosecond)) Expect(MinDuration(time.Nanosecond, time.Microsecond)).To(Equal(time.Nanosecond)) }) + + It("returns packet number max", func() { + Expect(MaxPacketNumber(1, 2)).To(Equal(protocol.PacketNumber(2))) + Expect(MaxPacketNumber(2, 1)).To(Equal(protocol.PacketNumber(2))) + }) }) Context("Min", func() { @@ -55,6 +61,11 @@ var _ = Describe("Min / Max", func() { Expect(MinInt64(7, 5)).To(Equal(int64(5))) Expect(MinInt64(5, 7)).To(Equal(int64(5))) }) + + It("returns packet number min", func() { + Expect(MinPacketNumber(1, 2)).To(Equal(protocol.PacketNumber(1))) + Expect(MinPacketNumber(2, 1)).To(Equal(protocol.PacketNumber(1))) + }) }) It("returns the abs time", func() {