Skip to content

Commit

Permalink
reverseproxy: Add ID field for upstreams
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Sep 13, 2021
1 parent a779e1b commit a5f4fae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions modules/caddyhttp/reverseproxy/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ type adminUpstreams struct{}

// upstreamResults holds the status of a particular upstream
type upstreamStatus struct {
Address string `json:"address"`
ID string `json:"id"`
Address string `json:"address"` // Address is deprecated, should be removed in a future release.
Healthy bool `json:"healthy"`
NumRequests int `json:"num_requests"`
Fails int `json:"fails"`
Expand Down Expand Up @@ -78,7 +79,7 @@ func (adminUpstreams) handleUpstreams(w http.ResponseWriter, r *http.Request) er
// Iterate over the upstream pool (needs to be fast)
var rangeErr error
hosts.Range(func(key, val interface{}) bool {
address, ok := key.(string)
id, ok := key.(string)
if !ok {
rangeErr = caddy.APIError{
HTTPStatus: http.StatusInternalServerError,
Expand All @@ -97,7 +98,8 @@ func (adminUpstreams) handleUpstreams(w http.ResponseWriter, r *http.Request) er
}

results = append(results, upstreamStatus{
Address: address,
ID: id,
Address: id,
Healthy: !upstream.Unhealthy(),
NumRequests: upstream.NumRequests(),
Fails: upstream.Fails(),
Expand Down
9 changes: 9 additions & 0 deletions modules/caddyhttp/reverseproxy/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ type UpstreamPool []*Upstream
type Upstream struct {
Host `json:"-"`

// The unique ID for this upstream, to disambiguate multiple
// upstreams with the same Dial address. This is optional,
// and only necessary if the upstream states need to be
// separate, such as having different health checking policies.
ID string `json:"id,omitempty"`

// The [network address](/docs/conventions#network-addresses)
// to dial to connect to the upstream. Must represent precisely
// one socket (i.e. no port ranges). A valid network address
Expand Down Expand Up @@ -98,6 +104,9 @@ type Upstream struct {
}

func (u Upstream) String() string {
if u.ID != "" {
return u.ID
}
if u.LookupSRV != "" {
return u.LookupSRV
}
Expand Down

0 comments on commit a5f4fae

Please sign in to comment.