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 739d5d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
20 changes: 19 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,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 {
Expand Down
3 changes: 2 additions & 1 deletion pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"fmt"
"runtime"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 739d5d0

Please sign in to comment.