balancing.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //go:build !confonly
  2. // +build !confonly
  3. package router
  4. import (
  5. "context"
  6. "github.com/v2fly/v2ray-core/v4/features/extension"
  7. "github.com/v2fly/v2ray-core/v4/features/outbound"
  8. )
  9. type BalancingStrategy interface {
  10. PickOutbound([]string) string
  11. }
  12. type BalancingPrincipleTarget interface {
  13. GetPrincipleTarget([]string) []string
  14. }
  15. type Balancer struct {
  16. selectors []string
  17. strategy BalancingStrategy
  18. ohm outbound.Manager
  19. fallbackTag string
  20. override override
  21. }
  22. // PickOutbound picks the tag of an outbound
  23. func (b *Balancer) PickOutbound() (string, error) {
  24. candidates, err := b.SelectOutbounds()
  25. if err != nil {
  26. if b.fallbackTag != "" {
  27. newError("fallback to [", b.fallbackTag, "], due to error: ", err).AtInfo().WriteToLog()
  28. return b.fallbackTag, nil
  29. }
  30. return "", err
  31. }
  32. var tag string
  33. if o := b.override.Get(); o != "" {
  34. tag = o
  35. } else {
  36. tag = b.strategy.PickOutbound(candidates)
  37. }
  38. if tag == "" {
  39. if b.fallbackTag != "" {
  40. newError("fallback to [", b.fallbackTag, "], due to empty tag returned").AtInfo().WriteToLog()
  41. return b.fallbackTag, nil
  42. }
  43. // will use default handler
  44. return "", newError("balancing strategy returns empty tag")
  45. }
  46. return tag, nil
  47. }
  48. func (b *Balancer) InjectContext(ctx context.Context) {
  49. if contextReceiver, ok := b.strategy.(extension.ContextReceiver); ok {
  50. contextReceiver.InjectContext(ctx)
  51. }
  52. }
  53. // SelectOutbounds select outbounds with selectors of the Balancer
  54. func (b *Balancer) SelectOutbounds() ([]string, error) {
  55. hs, ok := b.ohm.(outbound.HandlerSelector)
  56. if !ok {
  57. return nil, newError("outbound.Manager is not a HandlerSelector")
  58. }
  59. tags := hs.Select(b.selectors)
  60. return tags, nil
  61. }
  62. // GetPrincipleTarget implements routing.BalancerPrincipleTarget
  63. func (r *Router) GetPrincipleTarget(tag string) ([]string, error) {
  64. if b, ok := r.balancers[tag]; ok {
  65. if s, ok := b.strategy.(BalancingPrincipleTarget); ok {
  66. candidates, err := b.SelectOutbounds()
  67. if err != nil {
  68. return nil, newError("unable to select outbounds").Base(err)
  69. }
  70. return s.GetPrincipleTarget(candidates), nil
  71. }
  72. return nil, newError("unsupported GetPrincipleTarget")
  73. }
  74. return nil, newError("cannot find tag")
  75. }
  76. // SetOverrideTarget implements routing.BalancerOverrider
  77. func (r *Router) SetOverrideTarget(tag, target string) error {
  78. if b, ok := r.balancers[tag]; ok {
  79. b.override.Put(target)
  80. return nil
  81. }
  82. return newError("cannot find tag")
  83. }
  84. // GetOverrideTarget implements routing.BalancerOverrider
  85. func (r *Router) GetOverrideTarget(tag string) (string, error) {
  86. if b, ok := r.balancers[tag]; ok {
  87. return b.override.Get(), nil
  88. }
  89. return "", newError("cannot find tag")
  90. }