From 4a0c712c0f427bca95ceac35715a58be848b388a Mon Sep 17 00:00:00 2001 From: Alex Ellis Date: Sat, 8 Apr 2017 09:53:10 +0100 Subject: [PATCH] Extract magic variables --- gateway/handlers/alerthandler.go | 47 ++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/gateway/handlers/alerthandler.go b/gateway/handlers/alerthandler.go index 4ec5c63f..29511491 100644 --- a/gateway/handlers/alerthandler.go +++ b/gateway/handlers/alerthandler.go @@ -14,6 +14,9 @@ import ( "github.com/docker/docker/client" ) +// DefaultMaxReplicas is the amount of replicas a service will auto-scale up to. +const DefaultMaxReplicas = 20 + // MakeAlertHandler handles alerts from Prometheus Alertmanager func MakeAlertHandler(c *client.Client) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { @@ -45,26 +48,6 @@ func MakeAlertHandler(c *client.Client) http.HandlerFunc { } } -// CalculateReplicas decides what replica count to set depending on a Prometheus alert -func CalculateReplicas(status string, currentReplicas uint64, maxReplicas uint64) uint64 { - newReplicas := currentReplicas - - if status == "firing" { - if currentReplicas == 1 { - newReplicas = 5 - } else { - if currentReplicas+5 > maxReplicas { - newReplicas = maxReplicas - } else { - newReplicas = currentReplicas + 5 - } - } - } else { // Resolved event. - newReplicas = 1 - } - return newReplicas -} - func scaleService(req requests.PrometheusAlert, c *client.Client) error { var err error serviceName := req.Alerts[0].Labels.FunctionName @@ -78,7 +61,7 @@ func scaleService(req requests.PrometheusAlert, c *client.Client) error { status := req.Status replicaLabel := service.Spec.TaskTemplate.ContainerSpec.Labels["com.faas.max_replicas"] - maxReplicas := 20 + maxReplicas := DefaultMaxReplicas if len(replicaLabel) > 0 { maxReplicas, err = strconv.Atoi(replicaLabel) if err != nil { @@ -108,3 +91,25 @@ func scaleService(req requests.PrometheusAlert, c *client.Client) error { } return err } + +// CalculateReplicas decides what replica count to set depending on current/desired amount +func CalculateReplicas(status string, currentReplicas uint64, maxReplicas uint64) uint64 { + newReplicas := currentReplicas + const step = 5 + + if status == "firing" { + if currentReplicas == 1 { + // First jump is from 1 to "step" i.e. 1->5 + newReplicas = step + } else { + if currentReplicas+step > maxReplicas { + newReplicas = maxReplicas + } else { + newReplicas = currentReplicas + step + } + } + } else { // Resolved event. + newReplicas = 1 + } + return newReplicas +}