router_strategy.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package conf
  2. import (
  3. "github.com/golang/protobuf/proto"
  4. "github.com/v2fly/v2ray-core/v4/app/observatory/burst"
  5. "github.com/v2fly/v2ray-core/v4/app/router"
  6. )
  7. const (
  8. strategyRandom string = "random"
  9. strategyLeastLoad string = "leastload"
  10. strategyLeastPing string = "leastping"
  11. )
  12. var (
  13. strategyConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{
  14. strategyRandom: func() interface{} { return new(strategyEmptyConfig) },
  15. strategyLeastLoad: func() interface{} { return new(strategyLeastLoadConfig) },
  16. strategyLeastPing: func() interface{} { return new(strategyLeastPingConfig) },
  17. }, "type", "settings")
  18. )
  19. type strategyEmptyConfig struct {
  20. }
  21. func (v *strategyEmptyConfig) Build() (proto.Message, error) {
  22. return nil, nil
  23. }
  24. type strategyLeastLoadConfig struct {
  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. ObserverTag string `json:"observerTag,omitempty"`
  36. }
  37. // healthCheckSettings holds settings for health Checker
  38. type healthCheckSettings struct {
  39. Destination string `json:"destination"`
  40. Connectivity string `json:"connectivity"`
  41. Interval Duration `json:"interval"`
  42. SamplingCount int `json:"sampling"`
  43. Timeout Duration `json:"timeout"`
  44. }
  45. func (h healthCheckSettings) Build() (proto.Message, error) {
  46. return &burst.HealthPingConfig{
  47. Destination: h.Destination,
  48. Connectivity: h.Connectivity,
  49. Interval: int64(h.Interval),
  50. Timeout: int64(h.Timeout),
  51. SamplingCount: int32(h.SamplingCount),
  52. }, nil
  53. }
  54. // Build implements Buildable.
  55. func (v *strategyLeastLoadConfig) Build() (proto.Message, error) {
  56. config := &router.StrategyLeastLoadConfig{}
  57. config.Costs = v.Costs
  58. config.Tolerance = float32(v.Tolerance)
  59. config.ObserverTag = v.ObserverTag
  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. }
  83. type strategyLeastPingConfig struct {
  84. ObserverTag string `json:"observerTag,omitempty"`
  85. }
  86. func (s strategyLeastPingConfig) Build() (proto.Message, error) {
  87. return &router.StrategyLeastPingConfig{ObserverTag: s.ObserverTag}, nil
  88. }