mirror of
https://github.com/openfaas/faas.git
synced 2025-06-22 14:53:25 +00:00
Feature for probing functions
Introduces a single-flight call to a function's health endpoint to verify that it is registered with an Istio sidecar (Envoy) before letting the invocation through. Results are cached for 5 seconds, before a probe is required again. Tested without Istio, with probe_functions environment variable set to true, I saw a probe execute in the logs. Fixes: #1721 for Istio users. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alex@openfaas.com>
This commit is contained in:
committed by
Alex Ellis
parent
01841f605c
commit
88eea5f62e
45
gateway/handlers/probe_handler.go
Normal file
45
gateway/handlers/probe_handler.go
Normal file
@ -0,0 +1,45 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"golang.org/x/sync/singleflight"
|
||||
|
||||
"github.com/openfaas/faas/gateway/pkg/middleware"
|
||||
"github.com/openfaas/faas/gateway/probing"
|
||||
)
|
||||
|
||||
func MakeProbeHandler(prober probing.FunctionProber, cache probing.ProbeCacher, resolver middleware.BaseURLResolver, next http.HandlerFunc, defaultNamespace string) http.HandlerFunc {
|
||||
|
||||
group := singleflight.Group{}
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
functionName, namespace := middleware.GetNamespace(defaultNamespace, middleware.GetServiceName(r.URL.String()))
|
||||
|
||||
key := fmt.Sprintf("Probe-%s.%s", functionName, namespace)
|
||||
res, _, _ := group.Do(key, func() (interface{}, error) {
|
||||
|
||||
cached, hit := cache.Get(functionName, namespace)
|
||||
var probeResult probing.FunctionProbeResult
|
||||
if hit && cached != nil && cached.Available {
|
||||
probeResult = *cached
|
||||
} else {
|
||||
probeResult = prober.Probe(functionName, namespace)
|
||||
cache.Set(functionName, namespace, &probeResult)
|
||||
}
|
||||
|
||||
return probeResult, nil
|
||||
})
|
||||
|
||||
fnRes := res.(probing.FunctionProbeResult)
|
||||
|
||||
if !fnRes.Available {
|
||||
http.Error(w, fmt.Sprintf("unable to probe function endpoint %s", fnRes.Error),
|
||||
http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
|
||||
next(w, r)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user