From 5479063f4eef127383c16cdc4073b85a6e7c40d9 Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 13 Apr 2021 12:34:34 -0500 Subject: [PATCH] limit health check concurrency --- app/discovery/discovery_test.go | 2 +- app/proxy/health.go | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/discovery/discovery_test.go b/app/discovery/discovery_test.go index 08d10d2..8bb7032 100644 --- a/app/discovery/discovery_test.go +++ b/app/discovery/discovery_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestService_Do(t *testing.T) { +func TestService_Run(t *testing.T) { p1 := &ProviderMock{ EventsFunc: func(ctx context.Context) <-chan ProviderID { res := make(chan ProviderID, 1) diff --git a/app/proxy/health.go b/app/proxy/health.go index 0c81fc3..16e1c09 100644 --- a/app/proxy/health.go +++ b/app/proxy/health.go @@ -27,18 +27,25 @@ func (h *Http) healthMiddleware(next http.Handler) http.Handler { func (h *Http) healthHandler(w http.ResponseWriter, _ *http.Request) { + const concurrent = 8 + sema := make(chan struct{}, concurrent) // limit health check to 8 concurrent calls + // runs pings in parallel check := func(mappers []discovery.URLMapper) (ok bool, valid int, total int, errs []string) { - outCh := make(chan error, 8) + outCh := make(chan error, concurrent) var pinged int32 var wg sync.WaitGroup for _, m := range mappers { if m.PingURL == "" { continue } + sema <- struct{}{} wg.Add(1) go func(m discovery.URLMapper) { - defer wg.Done() + defer func() { + <-sema + wg.Done() + }() atomic.AddInt32(&pinged, 1) client := http.Client{Timeout: 100 * time.Millisecond}