Use sync package from unofficial Go library

Uses the sync package from the unofficial Go library instead
of simpler solution.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alex@openfaas.com>
This commit is contained in:
Alex Ellis (OpenFaaS Ltd)
2022-06-23 10:53:20 +01:00
committed by Alex Ellis
parent 6ed0ab71fb
commit 01841f605c
10 changed files with 286 additions and 9 deletions

View File

@ -6,13 +6,15 @@ package scaling
import (
"fmt"
"log"
"golang.org/x/sync/singleflight"
)
type CachedFunctionQuery struct {
cache FunctionCacher
serviceQuery ServiceQuery
emptyAnnotations map[string]string
singleFlight *SingleFlight
singleFlight *singleflight.Group
}
func NewCachedFunctionQuery(cache FunctionCacher, serviceQuery ServiceQuery) FunctionQuery {
@ -20,7 +22,7 @@ func NewCachedFunctionQuery(cache FunctionCacher, serviceQuery ServiceQuery) Fun
cache: cache,
serviceQuery: serviceQuery,
emptyAnnotations: map[string]string{},
singleFlight: NewSingleFlight(),
singleFlight: &singleflight.Group{},
}
}
@ -41,7 +43,7 @@ func (c *CachedFunctionQuery) Get(fn string, ns string) (ServiceQueryResponse, e
query, hit := c.cache.Get(fn, ns)
if !hit {
key := fmt.Sprintf("GetReplicas-%s.%s", fn, ns)
queryResponse, err := c.singleFlight.Do(key, func() (interface{}, error) {
queryResponse, err, _ := c.singleFlight.Do(key, func() (interface{}, error) {
log.Printf("Cache miss - run GetReplicas")
// If there is a cache miss, then fetch the value from the provider API
return c.serviceQuery.GetReplicas(fn, ns)

View File

@ -4,6 +4,8 @@ import (
"fmt"
"log"
"time"
"golang.org/x/sync/singleflight"
)
// NewFunctionScaler create a new scaler with the specified
@ -12,7 +14,7 @@ func NewFunctionScaler(config ScalingConfig, functionCacher FunctionCacher) Func
return FunctionScaler{
Cache: functionCacher,
Config: config,
SingleFlight: NewSingleFlight(),
SingleFlight: &singleflight.Group{},
}
}
@ -20,7 +22,7 @@ func NewFunctionScaler(config ScalingConfig, functionCacher FunctionCacher) Func
type FunctionScaler struct {
Cache FunctionCacher
Config ScalingConfig
SingleFlight *SingleFlight
SingleFlight *singleflight.Group
}
// FunctionScaleResult holds the result of scaling from zero
@ -47,7 +49,7 @@ func (f *FunctionScaler) Scale(functionName, namespace string) FunctionScaleResu
}
getKey := fmt.Sprintf("GetReplicas-%s.%s", functionName, namespace)
res, err := f.SingleFlight.Do(getKey, func() (interface{}, error) {
res, err, _ := f.SingleFlight.Do(getKey, func() (interface{}, error) {
return f.Config.ServiceQuery.GetReplicas(functionName, namespace)
})
@ -80,7 +82,7 @@ func (f *FunctionScaler) Scale(functionName, namespace string) FunctionScaleResu
scaleResult := backoff(func(attempt int) error {
res, err := f.SingleFlight.Do(getKey, func() (interface{}, error) {
res, err, _ := f.SingleFlight.Do(getKey, func() (interface{}, error) {
return f.Config.ServiceQuery.GetReplicas(functionName, namespace)
})
@ -98,7 +100,7 @@ func (f *FunctionScaler) Scale(functionName, namespace string) FunctionScaleResu
setKey := fmt.Sprintf("SetReplicas-%s.%s", functionName, namespace)
if _, err := f.SingleFlight.Do(setKey, func() (interface{}, error) {
if _, err, _ := f.SingleFlight.Do(setKey, func() (interface{}, error) {
log.Printf("[Scale %d] function=%s 0 => %d requested", attempt, functionName, minReplicas)
@ -125,7 +127,7 @@ func (f *FunctionScaler) Scale(functionName, namespace string) FunctionScaleResu
for i := 0; i < int(f.Config.MaxPollCount); i++ {
res, err := f.SingleFlight.Do(getKey, func() (interface{}, error) {
res, err, _ := f.SingleFlight.Do(getKey, func() (interface{}, error) {
return f.Config.ServiceQuery.GetReplicas(functionName, namespace)
})
queryResponse := res.(ServiceQueryResponse)