mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-29 12:16:16 +08:00
1136 fix
logic change
This commit is contained in:
parent
4adbcd2565
commit
be1c57acfe
|
@ -121,18 +121,13 @@ func (r *IPHash) Select(pool HostPool, request *http.Request) *UpstreamHost {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
clientIP = request.RemoteAddr
|
clientIP = request.RemoteAddr
|
||||||
}
|
}
|
||||||
hash := hash(clientIP)
|
index := hash(clientIP) % poolLen
|
||||||
for {
|
for i := uint32(0); i < poolLen; i++ {
|
||||||
if poolLen == 0 {
|
index += i
|
||||||
break
|
host := pool[index%poolLen]
|
||||||
}
|
|
||||||
index := hash % poolLen
|
|
||||||
host := pool[index]
|
|
||||||
if host.Available() {
|
if host.Available() {
|
||||||
return host
|
return host
|
||||||
}
|
}
|
||||||
pool = append(pool[:index], pool[index+1:]...)
|
|
||||||
poolLen--
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -163,14 +163,14 @@ func TestIPHashPolicy(t *testing.T) {
|
||||||
request.RemoteAddr = "172.0.0.1"
|
request.RemoteAddr = "172.0.0.1"
|
||||||
pool[1].Unhealthy = true
|
pool[1].Unhealthy = true
|
||||||
h = ipHash.Select(pool, request)
|
h = ipHash.Select(pool, request)
|
||||||
if h != pool[0] {
|
if h != pool[2] {
|
||||||
t.Error("Expected ip hash policy host to be the first host.")
|
t.Error("Expected ip hash policy host to be the third host.")
|
||||||
}
|
}
|
||||||
|
|
||||||
request.RemoteAddr = "172.0.0.2"
|
request.RemoteAddr = "172.0.0.2"
|
||||||
h = ipHash.Select(pool, request)
|
h = ipHash.Select(pool, request)
|
||||||
if h != pool[1] {
|
if h != pool[2] {
|
||||||
t.Error("Expected ip hash policy host to be the second host.")
|
t.Error("Expected ip hash policy host to be the third host.")
|
||||||
}
|
}
|
||||||
pool[1].Unhealthy = false
|
pool[1].Unhealthy = false
|
||||||
|
|
||||||
|
@ -182,8 +182,8 @@ func TestIPHashPolicy(t *testing.T) {
|
||||||
}
|
}
|
||||||
request.RemoteAddr = "172.0.0.4"
|
request.RemoteAddr = "172.0.0.4"
|
||||||
h = ipHash.Select(pool, request)
|
h = ipHash.Select(pool, request)
|
||||||
if h != pool[0] {
|
if h != pool[1] {
|
||||||
t.Error("Expected ip hash policy host to be the first host.")
|
t.Error("Expected ip hash policy host to be the second host.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// We should be able to resize the host pool and still be able to predict
|
// We should be able to resize the host pool and still be able to predict
|
||||||
|
|
Loading…
Reference in New Issue
Block a user