mirror of
https://github.com/openfaas/faas.git
synced 2025-06-18 20:16:37 +00:00
Move to single-flight for back-end queries
When querying for replicas during a scale up event, then the gateway can overwhelm the provider with requests. This is especially true under high concurrent load. The changes in this PR limit the inflight requests. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alex@openfaas.com>
This commit is contained in:
committed by
Alex Ellis
parent
8f8a93d43f
commit
6ed0ab71fb
@ -3,12 +3,16 @@
|
||||
|
||||
package scaling
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
type CachedFunctionQuery struct {
|
||||
cache FunctionCacher
|
||||
serviceQuery ServiceQuery
|
||||
emptyAnnotations map[string]string
|
||||
singleFlight *SingleFlight
|
||||
}
|
||||
|
||||
func NewCachedFunctionQuery(cache FunctionCacher, serviceQuery ServiceQuery) FunctionQuery {
|
||||
@ -16,6 +20,7 @@ func NewCachedFunctionQuery(cache FunctionCacher, serviceQuery ServiceQuery) Fun
|
||||
cache: cache,
|
||||
serviceQuery: serviceQuery,
|
||||
emptyAnnotations: map[string]string{},
|
||||
singleFlight: NewSingleFlight(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,13 +40,23 @@ 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) {
|
||||
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)
|
||||
})
|
||||
|
||||
log.Printf("Result: %v %v", queryResponse, err)
|
||||
|
||||
// If there is a cache miss, then fetch the value from the provider API
|
||||
queryResponse, err := c.serviceQuery.GetReplicas(fn, ns)
|
||||
if err != nil {
|
||||
return ServiceQueryResponse{}, err
|
||||
}
|
||||
c.cache.Set(fn, ns, queryResponse)
|
||||
|
||||
if queryResponse != nil {
|
||||
c.cache.Set(fn, ns, queryResponse.(ServiceQueryResponse))
|
||||
}
|
||||
|
||||
} else {
|
||||
return query, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user