refactor timer calculation in sesssion to reduce syscalls

This commit is contained in:
Lucas Clemente
2016-05-24 17:42:14 +02:00
parent 55fe1390b3
commit d4a189b2d1
6 changed files with 49 additions and 44 deletions

View File

@@ -21,7 +21,7 @@ type SentPacketHandler interface {
CongestionAllowsSending() bool
CheckForError() error
TimeToFirstRTO() time.Duration
TimeOfFirstRTO() time.Time
}
// ReceivedPacketHandler handles ACKs needed to send for incoming packets

View File

@@ -313,17 +313,15 @@ func (h *sentPacketHandler) queuePacketsRTO() {
}
}
func (h *sentPacketHandler) TimeToFirstRTO() time.Duration {
now := time.Now()
min := utils.InfDuration
func (h *sentPacketHandler) TimeOfFirstRTO() time.Time {
var min time.Time
for _, p := range h.packetHistory {
if p == nil || p.Retransmitted {
continue
}
if now.After(p.rtoTime) {
return 0
if min.IsZero() || min.After(p.rtoTime) {
min = p.rtoTime
}
min = utils.MinDuration(min, p.rtoTime.Sub(now))
}
return min
}

View File

@@ -646,27 +646,20 @@ var _ = Describe("SentPacketHandler", func() {
Context("RTO retransmission", func() {
Context("calculating the time to first RTO", func() {
It("defaults to inf", func() {
Expect(handler.TimeToFirstRTO()).To(Equal(utils.InfDuration))
It("defaults to zero", func() {
Expect(handler.TimeOfFirstRTO().IsZero()).To(BeTrue())
})
It("returns time to RTO", func() {
err := handler.SentPacket(&Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1})
Expect(err).NotTo(HaveOccurred())
Expect(handler.TimeToFirstRTO()).To(BeNumerically("~", protocol.DefaultRetransmissionTime, time.Millisecond))
})
It("returns 0 when RTOs are required", func() {
err := handler.SentPacket(&Packet{PacketNumber: 1, Frames: []frames.Frame{}, Length: 1})
Expect(err).NotTo(HaveOccurred())
handler.packetHistory[1].rtoTime = time.Now().Add(-time.Second)
Expect(handler.TimeToFirstRTO()).To(BeZero())
Expect(handler.TimeOfFirstRTO().Sub(time.Now())).To(BeNumerically("~", protocol.DefaultRetransmissionTime, time.Millisecond))
})
It("ignores nil packets", func() {
handler.packetHistory[1] = nil
handler.queuePacketsRTO()
Expect(handler.TimeToFirstRTO()).To(Equal(utils.InfDuration))
Expect(handler.TimeOfFirstRTO().IsZero()).To(BeTrue())
})
})