router_strategy.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package conf
  2. import (
  3. "google.golang.org/protobuf/proto"
  4. "github.com/xtls/xray-core/app/observatory/burst"
  5. "github.com/xtls/xray-core/app/router"
  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. ExpectedResponseCode []int32 `json:"expectedResponseCode"`
  47. }
  48. func (h healthCheckSettings) Build() (proto.Message, error) {
  49. return &burst.HealthPingConfig{
  50. Destination: h.Destination,
  51. Connectivity: h.Connectivity,
  52. Interval: int64(h.Interval),
  53. Timeout: int64(h.Timeout),
  54. SamplingCount: int32(h.SamplingCount),
  55. ExpectedResponseCode: h.ExpectedResponseCode,
  56. }, nil
  57. }
  58. // Build implements Buildable.
  59. func (v *strategyLeastLoadConfig) Build() (proto.Message, error) {
  60. config := &router.StrategyLeastLoadConfig{}
  61. config.Costs = v.Costs
  62. config.Tolerance = float32(v.Tolerance)
  63. if config.Tolerance < 0 {
  64. config.Tolerance = 0
  65. }
  66. if config.Tolerance > 1 {
  67. config.Tolerance = 1
  68. }
  69. config.Expected = v.Expected
  70. if config.Expected < 0 {
  71. config.Expected = 0
  72. }
  73. config.MaxRTT = int64(v.MaxRTT)
  74. if config.MaxRTT < 0 {
  75. config.MaxRTT = 0
  76. }
  77. config.Baselines = make([]int64, 0)
  78. for _, b := range v.Baselines {
  79. if b <= 0 {
  80. continue
  81. }
  82. config.Baselines = append(config.Baselines, int64(b))
  83. }
  84. return config, nil
  85. }