Skip to content

Commit

Permalink
Add --health-timeout and set it to 10 seconds (#111)
Browse files Browse the repository at this point in the history
One minute timeout is too long for a health check operation. Add
--health-timeout and use 10 seconds as default.
  • Loading branch information
vadmeste authored Jun 19, 2024
1 parent 7f7d959 commit 7a292f1
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ type Backend struct {
up int32
healthCheckURL string
healthCheckDuration time.Duration
healthCheckTimeout time.Duration
Stats *BackendStats
}

Expand Down Expand Up @@ -323,7 +324,7 @@ func drainBody(resp *http.Response) {

func (b *Backend) doHealthCheck() error {
// Set up a maximum timeout time for the healtcheck operation
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
ctx, cancel := context.WithTimeout(context.Background(), b.healthCheckTimeout)
defer cancel()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, b.healthCheckURL, nil)
Expand Down Expand Up @@ -739,7 +740,7 @@ func IsLoopback(addr string) bool {
return net.ParseIP(host).IsLoopback()
}

func configureSite(ctx *cli.Context, siteNum int, siteStrs []string, healthCheckPath string, healthCheckPort int, healthCheckDuration time.Duration) *site {
func configureSite(ctx *cli.Context, siteNum int, siteStrs []string, healthCheckPath string, healthCheckPort int, healthCheckDuration, healthCheckTimeout time.Duration) *site {
var endpoints []string

if ellipses.HasEllipses(siteStrs...) {
Expand Down Expand Up @@ -815,7 +816,7 @@ func configureSite(ctx *cli.Context, siteNum int, siteStrs []string, healthCheck
}
backend := &Backend{siteNum, endpoint, proxy, &http.Client{
Transport: proxy.Transport,
}, 0, healthCheckURL, healthCheckDuration, &stats}
}, 0, healthCheckURL, healthCheckDuration, healthCheckTimeout, &stats}
go backend.healthCheck()
proxy.ErrorHandler = backend.ErrorHandler
backends = append(backends, backend)
Expand All @@ -840,6 +841,7 @@ func sidekickMain(ctx *cli.Context) {
healthReadCheckPath := ctx.GlobalString("read-health-path")
healthCheckPort := ctx.GlobalInt("health-port")
healthCheckDuration := ctx.GlobalDuration("health-duration")
healthCheckTimeout := ctx.GlobalDuration("health-timeout")
addr := ctx.GlobalString("address")

// Validate port range which should be in [0, 65535]
Expand Down Expand Up @@ -886,7 +888,7 @@ func sidekickMain(ctx *cli.Context) {
healthCheckPath = healthReadCheckPath
}

site := configureSite(ctx, i+1, strings.Split(siteStrs, ","), healthCheckPath, healthCheckPort, healthCheckDuration)
site := configureSite(ctx, i+1, strings.Split(siteStrs, ","), healthCheckPath, healthCheckPort, healthCheckDuration, healthCheckTimeout)
sites = append(sites, site)
}

Expand Down Expand Up @@ -978,6 +980,11 @@ func main() {
Usage: "health check duration in seconds",
Value: 5 * time.Second,
},
cli.DurationFlag{
Name: "health-timeout",
Usage: "health check timeout in seconds",
Value: 10 * time.Second,
},
cli.BoolFlag{
Name: "insecure, i",
Usage: "disable TLS certificate verification",
Expand Down

0 comments on commit 7a292f1

Please sign in to comment.