-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow proxying readiness checks to the function
Allow setting an endpoint path for the function readiness check via an ENV variable `function_ready_endpoint` When this value is set, the requests to `/_/ready` will execute an empty GET request with/to the configured endpoint. This allows the function authors to implement custom readiness check logic. This custom request is checked _after_ the the standard liviness checks and the ConcurrencyLimiter check. For a completely custom readiness check, the function should be deployed with `max_inflight == 0` and `function_ready_endpoint` to the custom path. Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
- Loading branch information
1 parent
29909ab
commit 42bfe89
Showing
10 changed files
with
179 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,4 @@ template | |
bin | ||
/handler | ||
/Dockerfile2 | ||
.vscode |
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
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
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
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,94 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestReadinessHandler(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
endpoint string | ||
limitMet bool | ||
acceptingConnections int32 | ||
readyResponseCode int | ||
expectedCode int | ||
}{ | ||
{ | ||
name: "return 503 when not accepting connections", | ||
acceptingConnections: 0, | ||
expectedCode: http.StatusServiceUnavailable, | ||
}, | ||
{ | ||
name: "returns 200 when no upstream endpoint and no limiter", | ||
acceptingConnections: 1, | ||
expectedCode: http.StatusOK, | ||
}, | ||
{ | ||
name: "returns the upstream endpoint response code when no limiter", | ||
acceptingConnections: 1, | ||
endpoint: "/custom/ready", | ||
readyResponseCode: http.StatusNoContent, | ||
expectedCode: http.StatusNoContent, | ||
}, | ||
{ | ||
name: "return 429 when limiter is met", | ||
limitMet: true, | ||
acceptingConnections: 1, | ||
expectedCode: http.StatusTooManyRequests, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
upstream := testUpstreamHandler(tc.endpoint, tc.readyResponseCode) | ||
handler := &readiness{ | ||
functionHandler: upstream, | ||
endpoint: tc.endpoint, | ||
lockCheck: func() bool { return true }, | ||
limiter: &testLimiter{met: tc.limitMet}, | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
req, err := http.NewRequest(http.MethodGet, "/_/ready", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
acceptingConnections = tc.acceptingConnections | ||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != tc.expectedCode { | ||
t.Errorf("handler returned wrong status code - want: %v, got: %v", tc.expectedCode, status) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func testUpstreamHandler(endpoint string, status int) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path != endpoint { | ||
w.WriteHeader(http.StatusNotFound) | ||
return | ||
} | ||
|
||
if r.Method != http.MethodGet { | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
w.WriteHeader(status) | ||
}) | ||
} | ||
|
||
type testLimiter struct { | ||
met bool | ||
} | ||
|
||
func (t *testLimiter) Met() bool { | ||
if t == nil { | ||
return false | ||
} | ||
return t.met | ||
} |
11 changes: 11 additions & 0 deletions
11
vendor/github.com/openfaas/faas-middleware/concurrency-limiter/concurrency_limiter.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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