merge protocol.MinMax into utils.MinMax

This commit is contained in:
Lucas Clemente
2016-05-14 13:28:18 +02:00
parent 40acb8e9b5
commit 967a5c401c
6 changed files with 37 additions and 42 deletions

View File

@@ -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

View File

@@ -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()))
}
}

View File

@@ -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
}

View File

@@ -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)))
})
})

View File

@@ -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
}

View File

@@ -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() {