router_strategy.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/app/router"
  6. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/duration"
  7. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/loader"
  8. )
  9. const (
  10. strategyRandom string = "random"
  11. strategyLeastLoad string = "leastload"
  12. strategyLeastPing string = "leastping"
  13. )
  14. var strategyConfigLoader = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{
  15. strategyRandom: func() interface{} { return new(strategyEmptyConfig) },
  16. strategyLeastLoad: func() interface{} { return new(strategyLeastLoadConfig) },
  17. strategyLeastPing: func() interface{} { return new(strategyLeastPingConfig) },
  18. }, "type", "settings")
  19. type strategyEmptyConfig struct{}
  20. func (v *strategyEmptyConfig) Build() (proto.Message, error) {
  21. return nil, nil
  22. }
  23. type strategyLeastLoadConfig struct {
  24. // weight settings
  25. Costs []*router.StrategyWeight `json:"costs,omitempty"`
  26. // ping rtt baselines
  27. Baselines []duration.Duration `json:"baselines,omitempty"`
  28. // expected nodes count to select
  29. Expected int32 `json:"expected,omitempty"`
  30. // max acceptable rtt, filter away high delay nodes. defalut 0
  31. MaxRTT duration.Duration `json:"maxRTT,omitempty"`
  32. // acceptable failure rate
  33. Tolerance float64 `json:"tolerance,omitempty"`
  34. ObserverTag string `json:"observerTag,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.Duration `json:"interval"`
  41. SamplingCount int `json:"sampling"`
  42. Timeout duration.Duration `json:"timeout"`
  43. }
  44. func (h HealthCheckSettings) Build() (proto.Message, error) {
  45. return &burst.HealthPingConfig{
  46. Destination: h.Destination,
  47. Connectivity: h.Connectivity,
  48. Interval: int64(h.Interval),
  49. Timeout: int64(h.Timeout),
  50. SamplingCount: int32(h.SamplingCount),
  51. }, nil
  52. }
  53. // Build implements Buildable.
  54. func (v *strategyLeastLoadConfig) Build() (proto.Message, error) {
  55. config := &router.StrategyLeastLoadConfig{}
  56. config.Costs = v.Costs
  57. config.Tolerance = float32(v.Tolerance)
  58. config.ObserverTag = v.ObserverTag
  59. if config.Tolerance < 0 {
  60. config.Tolerance = 0
  61. }
  62. if config.Tolerance > 1 {
  63. config.Tolerance = 1
  64. }
  65. config.Expected = v.Expected
  66. if config.Expected < 0 {
  67. config.Expected = 0
  68. }
  69. config.MaxRTT = int64(v.MaxRTT)
  70. if config.MaxRTT < 0 {
  71. config.MaxRTT = 0
  72. }
  73. config.Baselines = make([]int64, 0)
  74. for _, b := range v.Baselines {
  75. if b <= 0 {
  76. continue
  77. }
  78. config.Baselines = append(config.Baselines, int64(b))
  79. }
  80. return config, nil
  81. }
  82. type strategyLeastPingConfig struct {
  83. ObserverTag string `json:"observerTag,omitempty"`
  84. }
  85. func (s strategyLeastPingConfig) Build() (proto.Message, error) {
  86. return &router.StrategyLeastPingConfig{ObserverTag: s.ObserverTag}, nil
  87. }