limit health check concurrency

This commit is contained in:
Umputun
2021-04-13 12:34:34 -05:00
parent f6491246f8
commit 5479063f4e
2 changed files with 10 additions and 3 deletions

View File

@@ -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)

View File

@@ -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}