]> Sergey Matveev's repositories - btrtrc.git/blobdiff - smartban.go
Drop support for go 1.20
[btrtrc.git] / smartban.go
index 9f43104f338e069b061b90360bd33d3b37a291bd..034a702d950372400c8dddb002fa1121304073e5 100644 (file)
@@ -3,19 +3,22 @@ package torrent
 import (
        "bytes"
        "crypto/sha1"
+       "net/netip"
+
+       g "github.com/anacrolix/generics"
 
        "github.com/anacrolix/torrent/smartban"
 )
 
-type banPrefix = string
+type bannableAddr = netip.Addr
 
-type smartBanCache = smartban.Cache[banPrefix, RequestIndex, [sha1.Size]byte]
+type smartBanCache = smartban.Cache[bannableAddr, RequestIndex, [sha1.Size]byte]
 
 type blockCheckingWriter struct {
        cache        *smartBanCache
        requestIndex RequestIndex
        // Peers that didn't match blocks written now.
-       badPeers    map[banPrefix]struct{}
+       badPeers    map[bannableAddr]struct{}
        blockBuffer bytes.Buffer
        chunkSize   int
 }
@@ -23,7 +26,7 @@ type blockCheckingWriter struct {
 func (me *blockCheckingWriter) checkBlock() {
        b := me.blockBuffer.Next(me.chunkSize)
        for _, peer := range me.cache.CheckBlock(me.requestIndex, b) {
-               me.badPeers[peer] = struct{}{}
+               g.MakeMapIfNilAndSet(&me.badPeers, peer, struct{}{})
        }
        me.requestIndex++
 }