github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/infra/conf/router_strategy.go (about) 1 package conf 2 3 import ( 4 "google.golang.org/protobuf/proto" 5 6 "github.com/xtls/xray-core/app/router" 7 "github.com/xtls/xray-core/app/observatory/burst" 8 "github.com/xtls/xray-core/infra/conf/cfgcommon/duration" 9 ) 10 11 const ( 12 strategyRandom string = "random" 13 strategyLeastPing string = "leastping" 14 strategyRoundRobin string = "roundrobin" 15 strategyLeastLoad string = "leastload" 16 ) 17 18 var ( 19 strategyConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{ 20 strategyRandom: func() interface{} { return new(strategyEmptyConfig) }, 21 strategyLeastPing: func() interface{} { return new(strategyEmptyConfig) }, 22 strategyRoundRobin: func() interface{} { return new(strategyEmptyConfig) }, 23 strategyLeastLoad: func() interface{} { return new(strategyLeastLoadConfig) }, 24 }, "type", "settings") 25 ) 26 27 type strategyEmptyConfig struct { 28 } 29 30 func (v *strategyEmptyConfig) Build() (proto.Message, error) { 31 return nil, nil 32 } 33 34 type strategyLeastLoadConfig struct { 35 // weight settings 36 Costs []*router.StrategyWeight `json:"costs,omitempty"` 37 // ping rtt baselines 38 Baselines []duration.Duration `json:"baselines,omitempty"` 39 // expected nodes count to select 40 Expected int32 `json:"expected,omitempty"` 41 // max acceptable rtt, filter away high delay nodes. default 0 42 MaxRTT duration.Duration `json:"maxRTT,omitempty"` 43 // acceptable failure rate 44 Tolerance float64 `json:"tolerance,omitempty"` 45 } 46 47 // healthCheckSettings holds settings for health Checker 48 type healthCheckSettings struct { 49 Destination string `json:"destination"` 50 Connectivity string `json:"connectivity"` 51 Interval duration.Duration `json:"interval"` 52 SamplingCount int `json:"sampling"` 53 Timeout duration.Duration `json:"timeout"` 54 } 55 56 func (h healthCheckSettings) Build() (proto.Message, error) { 57 return &burst.HealthPingConfig{ 58 Destination: h.Destination, 59 Connectivity: h.Connectivity, 60 Interval: int64(h.Interval), 61 Timeout: int64(h.Timeout), 62 SamplingCount: int32(h.SamplingCount), 63 }, nil 64 } 65 66 // Build implements Buildable. 67 func (v *strategyLeastLoadConfig) Build() (proto.Message, error) { 68 config := &router.StrategyLeastLoadConfig{} 69 config.Costs = v.Costs 70 config.Tolerance = float32(v.Tolerance) 71 if config.Tolerance < 0 { 72 config.Tolerance = 0 73 } 74 if config.Tolerance > 1 { 75 config.Tolerance = 1 76 } 77 config.Expected = v.Expected 78 if config.Expected < 0 { 79 config.Expected = 0 80 } 81 config.MaxRTT = int64(v.MaxRTT) 82 if config.MaxRTT < 0 { 83 config.MaxRTT = 0 84 } 85 config.Baselines = make([]int64, 0) 86 for _, b := range v.Baselines { 87 if b <= 0 { 88 continue 89 } 90 config.Baselines = append(config.Baselines, int64(b)) 91 } 92 return config, nil 93 }