diff --git a/connection.go b/connection.go index 91be88e01..c4a36c5cb 100644 --- a/connection.go +++ b/connection.go @@ -706,10 +706,10 @@ func (s *connection) nextKeepAliveTime() time.Time { func (s *connection) maybeResetTimer() { var deadline time.Time if !s.handshakeComplete { - deadline = utils.MinTime( - s.creationTime.Add(s.config.handshakeTimeout()), - s.idleTimeoutStartTime().Add(s.config.HandshakeIdleTimeout), - ) + deadline = s.creationTime.Add(s.config.handshakeTimeout()) + if t := s.idleTimeoutStartTime().Add(s.config.HandshakeIdleTimeout); t.Before(deadline) { + deadline = t + } } else { if keepAliveTime := s.nextKeepAliveTime(); !keepAliveTime.IsZero() { deadline = keepAliveTime @@ -727,7 +727,11 @@ func (s *connection) maybeResetTimer() { } func (s *connection) idleTimeoutStartTime() time.Time { - return utils.MaxTime(s.lastPacketReceivedTime, s.firstAckElicitingPacketAfterIdleSentTime) + startTime := s.lastPacketReceivedTime + if t := s.firstAckElicitingPacketAfterIdleSentTime; t.After(startTime) { + startTime = t + } + return startTime } func (s *connection) handleHandshakeComplete() error { @@ -1767,7 +1771,10 @@ func (s *connection) checkTransportParameters(params *wire.TransportParameters) func (s *connection) applyTransportParameters() { params := s.peerParams // Our local idle timeout will always be > 0. - s.idleTimeout = utils.MinNonZeroDuration(s.config.MaxIdleTimeout, params.MaxIdleTimeout) + s.idleTimeout = s.config.MaxIdleTimeout + if s.idleTimeout > 0 && params.MaxIdleTimeout < s.idleTimeout { + s.idleTimeout = params.MaxIdleTimeout + } s.keepAliveInterval = min(s.config.KeepAlivePeriod, min(s.idleTimeout/2, protocol.MaxKeepAliveInterval)) s.streamsMap.UpdateLimits(params) s.frameParser.SetAckDelayExponent(params.AckDelayExponent) diff --git a/integrationtests/self/timeout_test.go b/integrationtests/self/timeout_test.go index da4c6883b..9e1dc4b33 100644 --- a/integrationtests/self/timeout_test.go +++ b/integrationtests/self/timeout_test.go @@ -11,7 +11,6 @@ import ( "github.com/quic-go/quic-go" quicproxy "github.com/quic-go/quic-go/integrationtests/tools/proxy" - "github.com/quic-go/quic-go/internal/utils" "github.com/quic-go/quic-go/logging" . "github.com/onsi/ginkgo/v2" @@ -235,7 +234,7 @@ var _ = Describe("Timeout tests", func() { // We're ignoring here that only the first ack-eliciting packet sent resets the idle timeout. // This is ok since we're dealing with a lossless connection here, // and we'd expect to receive an ACK for additional other ack-eliciting packet sent. - Expect(time.Since(utils.MaxTime(lastAckElicitingPacketSentAt, lastPacketRcvdAt))).To(And( + Expect(max(time.Since(lastAckElicitingPacketSentAt), time.Since(lastPacketRcvdAt))).To(And( BeNumerically(">=", idleTimeout), BeNumerically("<", idleTimeout*6/5), )) diff --git a/internal/congestion/cubic_sender_test.go b/internal/congestion/cubic_sender_test.go index 785655004..d2b2f213d 100644 --- a/internal/congestion/cubic_sender_test.go +++ b/internal/congestion/cubic_sender_test.go @@ -116,7 +116,7 @@ var _ = Describe("Cubic Sender", func() { AckNPackets(1) delay := sender.TimeUntilSend(bytesInFlight) Expect(delay).ToNot(BeZero()) - Expect(delay).ToNot(Equal(utils.InfDuration)) + Expect(delay.Sub(clock.Now())).To(BeNumerically("<", time.Hour)) }) It("application limited slow start", func() { diff --git a/internal/utils/minmax.go b/internal/utils/minmax.go deleted file mode 100644 index 03a9c9a87..000000000 --- a/internal/utils/minmax.go +++ /dev/null @@ -1,36 +0,0 @@ -package utils - -import ( - "math" - "time" -) - -// InfDuration is a duration of infinite length -const InfDuration = time.Duration(math.MaxInt64) - -// MinNonZeroDuration return the minimum duration that's not zero. -func MinNonZeroDuration(a, b time.Duration) time.Duration { - if a == 0 { - return b - } - if b == 0 { - return a - } - return min(a, b) -} - -// MinTime returns the earlier time -func MinTime(a, b time.Time) time.Time { - if a.After(b) { - return b - } - return a -} - -// MaxTime returns the later time -func MaxTime(a, b time.Time) time.Time { - if a.After(b) { - return a - } - return b -} diff --git a/internal/utils/minmax_test.go b/internal/utils/minmax_test.go deleted file mode 100644 index 3d6485025..000000000 --- a/internal/utils/minmax_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package utils - -import ( - "time" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -var _ = Describe("Min / Max", func() { - It("returns the maximum time", func() { - a := time.Now() - b := a.Add(time.Second) - Expect(MaxTime(a, b)).To(Equal(b)) - Expect(MaxTime(b, a)).To(Equal(b)) - }) - - It("returns the minimum duration", func() { - a := time.Now() - b := a.Add(time.Second) - Expect(MinTime(a, b)).To(Equal(a)) - Expect(MinTime(b, a)).To(Equal(a)) - }) - - It("returns the minium non-zero duration", func() { - var a time.Duration - b := time.Second - Expect(MinNonZeroDuration(0, 0)).To(BeZero()) - Expect(MinNonZeroDuration(a, b)).To(Equal(b)) - Expect(MinNonZeroDuration(b, a)).To(Equal(b)) - Expect(MinNonZeroDuration(time.Minute, time.Hour)).To(Equal(time.Minute)) - }) -}) diff --git a/internal/utils/rtt_stats.go b/internal/utils/rtt_stats.go index 463b95424..24e1f64c9 100644 --- a/internal/utils/rtt_stats.go +++ b/internal/utils/rtt_stats.go @@ -64,7 +64,7 @@ func (r *RTTStats) PTO(includeMaxAckDelay bool) time.Duration { // UpdateRTT updates the RTT based on a new sample. func (r *RTTStats) UpdateRTT(sendDelta, ackDelay time.Duration, now time.Time) { - if sendDelta == InfDuration || sendDelta <= 0 { + if sendDelta <= 0 { return } diff --git a/internal/utils/rtt_stats_test.go b/internal/utils/rtt_stats_test.go index eae31926b..b4a7c3c54 100644 --- a/internal/utils/rtt_stats_test.go +++ b/internal/utils/rtt_stats_test.go @@ -101,25 +101,17 @@ var _ = Describe("RTT stats", func() { }) It("UpdateRTTWithBadSendDeltas", func() { - // Make sure we ignore bad RTTs. - // base::test::MockLog log; - - initialRtt := (10 * time.Millisecond) + initialRtt := 10 * time.Millisecond rttStats.UpdateRTT(initialRtt, 0, time.Time{}) Expect(rttStats.MinRTT()).To(Equal(initialRtt)) Expect(rttStats.SmoothedRTT()).To(Equal(initialRtt)) badSendDeltas := []time.Duration{ 0, - InfDuration, -1000 * time.Microsecond, } - // log.StartCapturingLogs(); for _, badSendDelta := range badSendDeltas { - // SCOPED_TRACE(Message() << "bad_send_delta = " - // << bad_send_delta.ToMicroseconds()); - // EXPECT_CALL(log, Log(LOG_WARNING, _, _, _, HasSubstr("Ignoring"))); rttStats.UpdateRTT(badSendDelta, 0, time.Time{}) Expect(rttStats.MinRTT()).To(Equal(initialRtt)) Expect(rttStats.SmoothedRTT()).To(Equal(initialRtt)) diff --git a/internal/wire/ack_frame.go b/internal/wire/ack_frame.go index e0f2db3c7..8befef4f2 100644 --- a/internal/wire/ack_frame.go +++ b/internal/wire/ack_frame.go @@ -2,11 +2,11 @@ package wire import ( "errors" + "math" "sort" "time" "github.com/quic-go/quic-go/internal/protocol" - "github.com/quic-go/quic-go/internal/utils" "github.com/quic-go/quic-go/quicvarint" ) @@ -40,7 +40,7 @@ func parseAckFrame(frame *AckFrame, b []byte, typ uint64, ackDelayExponent uint8 delayTime := time.Duration(delay*1<