hellofresh/health-go

Perform healthchecks in parallel

AchoArnold opened this issue · 2 comments

Hello, I noticed the health checks are done sequentially, Perhaps you can add a configuration to make it possible to run the health checks in parallel.

I currently run the go app on a platform with a request timeout of about 60 seconds, and if I have to check 2 slow APIs that respond in > 30 seconds, the health check endpoint times out.

The sample code below takes 30 seconds instead of 15 seconds.

package main

import (
	"context"
	"log"
	"time"

	"github.com/hellofresh/health-go/v4"
)

func main() {
	// add some checks on instance creation
	h, _ := health.New(health.WithChecks(
		health.Config{
			Name:      "one",
			Timeout:   time.Second * 30,
			SkipOnErr: true,
			Check: func(ctx context.Context) error {
				time.Sleep(15 * time.Second)
				return nil
			},
		},
		health.Config{
			Name:    "two",
			Timeout: time.Second * 30,
			Check: func(ctx context.Context) error {
				time.Sleep(15 * time.Second)
				return nil
			},
		},
	))

	log.Println("start")
	h.Measure(context.Background())
	log.Println("end")
}

As of version v4.7.0 checks are running concurrently, but the default concurrency is set to the runtime.NumCPU(). Try setting it to the higher value using WithMaxConcurrent option.

Thanks @vgarvardt

I just tested with the latest version and it's done in parallel!