mirror of
https://github.com/caddyserver/caddy.git
synced 2025-05-24 02:02:26 -04:00
reverseproxy: Add valid Upstream to DialInfo in active health checks (#6949)
Currently if we extract the DialInfo from a Request Context during an active health check, then the Upstream in the DialInfo is nil. This PR attempts to set the Upstream to a sensible value, based on wether or not the Upstream has been overriden in the active health check's config.
This commit is contained in:
parent
def9db1f16
commit
6c38ae7381
@ -309,7 +309,9 @@ func (h *Handler) doActiveHealthCheckForAllHosts() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
networkAddr, err := caddy.NewReplacer().ReplaceOrErr(upstream.Dial, true, true)
|
repl := caddy.NewReplacer()
|
||||||
|
|
||||||
|
networkAddr, err := repl.ReplaceOrErr(upstream.Dial, true, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if c := h.HealthChecks.Active.logger.Check(zapcore.ErrorLevel, "invalid use of placeholders in dial address for active health checks"); c != nil {
|
if c := h.HealthChecks.Active.logger.Check(zapcore.ErrorLevel, "invalid use of placeholders in dial address for active health checks"); c != nil {
|
||||||
c.Write(
|
c.Write(
|
||||||
@ -344,14 +346,24 @@ func (h *Handler) doActiveHealthCheckForAllHosts() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
hostAddr := addr.JoinHostPort(0)
|
hostAddr := addr.JoinHostPort(0)
|
||||||
dialAddr := hostAddr
|
|
||||||
if addr.IsUnixNetwork() || addr.IsFdNetwork() {
|
if addr.IsUnixNetwork() || addr.IsFdNetwork() {
|
||||||
// this will be used as the Host portion of a http.Request URL, and
|
// this will be used as the Host portion of a http.Request URL, and
|
||||||
// paths to socket files would produce an error when creating URL,
|
// paths to socket files would produce an error when creating URL,
|
||||||
// so use a fake Host value instead; unix sockets are usually local
|
// so use a fake Host value instead; unix sockets are usually local
|
||||||
hostAddr = "localhost"
|
hostAddr = "localhost"
|
||||||
}
|
}
|
||||||
err = h.doActiveHealthCheck(DialInfo{Network: addr.Network, Address: dialAddr}, hostAddr, networkAddr, upstream)
|
|
||||||
|
// Fill in the dial info for the upstream
|
||||||
|
// If the upstream is set, use that instead
|
||||||
|
dialInfoUpstream := upstream
|
||||||
|
if h.HealthChecks.Active.Upstream != "" {
|
||||||
|
dialInfoUpstream = &Upstream{
|
||||||
|
Dial: h.HealthChecks.Active.Upstream,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dialInfo, _ := dialInfoUpstream.fillDialInfo(repl)
|
||||||
|
|
||||||
|
err = h.doActiveHealthCheck(dialInfo, hostAddr, networkAddr, upstream)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if c := h.HealthChecks.Active.logger.Check(zapcore.ErrorLevel, "active health check failed"); c != nil {
|
if c := h.HealthChecks.Active.logger.Check(zapcore.ErrorLevel, "active health check failed"); c != nil {
|
||||||
c.Write(
|
c.Write(
|
||||||
|
@ -17,7 +17,6 @@ package reverseproxy
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@ -100,8 +99,7 @@ func (u *Upstream) Full() bool {
|
|||||||
|
|
||||||
// fillDialInfo returns a filled DialInfo for upstream u, using the request
|
// fillDialInfo returns a filled DialInfo for upstream u, using the request
|
||||||
// context. Note that the returned value is not a pointer.
|
// context. Note that the returned value is not a pointer.
|
||||||
func (u *Upstream) fillDialInfo(r *http.Request) (DialInfo, error) {
|
func (u *Upstream) fillDialInfo(repl *caddy.Replacer) (DialInfo, error) {
|
||||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
|
||||||
var addr caddy.NetworkAddress
|
var addr caddy.NetworkAddress
|
||||||
|
|
||||||
// use provided dial address
|
// use provided dial address
|
||||||
|
@ -532,7 +532,7 @@ func (h *Handler) proxyLoopIteration(r *http.Request, origReq *http.Request, w h
|
|||||||
// the dial address may vary per-request if placeholders are
|
// the dial address may vary per-request if placeholders are
|
||||||
// used, so perform those replacements here; the resulting
|
// used, so perform those replacements here; the resulting
|
||||||
// DialInfo struct should have valid network address syntax
|
// DialInfo struct should have valid network address syntax
|
||||||
dialInfo, err := upstream.fillDialInfo(r)
|
dialInfo, err := upstream.fillDialInfo(repl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true, fmt.Errorf("making dial info: %v", err)
|
return true, fmt.Errorf("making dial info: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user