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 f9ec4bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
22 changes: 21 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 All @@ -14,6 +18,11 @@ var (
// DefaultPoolTimeout is the time a request to the DriverPool can wait
// before getting a driver assigned.
DefaultPoolTimeout = time.Second * 5

// DefaultMaxInstancesPerDriver is the maximum number of instances of
// the same driver which can be launched following the default
// scaling policy (see DefaultScalingPolicy()).
DefaultMaxInstancesPerDriver = runtime.NumCPU()
)

// DriverPool controls a pool of drivers and balances requests among them,
Expand Down Expand Up @@ -235,7 +244,7 @@ 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)))
return MovingAverage(10, MinMax(1, DefaultMaxInstancesPerDriver, AIMD(1, 0.5)))
}

type movingAverage struct {
Expand Down Expand Up @@ -334,3 +343,14 @@ func (p *aimd) Scale(total, load int) int {

return res
}

func init() {
// Try to read DefaultMaxInstancesPerDriver from the environment variable
defaultMaxInstancesPerDriverEnv := os.Getenv("BBLFSH_MAX_INSTANCES_PER_DRIVER")
if len(defaultMaxInstancesPerDriverEnv) > 0 {
maxInstances, err := strconv.Atoi(maxInstancesEnv)
if err == nil && maxInstances > 0 {
DefaultMaxInstancesPerDriver = maxInstances
}
}
}
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 f9ec4bc

Please sign in to comment.