Use time.AfterFunc for session inactivity timeout to reduce goroutine count

This commit is contained in:
Andy Wang 2020-12-05 21:38:16 +00:00
parent a3520c1018
commit c0040f20c3
No known key found for this signature in database
GPG Key ID: 181B49F9F38F3374
1 changed files with 3 additions and 5 deletions

View File

@ -111,7 +111,7 @@ func MakeSession(id uint32, config SessionConfig) *Session {
sesh.maxStreamUnitWrite = sesh.MsgOnWireSizeLimit - frameHeaderLength - sesh.Obfuscator.maxOverhead sesh.maxStreamUnitWrite = sesh.MsgOnWireSizeLimit - frameHeaderLength - sesh.Obfuscator.maxOverhead
sesh.sb = makeSwitchboard(sesh) sesh.sb = makeSwitchboard(sesh)
go sesh.timeoutAfter(sesh.InactivityTimeout) time.AfterFunc(sesh.InactivityTimeout, sesh.checkTimeout)
return sesh return sesh
} }
@ -204,7 +204,7 @@ func (sesh *Session) closeStream(s *Stream, active bool) error {
return sesh.Close() return sesh.Close()
} else { } else {
log.Debugf("session %v has no active stream left", sesh.id) log.Debugf("session %v has no active stream left", sesh.id)
go sesh.timeoutAfter(sesh.InactivityTimeout) time.AfterFunc(sesh.InactivityTimeout, sesh.checkTimeout)
} }
} }
return nil return nil
@ -334,9 +334,7 @@ func (sesh *Session) IsClosed() bool {
return atomic.LoadUint32(&sesh.closed) == 1 return atomic.LoadUint32(&sesh.closed) == 1
} }
func (sesh *Session) timeoutAfter(to time.Duration) { func (sesh *Session) checkTimeout() {
time.Sleep(to)
if sesh.streamCount() == 0 && !sesh.IsClosed() { if sesh.streamCount() == 0 && !sesh.IsClosed() {
sesh.SetTerminalMsg("timeout") sesh.SetTerminalMsg("timeout")
sesh.Close() sesh.Close()