1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 17:27:50 -04:00
v2fly/infra/conf/router_strategy.go

92 lines
2.4 KiB
Go
Raw Normal View History

2021-04-08 16:08:20 -04:00
package conf
import (
"github.com/golang/protobuf/proto"
2021-06-04 19:42:53 -04:00
"github.com/v2fly/v2ray-core/v4/app/router"
)
2021-04-08 16:08:20 -04:00
const (
strategyRandom string = "random"
strategyLeastLoad string = "leastload"
2021-04-08 16:08:20 -04:00
strategyLeastPing string = "leastping"
)
var (
strategyConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{
strategyRandom: func() interface{} { return new(strategyEmptyConfig) },
strategyLeastLoad: func() interface{} { return new(strategyLeastLoadConfig) },
}, "type", "settings")
)
type strategyEmptyConfig struct {
}
func (v *strategyEmptyConfig) Build() (proto.Message, error) {
return nil, nil
}
type strategyLeastLoadConfig struct {
2021-06-05 08:00:14 -04:00
// health check settings
HealthCheck *healthCheckSettings `json:"healthCheck,omitempty"`
// weight settings
Costs []*router.StrategyWeight `json:"costs,omitempty"`
2021-06-05 08:00:14 -04:00
// ping rtt baselines
Baselines []Duration `json:"baselines,omitempty"`
// expected nodes count to select
Expected int32 `json:"expected,omitempty"`
2021-06-05 08:00:14 -04:00
// max acceptable rtt, filter away high delay nodes. defalut 0
MaxRTT Duration `json:"maxRTT,omitempty"`
// acceptable failure rate
Tolerance float64 `json:"tolerance,omitempty"`
}
2021-06-05 08:00:14 -04:00
// healthCheckSettings holds settings for health Checker
type healthCheckSettings struct {
Destination string `json:"destination"`
Connectivity string `json:"connectivity"`
Interval Duration `json:"interval"`
SamplingCount int `json:"sampling"`
Timeout Duration `json:"timeout"`
}
// Build implements Buildable.
func (v *strategyLeastLoadConfig) Build() (proto.Message, error) {
config := &router.StrategyLeastLoadConfig{
HealthCheck: &router.HealthPingConfig{},
}
if v.HealthCheck != nil {
config.HealthCheck = &router.HealthPingConfig{
Destination: v.HealthCheck.Destination,
Connectivity: v.HealthCheck.Connectivity,
2021-06-05 08:00:14 -04:00
Interval: int64(v.HealthCheck.Interval),
Timeout: int64(v.HealthCheck.Timeout),
SamplingCount: int32(v.HealthCheck.SamplingCount),
}
}
config.Costs = v.Costs
config.Tolerance = float32(v.Tolerance)
if config.Tolerance < 0 {
config.Tolerance = 0
}
if config.Tolerance > 1 {
config.Tolerance = 1
}
config.Expected = v.Expected
if config.Expected < 0 {
config.Expected = 0
}
2021-06-05 08:00:14 -04:00
config.MaxRTT = int64(v.MaxRTT)
if config.MaxRTT < 0 {
config.MaxRTT = 0
}
config.Baselines = make([]int64, 0)
for _, b := range v.Baselines {
if b <= 0 {
continue
}
2021-06-05 08:00:14 -04:00
config.Baselines = append(config.Baselines, int64(b))
}
return config, nil
}