Scale scales a function from zero replicas to 1 or the value set in the minimum replicas metadata
(functionName, namespace string)
| 37 | // Scale scales a function from zero replicas to 1 or the value set in |
| 38 | // the minimum replicas metadata |
| 39 | func (f *FunctionScaler) Scale(functionName, namespace string) FunctionScaleResult { |
| 40 | start := time.Now() |
| 41 | |
| 42 | // First check the cache, if there are available replicas, then the |
| 43 | // request can be served. |
| 44 | if cachedResponse, hit := f.Cache.Get(functionName, namespace); hit && |
| 45 | cachedResponse.AvailableReplicas > 0 { |
| 46 | return FunctionScaleResult{ |
| 47 | Error: nil, |
| 48 | Available: true, |
| 49 | Found: true, |
| 50 | Duration: time.Since(start), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // The wasn't a hit, or there were no available replicas found |
| 55 | // so query the live endpoint |
| 56 | getKey := fmt.Sprintf("GetReplicas-%s.%s", functionName, namespace) |
| 57 | res, err, _ := f.SingleFlight.Do(getKey, func() (interface{}, error) { |
| 58 | return f.Config.ServiceQuery.GetReplicas(functionName, namespace) |
| 59 | }) |
| 60 | |
| 61 | if err != nil { |
| 62 | return FunctionScaleResult{ |
| 63 | Error: err, |
| 64 | Available: false, |
| 65 | Found: false, |
| 66 | Duration: time.Since(start), |
| 67 | } |
| 68 | } |
| 69 | if res == nil { |
| 70 | return FunctionScaleResult{ |
| 71 | Error: fmt.Errorf("empty response from server"), |
| 72 | Available: false, |
| 73 | Found: false, |
| 74 | Duration: time.Since(start), |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Check if there are available replicas in the live data |
| 79 | if res.(ServiceQueryResponse).AvailableReplicas > 0 { |
| 80 | return FunctionScaleResult{ |
| 81 | Error: nil, |
| 82 | Available: true, |
| 83 | Found: true, |
| 84 | Duration: time.Since(start), |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Store the result of GetReplicas in the cache |
| 89 | queryResponse := res.(ServiceQueryResponse) |
| 90 | f.Cache.Set(functionName, namespace, queryResponse) |
| 91 | |
| 92 | // If the desired replica count is 0, then a scale up event |
| 93 | // is required. |
| 94 | if queryResponse.Replicas == 0 { |
| 95 | minReplicas := uint64(1) |
| 96 | if queryResponse.MinReplicas > 0 { |
no test coverage detected