Skip to content

Commit

Permalink
Add X-OpenFaaS-Internal header for concurrency limiting
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Nov 10, 2023
1 parent fcf59bb commit 6925702
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions concurrency-limiter/concurrency_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (cl *ConcurrencyLimiter) ServeHTTP(w http.ResponseWriter, r *http.Request)
// Some APIs only return JSON, since we can interfere here and send a plain/text
// message, let's do the right thing so that downstream users can consume it.
w.Header().Add("Content-Type", "text/plain")
w.Header().Add("X-OpenFaaS-Internal", "faas-middleware")

w.WriteHeader(http.StatusTooManyRequests)

Expand Down
14 changes: 14 additions & 0 deletions concurrency-limiter/concurrency_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func TestConcurrencyLimitOverLimit(t *testing.T) {

want := "text/plain"
gotContentType := 0
gotInternalHeader := 0
if rr1.Header().Get("Content-Type") == want {
gotContentType++
}
Expand All @@ -270,10 +271,23 @@ func TestConcurrencyLimitOverLimit(t *testing.T) {
gotContentType++
}

if rr1.Header().Get("X-OpenFaaS-Internal") == "faas-middleware" {
gotInternalHeader++
}
if rr2.Header().Get("X-OpenFaaS-Internal") == "faas-middleware" {
gotInternalHeader++
}
if rr3.Header().Get("X-OpenFaaS-Internal") == "faas-middleware" {
gotInternalHeader++
}

if gotContentType == 0 {
t.Fatalf("Want at least one request with Content-Type %q, got: %q %q %q", want, rr1.Header().Get("Content-Type"), rr2.Header().Get("Content-Type"), rr3.Header().Get("Content-Type"))
}

if gotInternalHeader == 0 {
t.Fatalf("Want at least one request with X-OpenFaaS-Internal header, got: %q %q %q", rr1.Header().Get("X-OpenFaaS-Internal"), rr2.Header().Get("X-OpenFaaS-Internal"), rr3.Header().Get("X-OpenFaaS-Internal"))
}
}

func TestConcurrencyLimitOverLimitAndRecover(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/openfaas/faas-middleware

go 1.18
go 1.20

0 comments on commit 6925702

Please sign in to comment.