Skip to content

Commit

Permalink
Renamed old RoundRobin strategy to Random.
Browse files Browse the repository at this point in the history
Added RoundRobin strategy.
Added StrategyFactory to config.
  • Loading branch information
casskir committed Sep 30, 2019
1 parent bf198b6 commit d41264d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 12 deletions.
3 changes: 3 additions & 0 deletions broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func mergeConfigs(baseConfig moleculer.Config, userConfig []*moleculer.Config) m
if config.TransporterFactory != nil {
baseConfig.TransporterFactory = config.TransporterFactory
}
if config.StrategyFactory != nil {
baseConfig.StrategyFactory = config.StrategyFactory
}
if config.DisableInternalMiddlewares {
baseConfig.DisableInternalMiddlewares = config.DisableInternalMiddlewares
}
Expand Down
2 changes: 2 additions & 0 deletions moleculer.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ type Mixin struct {
}

type TransporterFactoryFunc func() interface{}
type StrategyFactoryFunc func() interface{}

type Config struct {
LogLevel string
LogFormat string
DiscoverNodeID func() string
Transporter string
TransporterFactory TransporterFactoryFunc
StrategyFactory StrategyFactoryFunc
HeartbeatFrequency time.Duration
HeartbeatTimeout time.Duration
OfflineCheckFrequency time.Duration
Expand Down
2 changes: 1 addition & 1 deletion registry/actionCatalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

var _ = Describe("Actions Catalog", func() {
logger := log.WithField("unit test pkg", "registry_test")
strategy := strategy.RoundRobinStrategy{}
strategy := strategy.RandomStrategy{}
params := moleculer.ObjectSchema{nil}
node1 := registry.CreateNode("node-test-1", true, logger)
node2 := registry.CreateNode("node-test-2", true, logger)
Expand Down
6 changes: 5 additions & 1 deletion registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ func createTransit(broker *moleculer.BrokerDelegates) transit.Transit {
// createStrategy create a strsategy instance based on the config.
func createStrategy(broker *moleculer.BrokerDelegates) strategy.Strategy {
//TODO: when new strategies are addes.. adde config check here to load the right one.
return strategy.RoundRobinStrategy{}
if broker.Config.StrategyFactory != nil {
return broker.Config.StrategyFactory().(strategy.Strategy)
}

return strategy.RandomStrategy{}
}

func CreateRegistry(nodeID string, broker *moleculer.BrokerDelegates) *ServiceRegistry {
Expand Down
18 changes: 18 additions & 0 deletions strategy/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package strategy

import (
"math/rand"
)

// RoundRobinStrategy exposes the type as a strategy option
type RandomStrategy struct {
}

func (randomStrategy RandomStrategy) Select(nodes []Selector) *Selector {
if len(nodes) == 0 {
return nil
}
// Returns a number among the indexes up to the length
// of the slice
return &nodes[rand.Intn(len(nodes))]
}
22 changes: 14 additions & 8 deletions strategy/round-robin.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package strategy

import (
"math/rand"
)

// RoundRobinStrategy exposes the type as a strategy option
type RoundRobinStrategy struct {
counter int
}

func NewRoundRobinStrategy() Strategy {
return &RoundRobinStrategy{counter: -1}
}

func (roundRobinStrategy RoundRobinStrategy) Select(nodes []Selector) *Selector {
func (roundRobinStrategy *RoundRobinStrategy) Select(nodes []Selector) *Selector {
if len(nodes) == 0 {
return nil
}
// Returns a number among the indexes up to the length
// of the slice
return &nodes[rand.Intn(len(nodes))]

roundRobinStrategy.counter++

if roundRobinStrategy.counter >= len(nodes) {
roundRobinStrategy.counter = 0
}

return &nodes[roundRobinStrategy.counter]
}
4 changes: 2 additions & 2 deletions strategy/strategy_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func (sel SelectorImpl) TargetNodeID() string {
}

var _ = Describe("Strategy", func() {
thisStrategy := strategy.RoundRobinStrategy{}
thisStrategy := strategy.RandomStrategy{}

list := []strategy.Selector{SelectorImpl{"alpha"}, SelectorImpl{"beta"}, SelectorImpl{"gamma"}, SelectorImpl{"delta"}}

emptyList := []strategy.Selector{}

It("Should return a ramdon target node according to strategy", func() {
It("Should return a random target node according to strategy", func() {
thisNode := thisStrategy.Select(list)
Expect(thisNode).Should(Not(BeNil()))
})
Expand Down

0 comments on commit d41264d

Please sign in to comment.