diff options
author | Hayder <[email protected]> | 2024-03-29 12:56:18 -0400 |
---|---|---|
committer | GitHub <[email protected]> | 2024-03-29 10:56:18 -0600 |
commit | 74949fb0914d7d496efadf51ef2dd81e64b1b7d0 (patch) | |
tree | 42bbf0f28803bcc2307f2b777177fed9a5c4e046 /modules/caddyhttp/reverseproxy/selectionpolicies.go | |
parent | ddb1d2c2b11b860f1e91b43d830d283d1e1363b2 (diff) | |
download | caddy-74949fb0914d7d496efadf51ef2dd81e64b1b7d0.tar.gz caddy-74949fb0914d7d496efadf51ef2dd81e64b1b7d0.zip |
reverseproxy: Use xxhash instead of fnv32 for LB (#6203)
* Added Faster Non-cryptographic Hash Function for Load Balancing
* Ran golangci-lint
* Updated hash version and hash return type
Diffstat (limited to 'modules/caddyhttp/reverseproxy/selectionpolicies.go')
-rw-r--r-- | modules/caddyhttp/reverseproxy/selectionpolicies.go | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/modules/caddyhttp/reverseproxy/selectionpolicies.go b/modules/caddyhttp/reverseproxy/selectionpolicies.go index b6f807c16..e61b3e0f4 100644 --- a/modules/caddyhttp/reverseproxy/selectionpolicies.go +++ b/modules/caddyhttp/reverseproxy/selectionpolicies.go @@ -20,7 +20,6 @@ import ( "encoding/hex" "encoding/json" "fmt" - "hash/fnv" weakrand "math/rand" "net" "net/http" @@ -28,6 +27,8 @@ import ( "strings" "sync/atomic" + "github.com/cespare/xxhash/v2" + "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/caddyconfig" "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" @@ -807,7 +808,7 @@ func hostByHashing(pool []*Upstream, s string) *Upstream { // see https://medium.com/i0exception/rendezvous-hashing-8c00e2fb58b0, // https://randorithms.com/2020/12/26/rendezvous-hashing.html, // and https://en.wikipedia.org/wiki/Rendezvous_hashing. - var highestHash uint32 + var highestHash uint64 var upstream *Upstream for _, up := range pool { if !up.Available() { @@ -823,10 +824,10 @@ func hostByHashing(pool []*Upstream, s string) *Upstream { } // hash calculates a fast hash based on s. -func hash(s string) uint32 { - h := fnv.New32a() +func hash(s string) uint64 { + h := xxhash.New() _, _ = h.Write([]byte(s)) - return h.Sum32() + return h.Sum64() } func loadFallbackPolicy(d *caddyfile.Dispenser) (json.RawMessage, error) { |