add cached shuffle mode to try to use as many different connecitons

This commit is contained in:
mantaohuang 2020-04-09 18:46:11 -04:00
parent f7e572b309
commit 524a4680d4
2 changed files with 33 additions and 14 deletions

View File

@ -2,5 +2,5 @@ proxy:{% for idx in running_idx %}
- url: "socks5://192.168.122.128:108{{idx}}" - url: "socks5://192.168.122.128:108{{idx}}"
weight: 5 weight: 5
{% end %} {% end %}
sticky: false load-balance-mode: "cached-shuffle"
cache-clean-interval: 0 cache-clean-interval: 0

View File

@ -16,7 +16,9 @@ import (
) )
const ( const (
URL = "http://connectivitycheck.gstatic.com/generate_204" URL = "http://connectivitycheck.gstatic.com/generate_204"
cachedShuffle = "cached-shuffle"
sticky = "sticky"
) )
func proxyTestStatusCode(proxyURL string, URL string, StatusCode int) bool { func proxyTestStatusCode(proxyURL string, URL string, StatusCode int) bool {
@ -84,7 +86,7 @@ type proxyInst struct {
type ProxyManager struct { type ProxyManager struct {
Proxys []proxyInst Proxys []proxyInst
Cache map[string]int Cache map[string]int
Sticky bool LoadBalanceMode string
CacheCleanInterval int CacheCleanInterval int
Chooser wr.Chooser Chooser wr.Chooser
mux sync.Mutex mux sync.Mutex
@ -94,8 +96,8 @@ type proxyConf struct {
URL string `yaml:"url"` URL string `yaml:"url"`
Weight int `yaml:"weight"` Weight int `yaml:"weight"`
} `yaml:"proxy"` } `yaml:"proxy"`
Sticky bool `yaml:"sticky"` LoadBalanceMode string `yaml:"load-balance-mode"`
CacheCleanInterval int `yaml:"cache-clean-interval"` CacheCleanInterval int `yaml:"cache-clean-interval"`
} }
func NewPM(confFp string) (*ProxyManager, error) { func NewPM(confFp string) (*ProxyManager, error) {
@ -120,7 +122,7 @@ func NewPM(confFp string) (*ProxyManager, error) {
pm.Proxys = append(pm.Proxys, pi) pm.Proxys = append(pm.Proxys, pi)
chooseArr = append(chooseArr, wr.Choice{Item: idx, Weight: uint(pi.Weight)}) chooseArr = append(chooseArr, wr.Choice{Item: idx, Weight: uint(pi.Weight)})
} }
pm.Sticky = cfg.Sticky pm.LoadBalanceMode = cfg.LoadBalanceMode
pm.CacheCleanInterval = cfg.CacheCleanInterval pm.CacheCleanInterval = cfg.CacheCleanInterval
pm.Cache = make(map[string]int) pm.Cache = make(map[string]int)
pm.Chooser = wr.NewChooser(chooseArr...) pm.Chooser = wr.NewChooser(chooseArr...)
@ -132,17 +134,31 @@ func (pm *ProxyManager) Get(addr string) string {
pm.mux.Lock() pm.mux.Lock()
defer pm.mux.Unlock() defer pm.mux.Unlock()
// fmt.Println(pm.Cache) // fmt.Println(pm.Cache)
if pm.Sticky { if pm.LoadBalanceMode == sticky {
idx, ok := pm.Cache[addr] idx, ok := pm.Cache[addr]
if ok { if ok {
// fmt.Println("match addr", addr, "using:", idx) // fmt.Println("match addr", addr, "using:", idx)
return pm.Proxys[idx].URL return pm.Proxys[idx].URL
} else {
idx := pm.Chooser.Pick().(int)
pm.Cache[addr] = idx
return pm.Proxys[idx].URL
} }
} else if pm.LoadBalanceMode == cachedShuffle {
idx, ok := pm.Cache[addr]
if ok {
pm.Cache[addr] = (idx + 1) % (len(pm.Proxys))
return pm.Proxys[pm.Cache[addr]].URL
} else {
idx := pm.Chooser.Pick().(int)
pm.Cache[addr] = idx
return pm.Proxys[idx].URL
}
} else {
idx := pm.Chooser.Pick().(int)
return pm.Proxys[idx].URL
} }
idx := pm.Chooser.Pick().(int) //return ""
pm.Cache[addr] = idx
// fmt.Println("addr", addr, "using:", idx)
return pm.Proxys[idx].URL
} }
func (pm *ProxyManager) ClearCache() { func (pm *ProxyManager) ClearCache() {
@ -190,10 +206,13 @@ func main() {
if err != nil { if err != nil {
return return
} }
if pm.Sticky { fmt.Printf("Load balance mode: ")
fmt.Printf("Sticky with interval %ds.\n", pm.CacheCleanInterval) if pm.LoadBalanceMode == sticky {
fmt.Printf("sticky with cache clearing interval: %ds.\n", pm.CacheCleanInterval)
} else if pm.LoadBalanceMode == cachedShuffle {
fmt.Println("cached shuffling.")
} else { } else {
fmt.Println("Randomize every connection.") fmt.Println("randomize every connection.")
} }
conf := &Config{PM: pm} conf := &Config{PM: pm}
server, err := New(conf) server, err := New(conf)