loadbalance.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package loadbalance
  2. import (
  3. "imuslab.com/zoraxy/mod/geodb"
  4. "imuslab.com/zoraxy/mod/info/logger"
  5. "imuslab.com/zoraxy/mod/uptime"
  6. )
  7. /*
  8. Load Balancer
  9. Handleing load balance request for upstream destinations
  10. */
  11. type BalancePolicy int
  12. const (
  13. BalancePolicy_RoundRobin BalancePolicy = 0 //Round robin, will ignore upstream if down
  14. BalancePolicy_Fallback BalancePolicy = 1 //Fallback only. Will only switch to next node if the first one failed
  15. BalancePolicy_Random BalancePolicy = 2 //Random, randomly pick one from the list that is online
  16. BalancePolicy_GeoRegion BalancePolicy = 3 //Use the one defined for this geo-location, when down, pick the next avaible node
  17. )
  18. type LoadBalanceRule struct {
  19. Upstreams []string //Reverse proxy upstream servers
  20. LoadBalancePolicy BalancePolicy //Policy in deciding which target IP to proxy
  21. UseRegionLock bool //If this is enabled with BalancePolicy_Geo, when the main site failed, it will not pick another node
  22. UseStickySession bool //Use sticky session, if you are serving EU countries, make sure to add the "Do you want cookie" warning
  23. parent *RouteManager
  24. }
  25. type Options struct {
  26. Geodb *geodb.Store //GeoIP resolver for checking incoming request origin country
  27. UptimeMonitor *uptime.Monitor //For checking if the target is online, this might be nil when the module starts
  28. }
  29. type RouteManager struct {
  30. Options Options
  31. Logger *logger.Logger
  32. }
  33. // Create a new load balance route manager
  34. func NewRouteManager(options *Options, logger *logger.Logger) *RouteManager {
  35. newManager := RouteManager{
  36. Options: *options,
  37. Logger: logger,
  38. }
  39. logger.PrintAndLog("INFO", "Load Balance Route Manager started", nil)
  40. return &newManager
  41. }
  42. func (b *LoadBalanceRule) GetProxyTargetIP() {
  43. //TODO: Implement get proxy target IP logic here
  44. }
  45. // Print debug message
  46. func (m *RouteManager) debugPrint(message string, err error) {
  47. m.Logger.PrintAndLog("LB", message, err)
  48. }