Skip to content

Commit

Permalink
add header for http healthcheck (fatedier#4085)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiveYoung authored Mar 20, 2024
1 parent 002831e commit bc5fb91
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions client/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type Monitor struct {
addr string

// For http
url string

url string
header http.Header
failedTimes uint64
statusOK bool
statusNormalFn func()
Expand Down Expand Up @@ -73,13 +73,19 @@ func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string,
}
url = s + cfg.Path
}
header := make(http.Header)
for _, h := range cfg.HTTPHeaders {
header.Set(h.Name, h.Value)
}

return &Monitor{
checkType: cfg.Type,
interval: time.Duration(cfg.IntervalSeconds) * time.Second,
timeout: time.Duration(cfg.TimeoutSeconds) * time.Second,
maxFailedTimes: cfg.MaxFailed,
addr: addr,
url: url,
header: header,
statusOK: false,
statusNormalFn: statusNormalFn,
statusFailedFn: statusFailedFn,
Expand Down Expand Up @@ -163,6 +169,8 @@ func (monitor *Monitor) doHTTPCheck(ctx context.Context) error {
if err != nil {
return err
}
req.Header = monitor.header
req.Host = monitor.header.Get("Host")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions conf/frpc_full_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ healthCheck.path = "/status"
healthCheck.intervalSeconds = 10
healthCheck.maxFailed = 3
healthCheck.timeoutSeconds = 3
# set health check headers
healthCheck.httpheaders=[
{ name = "x-from-where", value = "frp" }
]

[[proxies]]
name = "web02"
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/v1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,8 @@ type HTTPPluginOptions struct {
type HeaderOperations struct {
Set map[string]string `json:"set,omitempty"`
}

type HTTPHeader struct {
Name string `json:"name"`
Value string `json:"value"`
}
3 changes: 3 additions & 0 deletions pkg/config/v1/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ type HealthCheckConfig struct {
// Path specifies the path to send health checks to if the
// health check type is "http".
Path string `json:"path,omitempty"`
// Headers specifies the headers to send health checks to if the
// health check type is "http".
HTTPHeaders []HTTPHeader `json:"httpHeaders,omitempty"`
}

type DomainConfig struct {
Expand Down

0 comments on commit bc5fb91

Please sign in to comment.