2021-04-08 16:08:20 -04:00
|
|
|
package conf
|
|
|
|
|
2021-01-29 19:31:11 -05:00
|
|
|
import (
|
|
|
|
"github.com/golang/protobuf/proto"
|
2021-06-04 19:42:53 -04:00
|
|
|
|
2021-01-29 19:31:11 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/app/router"
|
|
|
|
)
|
|
|
|
|
2021-04-08 16:08:20 -04:00
|
|
|
const (
|
|
|
|
strategyRandom string = "random"
|
2021-01-29 19:31:11 -05:00
|
|
|
strategyLeastLoad string = "leastload"
|
2021-04-08 16:08:20 -04:00
|
|
|
strategyLeastPing string = "leastping"
|
|
|
|
)
|
2021-01-29 19:31:11 -05:00
|
|
|
|
|
|
|
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"`
|
2021-01-29 19:31:11 -05:00
|
|
|
// weight settings
|
|
|
|
Costs []*router.StrategyWeight `json:"costs,omitempty"`
|
2021-06-05 08:00:14 -04:00
|
|
|
// ping rtt baselines
|
|
|
|
Baselines []Duration `json:"baselines,omitempty"`
|
2021-01-29 19:31:11 -05:00
|
|
|
// 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"`
|
2021-01-29 19:31:11 -05:00
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
2021-01-29 19:31:11 -05:00
|
|
|
// 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),
|
2021-01-29 19:31:11 -05:00
|
|
|
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)
|
2021-01-29 19:31:11 -05:00
|
|
|
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))
|
2021-01-29 19:31:11 -05:00
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|