router_strategy.go 2.6 KB

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