Skip to content

Commit 75deba4

Browse files
authored
fix: add idle timeout for server pool and implement keep-alive pings for clients
1 parent 309f5c0 commit 75deba4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

nph2.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ func NewServerPool(
213213
keepAlive: keepAlive,
214214
h2Server: &http2.Server{
215215
MaxConcurrentStreams: uint32(maxCap),
216+
IdleTimeout: keepAlive * 2,
216217
},
217218
}
218219
pool.ctx, pool.cancel = context.WithCancel(context.Background())
@@ -466,6 +467,23 @@ func (p *Pool) ClientManager() {
466467
}
467468
p.ctx, p.cancel = context.WithCancel(context.Background())
468469

470+
if p.keepAlive > 0 {
471+
go func() {
472+
ticker := time.NewTicker(p.keepAlive)
473+
defer ticker.Stop()
474+
for {
475+
select {
476+
case <-ticker.C:
477+
if client := p.h2Client.Load(); client != nil && *client != nil {
478+
(*client).Ping(p.ctx)
479+
}
480+
case <-p.ctx.Done():
481+
return
482+
}
483+
}
484+
}()
485+
}
486+
469487
for p.ctx.Err() == nil {
470488
p.adjustInterval()
471489
capacity := int(p.capacity.Load())

0 commit comments

Comments
 (0)