loadbalance.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. return &RouteManager{
  41. LoadBalanceMap: sync.Map{},
  42. OnlineStatusMap: sync.Map{},
  43. onlineStatusTickerStop: nil,
  44. Options: *options,
  45. }
  46. }
  47. // UpstreamsReady checks if the group of upstreams contains at least one
  48. // origin server that is ready
  49. func (m *RouteManager) UpstreamsReady(upstreams []*Upstream) bool {
  50. for _, upstream := range upstreams {
  51. if upstream.IsReady() {
  52. return true
  53. }
  54. }
  55. return false
  56. }
  57. // String format and convert a list of upstream into a string representations
  58. func GetUpstreamsAsString(upstreams []*Upstream) string {
  59. targets := []string{}
  60. for _, upstream := range upstreams {
  61. targets = append(targets, upstream.String())
  62. }
  63. return strings.Join(targets, ", ")
  64. }
  65. func (m *RouteManager) Close() {
  66. if m.onlineStatusTickerStop != nil {
  67. m.onlineStatusTickerStop <- true
  68. }
  69. }
  70. // Print debug message
  71. func (m *RouteManager) debugPrint(message string, err error) {
  72. m.Options.Logger.PrintAndLog("LoadBalancer", message, err)
  73. }