From 9ff9e06fba5f80e6ac34978f00fe354628b28443 Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Wed, 4 Jul 2018 14:29:35 +0800 Subject: [PATCH] add keep alive on client connection --- conn.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/conn.go b/conn.go index ea43fae..c36130c 100644 --- a/conn.go +++ b/conn.go @@ -128,14 +128,17 @@ func (c *ClientConn) Close() error { func (c *ClientConn) run() { var wantRead int var wantWrite int - var delay = 50 + var delay = 50 * time.Millisecond + var keepalive = 5 * time.Second var ret C.int + var lastDataRecv time.Time defer close(c.errch) datach := make(chan []byte) errch := make(chan error) + // data read loop go func() { buf := make([]byte, 16*1024) readloop: @@ -152,6 +155,24 @@ func (c *ClientConn) run() { break } datach <- buf[:n] + lastDataRecv = time.Now() + } + }() + + // keep alive loop + go func() { + for { + select { + case <-c.exitch: + return + case <-time.After(keepalive): + } + now := time.Now() + last := lastDataRecv + d := now.Sub(last) + if d > keepalive { + C.nghttp2_submit_ping(c.session, 0, nil) + } } }() @@ -198,7 +219,7 @@ loop: // make delay when no data read/write if wantRead == 0 && wantWrite == 0 { select { - case <-time.After(time.Duration(delay) * time.Millisecond): + case <-time.After(delay): } } }