router_strategy.go 2.4 KB

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