forked from quic-go/quic-go
Merge pull request #1680 from lucas-clemente/stream-deadlock
fix a deadlock when setting read and write deadlines
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package utils
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A Timer wrapper that behaves correctly when resetting
|
||||
type Timer struct {
|
||||
@@ -11,7 +14,7 @@ type Timer struct {
|
||||
|
||||
// NewTimer creates a new timer that is not set
|
||||
func NewTimer() *Timer {
|
||||
return &Timer{t: time.NewTimer(0)}
|
||||
return &Timer{t: time.NewTimer(time.Duration(math.MaxInt64))}
|
||||
}
|
||||
|
||||
// Chan returns the channel of the wrapped timer
|
||||
@@ -31,7 +34,9 @@ func (t *Timer) Reset(deadline time.Time) {
|
||||
if !t.t.Stop() && !t.read {
|
||||
<-t.t.C
|
||||
}
|
||||
t.t.Reset(time.Until(deadline))
|
||||
if !deadline.IsZero() {
|
||||
t.t.Reset(time.Until(deadline))
|
||||
}
|
||||
|
||||
t.read = false
|
||||
t.deadline = deadline
|
||||
|
||||
@@ -10,6 +10,11 @@ import (
|
||||
var _ = Describe("Timer", func() {
|
||||
const d = 10 * time.Millisecond
|
||||
|
||||
It("doesn't fire a newly created timer", func() {
|
||||
t := NewTimer()
|
||||
Consistently(t.Chan()).ShouldNot(Receive())
|
||||
})
|
||||
|
||||
It("works", func() {
|
||||
t := NewTimer()
|
||||
t.Reset(time.Now().Add(d))
|
||||
@@ -49,6 +54,12 @@ var _ = Describe("Timer", func() {
|
||||
Eventually(t.Chan()).Should(Receive())
|
||||
})
|
||||
|
||||
It("doesn't set a timer if the deadline is the zero value", func() {
|
||||
t := NewTimer()
|
||||
t.Reset(time.Time{})
|
||||
Consistently(t.Chan()).ShouldNot(Receive())
|
||||
})
|
||||
|
||||
It("fires the timer twice, if reset to the same deadline", func() {
|
||||
deadline := time.Now().Add(-time.Millisecond)
|
||||
t := NewTimer()
|
||||
|
||||
Reference in New Issue
Block a user