mirror of
https://github.com/openfaas/faas.git
synced 2025-06-09 00:36:46 +00:00
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>
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
// Copyright (c) OpenFaaS Author(s). All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
package probing
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// ProbeCacher queries functions and caches the results
|
|
type ProbeCacher interface {
|
|
Set(functionName, namespace string, result *FunctionProbeResult)
|
|
Get(functionName, namespace string) (result *FunctionProbeResult, hit bool)
|
|
}
|
|
|
|
// ProbeCache provides a cache of Probe replica counts
|
|
type ProbeCache struct {
|
|
Cache map[string]*FunctionProbeResult
|
|
Expiry time.Duration
|
|
Sync sync.RWMutex
|
|
}
|
|
|
|
// NewProbeCache creates a function cache to query function metadata
|
|
func NewProbeCache(cacheExpiry time.Duration) ProbeCacher {
|
|
return &ProbeCache{
|
|
Cache: make(map[string]*FunctionProbeResult),
|
|
Expiry: cacheExpiry,
|
|
}
|
|
}
|
|
|
|
// Set replica count for functionName
|
|
func (fc *ProbeCache) Set(functionName, namespace string, result *FunctionProbeResult) {
|
|
fc.Sync.Lock()
|
|
defer fc.Sync.Unlock()
|
|
|
|
fc.Cache[functionName+"."+namespace] = result
|
|
}
|
|
|
|
func (fc *ProbeCache) Get(functionName, namespace string) (*FunctionProbeResult, bool) {
|
|
|
|
result := &FunctionProbeResult{
|
|
Available: false,
|
|
Error: fmt.Errorf("unavailable in cache"),
|
|
}
|
|
|
|
hit := false
|
|
fc.Sync.RLock()
|
|
defer fc.Sync.RUnlock()
|
|
|
|
if val, exists := fc.Cache[functionName+"."+namespace]; exists {
|
|
hit = val.Expired(fc.Expiry) == false
|
|
result = val
|
|
}
|
|
|
|
return result, hit
|
|
}
|