From 41f4a8236f76b01bd54c024d0051c5cc29393af9 Mon Sep 17 00:00:00 2001 From: Vadim Markovtsev Date: Wed, 14 Jun 2017 11:49:11 +0200 Subject: [PATCH] Set the max number of driver instances to CPU cores --- pool.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pool.go b/pool.go index b2b5d58..a14956d 100644 --- a/pool.go +++ b/pool.go @@ -3,9 +3,13 @@ package server import ( "fmt" "math" + "runtime" + "strconv" "sync/atomic" "time" + "os" + "github.com/bblfsh/sdk/protocol" "github.com/pkg/errors" ) @@ -235,7 +239,20 @@ type ScalingPolicy interface { // DefaultScalingPolicy returns a new instance of the default scaling policy. // Instances returned by this function should not be reused. func DefaultScalingPolicy() ScalingPolicy { - return MovingAverage(10, MinMax(1, 10, AIMD(1, 0.5))) + maxInstances := 0 + // Try to read maxInstances from the environment variable + maxInstancesEnv := os.Getenv("BBLFSH_SCALING_MAX_INSTANCES") + if len(max_instances_env) > 0 { + maxInstances, err := strconv.Atoi(maxInstancesEnv) + if err != nil { + maxInstances = 0 + } + } + // Disregarding the fact that the env var exists or not and is valid or not + if maxInstances <= 0 { + maxInstances = runtime.NumCPU() + } + return MovingAverage(10, MinMax(1, maxInstances, AIMD(1, 0.5))) } type movingAverage struct {