@marcel91200 / caddy Mirror · read-only / commits / 90175571
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

reverseproxy: fix hash selection policy (#4137)

* caddyhttp: reverseproxy: fix hash selection policy Fixes: #4135 Test: go test './...' -count=1 * caddyhttp: reverseproxy: add test to catch #4135 If you revert the last commit, the test will fail.

Simão Gomes Viana committed Apr 29, 2021 at 18:52 UTC 90175571698d7cb0e4184d257a425f0bd11c713d
2 files changed +30 -2
modules/caddyhttp/reverseproxy/selectionpolicies.go
+1 -2
@@ -523,8 +523,7 @@ func hostByHashing(pool []*Upstream, s string) *Upstream {
523 }
524 index := hash(s) % poolLen
525 for i := uint32(0); i < poolLen; i++ {
526 - index += i
527 - upstream := pool[index%poolLen]
526 + upstream := pool[(index+i)%poolLen]
527 if upstream.Available() {
528 return upstream
529 }
modules/caddyhttp/reverseproxy/selectionpolicies_test.go
+29
@@ -198,6 +198,35 @@ func TestIPHashPolicy(t *testing.T) {
198 if h != nil {
199 t.Error("Expected ip hash policy host to be nil.")
200 }
201 +
202 + // Reproduce #4135
203 + pool = UpstreamPool{
204 + {Host: new(upstreamHost)},
205 + {Host: new(upstreamHost)},
206 + {Host: new(upstreamHost)},
207 + {Host: new(upstreamHost)},
208 + {Host: new(upstreamHost)},
209 + {Host: new(upstreamHost)},
210 + {Host: new(upstreamHost)},
211 + {Host: new(upstreamHost)},
212 + {Host: new(upstreamHost)},
213 + }
214 + pool[0].SetHealthy(false)
215 + pool[1].SetHealthy(false)
216 + pool[2].SetHealthy(false)
217 + pool[3].SetHealthy(false)
218 + pool[4].SetHealthy(false)
219 + pool[5].SetHealthy(false)
220 + pool[6].SetHealthy(false)
221 + pool[7].SetHealthy(false)
222 + pool[8].SetHealthy(true)
223 +
224 + // We should get a result back when there is one healthy host left.
225 + h = ipHash.Select(pool, req, nil)
226 + if h == nil {
227 + // If it is nil, it means we missed a host even though one is available
228 + t.Error("Expected ip hash policy host to not be nil, but it is nil.")
229 + }
230 }
231
232 func TestFirstPolicy(t *testing.T) {