From 600da361a21f22235a54c3ca234306909f37fd99 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Mon, 4 Mar 2024 09:44:38 +0700 Subject: [PATCH] save --- client.go | 5 +++-- torrent.go | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 938bd5f306..78cd6acac1 100644 --- a/client.go +++ b/client.go @@ -22,11 +22,9 @@ import ( "github.com/anacrolix/chansync/events" "github.com/anacrolix/dht/v2" "github.com/anacrolix/dht/v2/krpc" - . "github.com/anacrolix/generics" g "github.com/anacrolix/generics" "github.com/anacrolix/log" "github.com/anacrolix/missinggo/perf" - "github.com/anacrolix/missinggo/v2" "github.com/anacrolix/missinggo/v2/bitmap" "github.com/anacrolix/missinggo/v2/pproffd" "github.com/anacrolix/sync" @@ -35,6 +33,7 @@ import ( "github.com/dustin/go-humanize" gbtree "github.com/google/btree" "github.com/pion/datachannel" + "golang.org/x/sync/semaphore" "github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/internal/check" @@ -91,6 +90,7 @@ type Client struct { clientHolepunchAddrSets defaultLocalLtepProtocolMap LocalLtepProtocolMap + hashersLimit *semaphore.Weighted } type ipStr string @@ -198,6 +198,7 @@ func (cl *Client) announceKey() int32 { // Initializes a bare minimum Client. *Client and *ClientConfig must not be nil. func (cl *Client) init(cfg *ClientConfig) { cl.config = cfg + cl.hashersLimit = semaphore.NewWeighted(9000) g.MakeMap(&cl.dopplegangerAddrs) cl.torrents = make(map[metainfo.Hash]*Torrent) cl.activeAnnounceLimiter.SlotsPerKey = 2 diff --git a/torrent.go b/torrent.go index e27620f878..f370dc182d 100644 --- a/torrent.go +++ b/torrent.go @@ -2315,6 +2315,11 @@ func (t *Torrent) dropBannedPeers() { } func (t *Torrent) pieceHasher(index pieceIndex) { + if err := t.cl.hashersLimit.Acquire(context.Background(), 1); err != nil { + return + } + defer t.cl.hashersLimit.Release(1) + p := t.piece(index) sum, failedPeers, copyErr := t.hashPiece(index) correct := sum == *p.hash