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