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 {