-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: custom ready checks should not count to the concurrency
Document why we use the base function handler to provide custom ready checks to all function modes. Additionally, make sure that the unwrapped function handler is passed to the readiness check. This ensures that readiness checks do not count toward the concurrency limit Review by AE - reverts change to faas-middleware, by introducing an interface. Closes: #145 Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com> Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
- Loading branch information
1 parent
e479ad7
commit 0b79385
Showing
429 changed files
with
57,509 additions
and
22,799 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
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,74 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"sync/atomic" | ||
|
||
limiter "github.com/openfaas/faas-middleware/concurrency-limiter" | ||
) | ||
|
||
type readiness struct { | ||
// functionHandler is the function invoke HTTP Handler. Using this allows | ||
// custom ready checks in all invoke modes. For example, in forking mode | ||
// the handler implementation (a bash script) can check the path in the env | ||
// and respond accordingly, exit non-zero when not ready. | ||
functionHandler http.Handler | ||
endpoint string | ||
lockCheck func() bool | ||
limiter limiter.Limiter | ||
} | ||
|
||
// LimitMet returns true if the concurrency limit has been reached | ||
// or false if no limiter has been used | ||
func (r *readiness) LimitMet() bool { | ||
if r.limiter == nil { | ||
return false | ||
} | ||
return r.limiter.Met() | ||
} | ||
|
||
func (r *readiness) ServeHTTP(w http.ResponseWriter, req *http.Request) { | ||
switch req.Method { | ||
case http.MethodGet: | ||
status := http.StatusOK | ||
|
||
switch { | ||
case atomic.LoadInt32(&acceptingConnections) == 0, !r.lockCheck(): | ||
status = http.StatusServiceUnavailable | ||
case r.LimitMet(): | ||
log.Println("Limited") | ||
|
||
status = http.StatusTooManyRequests | ||
case r.endpoint != "": | ||
upstream := url.URL{ | ||
Scheme: req.URL.Scheme, | ||
Host: req.URL.Host, | ||
Path: r.endpoint, | ||
} | ||
|
||
readyReq, err := http.NewRequestWithContext(req.Context(), http.MethodGet, upstream.String(), nil) | ||
if err != nil { | ||
log.Printf("Error creating readiness request to: %s : %s", upstream.String(), err) | ||
status = http.StatusInternalServerError | ||
break | ||
} | ||
|
||
// we need to set the raw RequestURI for the function invoker to see our URL path, | ||
// otherwise it will just route to `/`, typically this shouldn't be used or set | ||
readyReq.RequestURI = r.endpoint | ||
readyReq.Header = req.Header.Clone() | ||
|
||
// Instead of calling http.DefaultClient.Do(), which only works with http mode | ||
// calling this handler can fork a process to run a request, such as when | ||
// using bash as the function. | ||
r.functionHandler.ServeHTTP(w, readyReq) | ||
return | ||
} | ||
|
||
w.WriteHeader(status) | ||
default: | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.