diff --git a/pool.go b/pool.go index b2b5d58..972553d 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,21 @@ 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(maxInstancesEnv) > 0 { + var err error + 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 { diff --git a/pool_test.go b/pool_test.go index c6ebaa6..7e49110 100644 --- a/pool_test.go +++ b/pool_test.go @@ -2,6 +2,7 @@ package server import ( "fmt" + "runtime" "sync" "testing" "time" @@ -121,7 +122,7 @@ func TestDriverPoolParallel(t *testing.T) { } wg.Wait() - require.Equal(10, dp.cur) + require.Equal(runtime.NumCPU(), dp.cur) time.Sleep(time.Second * 2) require.Equal(1, dp.cur)