router_strategy.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package router
  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/infra/conf/cfgcommon/duration"
  6. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/loader"
  7. "github.com/v2fly/v2ray-core/v4/app/router"
  8. )
  9. const (
  10. strategyRandom string = "random"
  11. strategyLeastLoad string = "leastload"
  12. strategyLeastPing string = "leastping"
  13. )
  14. var (
  15. strategyConfigLoader = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{
  16. strategyRandom: func() interface{} { return new(strategyEmptyConfig) },
  17. strategyLeastLoad: func() interface{} { return new(strategyLeastLoadConfig) },
  18. strategyLeastPing: func() interface{} { return new(strategyLeastPingConfig) },
  19. }, "type", "settings")
  20. )
  21. type strategyEmptyConfig struct {
  22. }
  23. func (v *strategyEmptyConfig) Build() (proto.Message, error) {
  24. return nil, nil
  25. }
  26. type strategyLeastLoadConfig struct {
  27. // weight settings
  28. Costs []*router.StrategyWeight `json:"costs,omitempty"`
  29. // ping rtt baselines
  30. Baselines []duration.Duration `json:"baselines,omitempty"`
  31. // expected nodes count to select
  32. Expected int32 `json:"expected,omitempty"`
  33. // max acceptable rtt, filter away high delay nodes. defalut 0
  34. MaxRTT duration.Duration `json:"maxRTT,omitempty"`
  35. // acceptable failure rate
  36. Tolerance float64 `json:"tolerance,omitempty"`
  37. ObserverTag string `json:"observerTag,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. config.ObserverTag = v.ObserverTag
  62. if config.Tolerance < 0 {
  63. config.Tolerance = 0
  64. }
  65. if config.Tolerance > 1 {
  66. config.Tolerance = 1
  67. }
  68. config.Expected = v.Expected
  69. if config.Expected < 0 {
  70. config.Expected = 0
  71. }
  72. config.MaxRTT = int64(v.MaxRTT)
  73. if config.MaxRTT < 0 {
  74. config.MaxRTT = 0
  75. }
  76. config.Baselines = make([]int64, 0)
  77. for _, b := range v.Baselines {
  78. if b <= 0 {
  79. continue
  80. }
  81. config.Baselines = append(config.Baselines, int64(b))
  82. }
  83. return config, nil
  84. }
  85. type strategyLeastPingConfig struct {
  86. ObserverTag string `json:"observerTag,omitempty"`
  87. }
  88. func (s strategyLeastPingConfig) Build() (proto.Message, error) {
  89. return &router.StrategyLeastPingConfig{ObserverTag: s.ObserverTag}, nil
  90. }