Skip to content

Commit

Permalink
feat: support timeout setting for lvscare http prober (#3901) (#3905)
Browse files Browse the repository at this point in the history
Co-authored-by: fengxsong <fengxsong@outlook.com>
  • Loading branch information
sealos-ci-robot and fengxsong authored Sep 12, 2023
1 parent a256283 commit 6bd5c0a
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
Original file line number Diff line number Diff line change
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 6bd5c0a

Please sign in to comment.