Add keepalive support for clients in quic.Config

Fixes #525.
This commit is contained in:
krish7919 (Krish)
2017-05-05 15:39:20 +02:00
committed by Lucas Clemente
parent 649933d17b
commit 755dedf1ff
6 changed files with 44 additions and 1 deletions

View File

@@ -111,6 +111,9 @@ type session struct {
lastNetworkActivityTime time.Time
timer *utils.Timer
// keepAlivePingSent stores whether a Ping frame was sent to the peer or not
// it is reset as soon as we receive a packet from the peer
keepAlivePingSent bool
}
var _ Session = &session{}
@@ -302,6 +305,12 @@ runLoop:
s.sentPacketHandler.OnAlarm()
}
if s.config.KeepAlive && time.Since(s.lastNetworkActivityTime) >= s.idleTimeout()/2 {
// send the PING frame since there is no activity in the session
s.packer.QueueControlFrame(&frames.PingFrame{})
s.keepAlivePingSent = true
}
if err := s.sendPacket(); err != nil {
s.closeLocal(err)
}
@@ -333,7 +342,12 @@ func (s *session) WaitUntilClosed() {
}
func (s *session) maybeResetTimer() {
deadline := s.lastNetworkActivityTime.Add(s.idleTimeout())
var deadline time.Time
if s.config.KeepAlive && !s.keepAlivePingSent {
deadline = s.lastNetworkActivityTime.Add(s.idleTimeout() / 2)
} else {
deadline = s.lastNetworkActivityTime.Add(s.idleTimeout())
}
if ackAlarm := s.receivedPacketHandler.GetAlarmTimeout(); !ackAlarm.IsZero() {
deadline = utils.MinTime(deadline, ackAlarm)
@@ -373,6 +387,7 @@ func (s *session) handlePacketImpl(p *receivedPacket) error {
}
s.lastNetworkActivityTime = p.rcvTime
s.keepAlivePingSent = false
hdr := p.publicHeader
data := p.data