Skip to content

Commit 77eb080

Browse files
authored
Merge pull request #101 from liggitt/close-leak
Avoid leaking goroutines on close
2 parents 93aa56f + 3f1023d commit 77eb080

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

connection.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,9 @@ func (s *Connection) shutdown(closeTimeout time.Duration) {
712712

713713
var timeout <-chan time.Time
714714
if closeTimeout > time.Duration(0) {
715-
timeout = time.After(closeTimeout)
715+
timer := time.NewTimer(closeTimeout)
716+
defer timer.Stop()
717+
timeout = timer.C
716718
}
717719
streamsClosed := make(chan bool)
718720

@@ -739,7 +741,15 @@ func (s *Connection) shutdown(closeTimeout time.Duration) {
739741
}
740742

741743
if err != nil {
742-
duration := 10 * time.Minute
744+
// default to 1 second
745+
duration := time.Second
746+
// if a closeTimeout was given, use that, clipped to 1s-10m
747+
if closeTimeout > time.Second {
748+
duration = closeTimeout
749+
}
750+
if duration > 10*time.Minute {
751+
duration = 10 * time.Minute
752+
}
743753
timer := time.NewTimer(duration)
744754
defer timer.Stop()
745755
select {
@@ -806,7 +816,9 @@ func (s *Connection) CloseWait() error {
806816
func (s *Connection) Wait(waitTimeout time.Duration) error {
807817
var timeout <-chan time.Time
808818
if waitTimeout > time.Duration(0) {
809-
timeout = time.After(waitTimeout)
819+
timer := time.NewTimer(waitTimeout)
820+
defer timer.Stop()
821+
timeout = timer.C
810822
}
811823

812824
select {

0 commit comments

Comments
 (0)