From b4c12f824bcea6b3038f5c878001f72e6a57de1e Mon Sep 17 00:00:00 2001 From: "Alex Ellis (VMware)" Date: Mon, 5 Nov 2018 22:12:13 +0000 Subject: [PATCH] Make use of cache in scaling - this reinstates the cache to reduce the count of lookups to the provider when checking if scaling is needed. Signed-off-by: Alex Ellis (VMware) --- gateway/scaling/function_scaler.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/gateway/scaling/function_scaler.go b/gateway/scaling/function_scaler.go index 1d126701..cadb6816 100644 --- a/gateway/scaling/function_scaler.go +++ b/gateway/scaling/function_scaler.go @@ -38,6 +38,17 @@ type FunctionScaleResult struct { // the minimum replicas metadata func (f *FunctionScaler) Scale(functionName string) FunctionScaleResult { start := time.Now() + + if cachedResponse, hit := f.Cache.Get(functionName); hit && + cachedResponse.AvailableReplicas > 0 { + return FunctionScaleResult{ + Error: nil, + Available: true, + Found: true, + Duration: time.Since(start), + } + } + queryResponse, err := f.Config.ServiceQuery.GetReplicas(functionName) if err != nil { @@ -90,7 +101,9 @@ func (f *FunctionScaler) Scale(functionName string) FunctionScaleResult { for i := 0; i < int(f.Config.MaxPollCount); i++ { queryResponse, err := f.Config.ServiceQuery.GetReplicas(functionName) - f.Cache.Set(functionName, queryResponse) + if err == nil { + f.Cache.Set(functionName, queryResponse) + } totalTime := time.Since(start) if err != nil { @@ -104,7 +117,8 @@ func (f *FunctionScaler) Scale(functionName string) FunctionScaleResult { if queryResponse.AvailableReplicas > 0 { - log.Printf("[Scale] function=%s 0 => %d successful - %f seconds", functionName, queryResponse.AvailableReplicas, totalTime.Seconds()) + log.Printf("[Scale] function=%s 0 => %d successful - %f seconds", + functionName, queryResponse.AvailableReplicas, totalTime.Seconds()) return FunctionScaleResult{ Error: nil,