From c0040f20c3353b2aee95386960558f977f127e7c Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Sat, 5 Dec 2020 21:38:16 +0000 Subject: [PATCH] Use time.AfterFunc for session inactivity timeout to reduce goroutine count --- internal/multiplex/session.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/internal/multiplex/session.go b/internal/multiplex/session.go index 04198f3..d86fd4e 100644 --- a/internal/multiplex/session.go +++ b/internal/multiplex/session.go @@ -111,7 +111,7 @@ func MakeSession(id uint32, config SessionConfig) *Session { sesh.maxStreamUnitWrite = sesh.MsgOnWireSizeLimit - frameHeaderLength - sesh.Obfuscator.maxOverhead sesh.sb = makeSwitchboard(sesh) - go sesh.timeoutAfter(sesh.InactivityTimeout) + time.AfterFunc(sesh.InactivityTimeout, sesh.checkTimeout) return sesh } @@ -204,7 +204,7 @@ func (sesh *Session) closeStream(s *Stream, active bool) error { return sesh.Close() } else { log.Debugf("session %v has no active stream left", sesh.id) - go sesh.timeoutAfter(sesh.InactivityTimeout) + time.AfterFunc(sesh.InactivityTimeout, sesh.checkTimeout) } } return nil @@ -334,9 +334,7 @@ func (sesh *Session) IsClosed() bool { return atomic.LoadUint32(&sesh.closed) == 1 } -func (sesh *Session) timeoutAfter(to time.Duration) { - time.Sleep(to) - +func (sesh *Session) checkTimeout() { if sesh.streamCount() == 0 && !sesh.IsClosed() { sesh.SetTerminalMsg("timeout") sesh.Close()