mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-01-04 16:25:21 -05:00
v5: Health Check & LeastLoad Strategy (rebased from 2c5a714903)
Some changes will be necessary to integrate it into V2Ray
This commit is contained in:
38
app/router/strategy_random.go
Normal file
38
app/router/strategy_random.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/v2fly/v2ray-core/v4/common/dice"
|
||||
"github.com/v2fly/v2ray-core/v4/features/routing"
|
||||
)
|
||||
|
||||
// RandomStrategy represents a random balancing strategy
|
||||
type RandomStrategy struct{}
|
||||
|
||||
// GetInformation implements the routing.BalancingStrategy.
|
||||
func (s *RandomStrategy) GetInformation(tags []string) *routing.StrategyInfo {
|
||||
items := make([]*routing.OutboundInfo, 0)
|
||||
for _, tag := range tags {
|
||||
items = append(items, &routing.OutboundInfo{Tag: tag})
|
||||
}
|
||||
return &routing.StrategyInfo{
|
||||
Settings: []string{"random"},
|
||||
ValueTitles: nil,
|
||||
Selects: items,
|
||||
Others: nil,
|
||||
}
|
||||
}
|
||||
|
||||
// SelectAndPick implements the routing.BalancingStrategy.
|
||||
func (s *RandomStrategy) SelectAndPick(candidates []string) string {
|
||||
return s.Pick(candidates)
|
||||
}
|
||||
|
||||
// Pick implements the routing.BalancingStrategy.
|
||||
func (s *RandomStrategy) Pick(candidates []string) string {
|
||||
count := len(candidates)
|
||||
if count == 0 {
|
||||
// goes to fallbackTag
|
||||
return ""
|
||||
}
|
||||
return candidates[dice.Roll(count)]
|
||||
}
|
||||
Reference in New Issue
Block a user