-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renamed old RoundRobin strategy to Random.
Added RoundRobin strategy. Added StrategyFactory to config.
- Loading branch information
Showing
7 changed files
with
45 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters