1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package loadbalance
- import (
- "strings"
- "sync"
- "sync/atomic"
- "imuslab.com/zoraxy/mod/dynamicproxy/dpcore"
- "imuslab.com/zoraxy/mod/geodb"
- "imuslab.com/zoraxy/mod/info/logger"
- )
- type Options struct {
- UseActiveHealthCheck bool
- Geodb *geodb.Store
- Logger *logger.Logger
- }
- type RouteManager struct {
- LoadBalanceMap sync.Map
- OnlineStatusMap sync.Map
- onlineStatusTickerStop chan bool
- Options Options
- }
- type Upstream struct {
-
- OriginIpOrDomain string
- RequireTLS bool
- SkipCertValidations bool
- SkipWebSocketOriginCheck bool
-
- Priority int
- MaxConn int
- Disabled bool
- currentConnectionCounts atomic.Uint64
- proxy *dpcore.ReverseProxy
- }
- func NewLoadBalancer(options *Options) *RouteManager {
- onlineStatusCheckerStopChan := make(chan bool)
- return &RouteManager{
- LoadBalanceMap: sync.Map{},
- OnlineStatusMap: sync.Map{},
- onlineStatusTickerStop: onlineStatusCheckerStopChan,
- Options: *options,
- }
- }
- func (m *RouteManager) UpstreamsReady(upstreams []*Upstream) bool {
- for _, upstream := range upstreams {
- if upstream.Disabled {
-
- continue
- }
- if upstream.IsReady() {
- return true
- }
- }
- return false
- }
- func GetUpstreamsAsString(upstreams []*Upstream) string {
- targets := []string{}
- for _, upstream := range upstreams {
- targets = append(targets, upstream.String())
- }
- return strings.Join(targets, ", ")
- }
- func (m *RouteManager) Close() {
- if m.onlineStatusTickerStop != nil {
- m.onlineStatusTickerStop <- true
- }
- }
- func (m *RouteManager) debugPrint(message string, err error) {
- m.Options.Logger.PrintAndLog("LoadBalancer", message, err)
- }
|