Skip to content

Commit

Permalink
feat: support timeout setting for lvscare http prober (#3901)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengxsong committed Sep 12, 2023
1 parent 51f43a6 commit a27db61
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion staging/src/github.com/labring/lvscare/care/prober.go
Expand Up @@ -16,13 +16,15 @@ package care

import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"time"

"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/sets"
Expand All @@ -40,6 +42,7 @@ type httpProber struct {
Body string
ValidStatusCodes []int
InsecureSkipVerify bool
timeout time.Duration

client *http.Client
validStatus sets.Int
Expand All @@ -53,6 +56,7 @@ func (p *httpProber) RegisterFlags(fs *pflag.FlagSet) {
fs.StringToStringVar(&p.Headers, "health-req-headers", map[string]string{}, "http request headers")
fs.IntSliceVar(&p.ValidStatusCodes, "health-status", []int{}, "valid status codes")
fs.BoolVar(&p.InsecureSkipVerify, "health-insecure-skip-verify", true, "skip verify insecure request")
fs.DurationVar(&p.timeout, "health-timeout", 10*time.Second, "http probe timeout")
}

func (p *httpProber) ValidateAndSetDefaults() error {
Expand Down Expand Up @@ -88,7 +92,9 @@ func (p *httpProber) Probe(host, port string) error {
if len(p.Body) > 0 {
body = bytes.NewBufferString(p.Body)
}
req, err := http.NewRequest(p.Method, uri.String(), body)
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, p.Method, uri.String(), body)
if err != nil {
return err
}
Expand Down

0 comments on commit a27db61

Please sign in to comment.