Skip to content

Commit

Permalink
Set the max number of driver instances to CPU cores
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarkovtsev committed Jun 14, 2017
1 parent fc0811c commit 41f4a82
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package server
import (
"fmt"
"math"
"runtime"
"strconv"
"sync/atomic"
"time"

"os"

"github.com/bblfsh/sdk/protocol"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 41f4a82

Please sign in to comment.