add fallback load balance mode
This commit is contained in:
parent
6cf38cd7f3
commit
e7f18216c8
@ -18,6 +18,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
cachedShuffle = "cached-shuffle"
|
cachedShuffle = "cached-shuffle"
|
||||||
sticky = "sticky"
|
sticky = "sticky"
|
||||||
|
fallback = "fallback"
|
||||||
)
|
)
|
||||||
|
|
||||||
type queueMapElement struct {
|
type queueMapElement struct {
|
||||||
@ -71,7 +72,7 @@ func (qm *queueMap) SetTrim(key string, value int, maxLength int) {
|
|||||||
qm.Set(key, value)
|
qm.Set(key, value)
|
||||||
qm.Trim(maxLength)
|
qm.Trim(maxLength)
|
||||||
}
|
}
|
||||||
func (qm *queueMap) Filter(f func(int) int) {
|
func (qm *queueMap) MapFilter(f func(int) int) {
|
||||||
e := qm.l.Front()
|
e := qm.l.Front()
|
||||||
for e != nil {
|
for e != nil {
|
||||||
key := e.Value.(string)
|
key := e.Value.(string)
|
||||||
@ -185,7 +186,7 @@ func (pm *ProxyManager) applyWeight() {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pm.Cache.Filter(filterFn)
|
pm.Cache.MapFilter(filterFn)
|
||||||
}
|
}
|
||||||
func (pm *ProxyManager) Get(addr string) (string, bool) {
|
func (pm *ProxyManager) Get(addr string) (string, bool) {
|
||||||
pm.mux.Lock()
|
pm.mux.Lock()
|
||||||
@ -210,6 +211,16 @@ func (pm *ProxyManager) Get(addr string) (string, bool) {
|
|||||||
}
|
}
|
||||||
pm.Cache.SetTrim(addr, idx, pm.CacheMaxLength)
|
pm.Cache.SetTrim(addr, idx, pm.CacheMaxLength)
|
||||||
return pm.Proxys[pm.enabledIndics[idx]].URL, true
|
return pm.Proxys[pm.enabledIndics[idx]].URL, true
|
||||||
|
} else if pm.LoadBalanceMode == fallback {
|
||||||
|
addr := "*"
|
||||||
|
idx, ok := pm.Cache.Get(addr)
|
||||||
|
if ok {
|
||||||
|
idx = (idx + 1) % (len(pm.enabledIndics))
|
||||||
|
} else {
|
||||||
|
idx = pm.Chooser.Pick().(int)
|
||||||
|
}
|
||||||
|
pm.Cache.SetTrim(addr, idx, pm.CacheMaxLength)
|
||||||
|
return pm.Proxys[pm.enabledIndics[idx]].URL, true
|
||||||
} else {
|
} else {
|
||||||
idx := pm.Chooser.Pick().(int)
|
idx := pm.Chooser.Pick().(int)
|
||||||
return pm.Proxys[pm.enabledIndics[idx]].URL, true
|
return pm.Proxys[pm.enabledIndics[idx]].URL, true
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user