From f3147e53e3c6d2e98ace1fd541f637f322d32d53 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sun, 18 Feb 2024 12:59:42 +1100 Subject: [PATCH] Switch to xxHash for smartban cache --- client.go | 11 +++++++++-- smartban.go | 3 +-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index 62c0d2b4..7aab0402 100644 --- a/client.go +++ b/client.go @@ -4,12 +4,12 @@ import ( "bufio" "context" "crypto/rand" - "crypto/sha1" "encoding/binary" "encoding/hex" "errors" "expvar" "fmt" + "github.com/cespare/xxhash" "io" "math" "net" @@ -1301,7 +1301,14 @@ func (cl *Client) newTorrentOpt(opts AddTorrentOpts) (t *Torrent) { webSeeds: make(map[string]*Peer), gotMetainfoC: make(chan struct{}), } - t.smartBanCache.Hash = sha1.Sum + var salt [8]byte + rand.Read(salt[:]) + t.smartBanCache.Hash = func(b []byte) uint64 { + h := xxhash.New() + h.Write(salt[:]) + h.Write(b) + return h.Sum64() + } t.smartBanCache.Init() t.networkingEnabled.Set() t.logger = cl.logger.WithDefaultLevel(log.Debug) diff --git a/smartban.go b/smartban.go index 034a702d..5515ded5 100644 --- a/smartban.go +++ b/smartban.go @@ -2,7 +2,6 @@ package torrent import ( "bytes" - "crypto/sha1" "net/netip" g "github.com/anacrolix/generics" @@ -12,7 +11,7 @@ import ( type bannableAddr = netip.Addr -type smartBanCache = smartban.Cache[bannableAddr, RequestIndex, [sha1.Size]byte] +type smartBanCache = smartban.Cache[bannableAddr, RequestIndex, uint64] type blockCheckingWriter struct { cache *smartBanCache -- 2.48.1