diff options
author | Kris Hamoud <[email protected]> | 2016-09-27 14:38:20 -0700 |
---|---|---|
committer | Kris Hamoud <[email protected]> | 2016-09-28 04:09:46 -0700 |
commit | be1c57acfec84be8f4f942c4fb528e22ffd269e7 (patch) | |
tree | b42d7b24f04c96280f33bebced68b1c8de2cee71 /caddyhttp | |
parent | 4adbcd256583f83144a74f65ddcd8a769a840071 (diff) | |
download | caddy-be1c57acfec84be8f4f942c4fb528e22ffd269e7.tar.gz caddy-be1c57acfec84be8f4f942c4fb528e22ffd269e7.zip |
1136 fix
logic change
Diffstat (limited to 'caddyhttp')
-rw-r--r-- | caddyhttp/proxy/policy.go | 13 | ||||
-rw-r--r-- | caddyhttp/proxy/policy_test.go | 12 |
2 files changed, 10 insertions, 15 deletions
diff --git a/caddyhttp/proxy/policy.go b/caddyhttp/proxy/policy.go index 1737c6c58..337ad1e83 100644 --- a/caddyhttp/proxy/policy.go +++ b/caddyhttp/proxy/policy.go @@ -121,18 +121,13 @@ func (r *IPHash) Select(pool HostPool, request *http.Request) *UpstreamHost { if err != nil { clientIP = request.RemoteAddr } - hash := hash(clientIP) - for { - if poolLen == 0 { - break - } - index := hash % poolLen - host := pool[index] + index := hash(clientIP) % poolLen + for i := uint32(0); i < poolLen; i++ { + index += i + host := pool[index%poolLen] if host.Available() { return host } - pool = append(pool[:index], pool[index+1:]...) - poolLen-- } return nil } diff --git a/caddyhttp/proxy/policy_test.go b/caddyhttp/proxy/policy_test.go index a736d8cac..2a8dfe61b 100644 --- a/caddyhttp/proxy/policy_test.go +++ b/caddyhttp/proxy/policy_test.go @@ -163,14 +163,14 @@ func TestIPHashPolicy(t *testing.T) { request.RemoteAddr = "172.0.0.1" pool[1].Unhealthy = true h = ipHash.Select(pool, request) - if h != pool[0] { - t.Error("Expected ip hash policy host to be the first host.") + if h != pool[2] { + t.Error("Expected ip hash policy host to be the third host.") } request.RemoteAddr = "172.0.0.2" h = ipHash.Select(pool, request) - if h != pool[1] { - t.Error("Expected ip hash policy host to be the second host.") + if h != pool[2] { + t.Error("Expected ip hash policy host to be the third host.") } pool[1].Unhealthy = false @@ -182,8 +182,8 @@ func TestIPHashPolicy(t *testing.T) { } request.RemoteAddr = "172.0.0.4" h = ipHash.Select(pool, request) - if h != pool[0] { - t.Error("Expected ip hash policy host to be the first host.") + if h != pool[1] { + 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 |