From 5307c5c3a190c6e6461785719a3bd84a888af5df Mon Sep 17 00:00:00 2001 From: tarat44 <32471142+tarat44@users.noreply.github.com> Date: Sat, 10 Apr 2021 00:53:53 -0400 Subject: [PATCH 1/4] close h2ping client connections --- agent/checks/check.go | 1 + 1 file changed, 1 insertion(+) diff --git a/agent/checks/check.go b/agent/checks/check.go index 868ad4d115..cc44e55f00 100644 --- a/agent/checks/check.go +++ b/agent/checks/check.go @@ -541,6 +541,7 @@ func (c *CheckH2PING) check() { } ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) defer cancel() + defer clientConn.Shutdown(ctx) err = clientConn.Ping(ctx) if err == nil { c.StatusHandler.updateCheck(c.CheckID, api.HealthPassing, "HTTP2 ping was successful") From a2e6ca1226526c6633d69fa1ce168c156ceb9876 Mon Sep 17 00:00:00 2001 From: tarat44 <32471142+tarat44@users.noreply.github.com> Date: Sun, 11 Apr 2021 15:11:00 -0400 Subject: [PATCH 2/4] add WaitGroup to h2ping --- agent/checks/check.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/agent/checks/check.go b/agent/checks/check.go index cc44e55f00..47752d9311 100644 --- a/agent/checks/check.go +++ b/agent/checks/check.go @@ -519,6 +519,7 @@ type CheckH2PING struct { stop bool stopCh chan struct{} stopLock sync.Mutex + stopWg sync.WaitGroup } func (c *CheckH2PING) check() { @@ -559,9 +560,11 @@ func (c *CheckH2PING) Stop() { c.stop = true close(c.stopCh) } + c.stopWg.Wait() } func (c *CheckH2PING) run() { + defer c.stopWg.Done() // Get the randomized initial pause time initialPauseTime := lib.RandomStagger(c.Interval) next := time.After(initialPauseTime) @@ -584,6 +587,7 @@ func (c *CheckH2PING) Start() { } c.stop = false c.stopCh = make(chan struct{}) + c.stopWg.Add(1) go c.run() } From 1ca5fa976981c7f388940cf83314913ddd9a0fe4 Mon Sep 17 00:00:00 2001 From: tarat44 <32471142+tarat44@users.noreply.github.com> Date: Sun, 11 Apr 2021 15:12:33 -0400 Subject: [PATCH 3/4] fix formatting --- agent/checks/check.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/agent/checks/check.go b/agent/checks/check.go index 47752d9311..e2417709a5 100644 --- a/agent/checks/check.go +++ b/agent/checks/check.go @@ -519,7 +519,7 @@ type CheckH2PING struct { stop bool stopCh chan struct{} stopLock sync.Mutex - stopWg sync.WaitGroup + stopWg sync.WaitGroup } func (c *CheckH2PING) check() { @@ -560,11 +560,11 @@ func (c *CheckH2PING) Stop() { c.stop = true close(c.stopCh) } - c.stopWg.Wait() + c.stopWg.Wait() } func (c *CheckH2PING) run() { - defer c.stopWg.Done() + defer c.stopWg.Done() // Get the randomized initial pause time initialPauseTime := lib.RandomStagger(c.Interval) next := time.After(initialPauseTime) @@ -587,7 +587,7 @@ func (c *CheckH2PING) Start() { } c.stop = false c.stopCh = make(chan struct{}) - c.stopWg.Add(1) + c.stopWg.Add(1) go c.run() } From 51f8db3879d3f7a4686fb51ac3162de40643886e Mon Sep 17 00:00:00 2001 From: tarat44 <32471142+tarat44@users.noreply.github.com> Date: Thu, 29 Apr 2021 18:05:50 -0400 Subject: [PATCH 4/4] create separate function with its own context to shutdown http2 client conn in h2ping check --- agent/checks/check.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/agent/checks/check.go b/agent/checks/check.go index e2417709a5..78a98b61e6 100644 --- a/agent/checks/check.go +++ b/agent/checks/check.go @@ -522,6 +522,17 @@ type CheckH2PING struct { stopWg sync.WaitGroup } +func shutdownHTTP2ClientConn(clientConn *http2.ClientConn, timeout time.Duration, checkIDString string, logger hclog.Logger) { + ctx, cancel := context.WithTimeout(context.Background(), timeout/2) + defer cancel() + err := clientConn.Shutdown(ctx) + if err != nil { + logger.Warn("Shutdown of H2Ping check client connection gave an error", + "check", checkIDString, + "error", err) + } +} + func (c *CheckH2PING) check() { t := &http2.Transport{ TLSClientConfig: c.TLSClientConfig, @@ -540,9 +551,9 @@ func (c *CheckH2PING) check() { c.StatusHandler.updateCheck(c.CheckID, api.HealthCritical, message) return } + defer shutdownHTTP2ClientConn(clientConn, c.Timeout, c.CheckID.String(), c.Logger) ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) defer cancel() - defer clientConn.Shutdown(ctx) err = clientConn.Ping(ctx) if err == nil { c.StatusHandler.updateCheck(c.CheckID, api.HealthPassing, "HTTP2 ping was successful")