loadbalance.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package loadbalance
  2. import (
  3. "strings"
  4. "sync"
  5. "sync/atomic"
  6. "imuslab.com/zoraxy/mod/dynamicproxy/dpcore"
  7. "imuslab.com/zoraxy/mod/geodb"
  8. "imuslab.com/zoraxy/mod/info/logger"
  9. )
  10. /*
  11. Load Balancer
  12. Handleing load balance request for upstream destinations
  13. */
  14. type Options struct {
  15. UseActiveHealthCheck bool //Use active health check, default to false
  16. Geodb *geodb.Store //GeoIP resolver for checking incoming request origin country
  17. Logger *logger.Logger
  18. }
  19. type RouteManager struct {
  20. LoadBalanceMap sync.Map //Sync map to store the last load balance state of a given node
  21. OnlineStatusMap sync.Map //Sync map to store the online status of a given ip address or domain name
  22. onlineStatusTickerStop chan bool //Stopping channel for the online status pinger
  23. Options Options //Options for the load balancer
  24. }
  25. /* Upstream or Origin Server */
  26. type Upstream struct {
  27. //Upstream Proxy Configs
  28. OriginIpOrDomain string //Target IP address or domain name with port
  29. RequireTLS bool //Require TLS connection
  30. SkipCertValidations bool //Set to true to accept self signed certs
  31. SkipWebSocketOriginCheck bool //Skip origin check on websocket upgrade connections
  32. //Load balancing configs
  33. Weight int //Random weight for round robin, 0 for fallback only
  34. MaxConn int //Maxmium connection to this server, 0 for unlimited
  35. currentConnectionCounts atomic.Uint64 //Counter for number of client currently connected
  36. proxy *dpcore.ReverseProxy
  37. }
  38. // Create a new load balancer
  39. func NewLoadBalancer(options *Options) *RouteManager {
  40. onlineStatusCheckerStopChan := make(chan bool)
  41. return &RouteManager{
  42. LoadBalanceMap: sync.Map{},
  43. OnlineStatusMap: sync.Map{},
  44. onlineStatusTickerStop: onlineStatusCheckerStopChan,
  45. Options: *options,
  46. }
  47. }
  48. // UpstreamsReady checks if the group of upstreams contains at least one
  49. // origin server that is ready
  50. func (m *RouteManager) UpstreamsReady(upstreams []*Upstream) bool {
  51. for _, upstream := range upstreams {
  52. if upstream.IsReady() {
  53. return true
  54. }
  55. }
  56. return false
  57. }
  58. // String format and convert a list of upstream into a string representations
  59. func GetUpstreamsAsString(upstreams []*Upstream) string {
  60. targets := []string{}
  61. for _, upstream := range upstreams {
  62. targets = append(targets, upstream.String())
  63. }
  64. return strings.Join(targets, ", ")
  65. }
  66. func (m *RouteManager) Close() {
  67. if m.onlineStatusTickerStop != nil {
  68. m.onlineStatusTickerStop <- true
  69. }
  70. }
  71. // Print debug message
  72. func (m *RouteManager) debugPrint(message string, err error) {
  73. m.Options.Logger.PrintAndLog("LoadBalancer", message, err)
  74. }