-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
275 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package middlewares | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/containous/traefik/healthcheck" | ||
) | ||
|
||
// EmptyBackendHandler is a middlware that checks whether the current Backend | ||
// has at least one active Server in respect to the healthchecks and if this | ||
// is not the case, it will stop the middleware chain and respond with 503. | ||
type EmptyBackendHandler struct { | ||
lb healthcheck.LoadBalancer | ||
next http.Handler | ||
} | ||
|
||
// NewEmptyBackendHandler creates a new EmptyBackendHandler instance. | ||
func NewEmptyBackendHandler(lb healthcheck.LoadBalancer, next http.Handler) *EmptyBackendHandler { | ||
return &EmptyBackendHandler{lb: lb, next: next} | ||
} | ||
|
||
// ServeHTTP responds with 503 when there is no active Server and otherwise | ||
// invokes the next handler in the middleware chain. | ||
func (h *EmptyBackendHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { | ||
if len(h.lb.Servers()) == 0 { | ||
rw.WriteHeader(http.StatusServiceUnavailable) | ||
rw.Write([]byte(http.StatusText(http.StatusServiceUnavailable))) | ||
} else { | ||
h.next.ServeHTTP(rw, r) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package middlewares | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/containous/traefik/testhelpers" | ||
"github.com/vulcand/oxy/roundrobin" | ||
) | ||
|
||
func TestEmptyBackendHandler(t *testing.T) { | ||
tests := []struct { | ||
amountServer int | ||
wantStatusCode int | ||
}{ | ||
{ | ||
amountServer: 0, | ||
wantStatusCode: http.StatusServiceUnavailable, | ||
}, | ||
{ | ||
amountServer: 1, | ||
wantStatusCode: http.StatusOK, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test | ||
|
||
t.Run(fmt.Sprintf("amount servers %d", test.amountServer), func(t *testing.T) { | ||
t.Parallel() | ||
|
||
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
handler := NewEmptyBackendHandler(&healthCheckLoadBalancer{test.amountServer}, nextHandler) | ||
|
||
recorder := httptest.NewRecorder() | ||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil) | ||
|
||
handler.ServeHTTP(recorder, req) | ||
|
||
if recorder.Result().StatusCode != test.wantStatusCode { | ||
t.Errorf("Received status code %d, wanted %d", recorder.Result().StatusCode, test.wantStatusCode) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type healthCheckLoadBalancer struct { | ||
amountServer int | ||
} | ||
|
||
func (lb *healthCheckLoadBalancer) RemoveServer(u *url.URL) error { | ||
return nil | ||
} | ||
|
||
func (lb *healthCheckLoadBalancer) UpsertServer(u *url.URL, options ...roundrobin.ServerOption) error { | ||
return nil | ||
} | ||
|
||
func (lb *healthCheckLoadBalancer) Servers() []*url.URL { | ||
servers := make([]*url.URL, lb.amountServer) | ||
for i := 0; i < lb.amountServer; i++ { | ||
servers = append(servers, testhelpers.MustParseURL("http://localhost")) | ||
} | ||
return servers | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters