Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support timeout setting for lvscare http prober #3901

Merged
merged 1 commit into from Sep 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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