Skip to content

Commit ffd8e56

Browse files
DougWagnertraefiker
authored andcommitted
Wrr loadbalancer honors old weight on recovered servers
1 parent 9299c3a commit ffd8e56

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

pkg/healthcheck/healthcheck.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,16 @@ func (opt Options) String() string {
5656
return fmt.Sprintf("[Hostname: %s Headers: %v Path: %s Port: %d Interval: %s Timeout: %s]", opt.Hostname, opt.Headers, opt.Path, opt.Port, opt.Interval, opt.Timeout)
5757
}
5858

59+
type backendURL struct {
60+
url *url.URL
61+
weight int
62+
}
63+
5964
// BackendConfig HealthCheck configuration for a backend
6065
type BackendConfig struct {
6166
Options
6267
name string
63-
disabledURLs []*url.URL
68+
disabledURLs []backendURL
6469
}
6570

6671
func (b *BackendConfig) newRequest(serverURL *url.URL) (*http.Request, error) {
@@ -135,21 +140,22 @@ func (hc *HealthCheck) execute(ctx context.Context, backend *BackendConfig) {
135140

136141
func (hc *HealthCheck) checkBackend(backend *BackendConfig) {
137142
enabledURLs := backend.LB.Servers()
138-
var newDisabledURLs []*url.URL
143+
var newDisabledURLs []backendURL
139144
// FIXME re enable metrics
140145
for _, disableURL := range backend.disabledURLs {
141146
// FIXME serverUpMetricValue := float64(0)
142-
if err := checkHealth(disableURL, backend); err == nil {
143-
log.Warnf("Health check up: Returning to server list. Backend: %q URL: %q", backend.name, disableURL.String())
144-
if err = backend.LB.UpsertServer(disableURL, roundrobin.Weight(1)); err != nil {
147+
if err := checkHealth(disableURL.url, backend); err == nil {
148+
log.Warnf("Health check up: Returning to server list. Backend: %q URL: %q Weight: %d",
149+
backend.name, disableURL.url.String(), disableURL.weight)
150+
if err = backend.LB.UpsertServer(disableURL.url, roundrobin.Weight(disableURL.weight)); err != nil {
145151
log.Error(err)
146152
}
147153
// FIXME serverUpMetricValue = 1
148154
} else {
149-
log.Warnf("Health check still failing. Backend: %q URL: %q Reason: %s", backend.name, disableURL.String(), err)
155+
log.Warnf("Health check still failing. Backend: %q URL: %q Reason: %s", backend.name, disableURL.url.String(), err)
150156
newDisabledURLs = append(newDisabledURLs, disableURL)
151157
}
152-
// FIXME labelValues := []string{"backend", backend.name, "url", disableURL.String()}
158+
// FIXME labelValues := []string{"backend", backend.name, "url", backendurl.url.String()}
153159
// FIXME hc.metrics.BackendServerUpGauge().With(labelValues...).Set(serverUpMetricValue)
154160
}
155161
backend.disabledURLs = newDisabledURLs
@@ -158,11 +164,20 @@ func (hc *HealthCheck) checkBackend(backend *BackendConfig) {
158164
for _, enableURL := range enabledURLs {
159165
// FIXME serverUpMetricValue := float64(1)
160166
if err := checkHealth(enableURL, backend); err != nil {
161-
log.Warnf("Health check failed: Remove from server list. Backend: %q URL: %q Reason: %s", backend.name, enableURL.String(), err)
167+
weight := 1
168+
rr, ok := backend.LB.(*roundrobin.RoundRobin)
169+
if ok {
170+
var gotWeight bool
171+
weight, gotWeight = rr.ServerWeight(enableURL)
172+
if !gotWeight {
173+
weight = 1
174+
}
175+
}
176+
log.Warnf("Health check failed: Remove from server list. Backend: %q URL: %q Weight: %d Reason: %s", backend.name, enableURL.String(), weight, err)
162177
if err := backend.LB.RemoveServer(enableURL); err != nil {
163178
log.Error(err)
164179
}
165-
backend.disabledURLs = append(backend.disabledURLs, enableURL)
180+
backend.disabledURLs = append(backend.disabledURLs, backendURL{enableURL, weight})
166181
// FIXME serverUpMetricValue = 0
167182
}
168183
// FIXME labelValues := []string{"backend", backend.name, "url", enableURL.String()}

pkg/healthcheck/healthcheck_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func TestSetBackendsConfiguration(t *testing.T) {
115115
if test.startHealthy {
116116
lb.servers = append(lb.servers, serverURL)
117117
} else {
118-
backend.disabledURLs = append(backend.disabledURLs, serverURL)
118+
backend.disabledURLs = append(backend.disabledURLs, backendURL{url: serverURL, weight: 1})
119119
}
120120

121121
collectingMetrics := testhelpers.NewCollectingHealthCheckMetrics()

0 commit comments

Comments
 (0)