From bd2839cb9ca1a337a1e302267c290aacd76ef993 Mon Sep 17 00:00:00 2001
From: Alex Sharov <AskAlexSharov@gmail.com>
Date: Wed, 13 Sep 2023 18:15:36 +0700
Subject: [PATCH] =?utf8?q?Configurable=20hashers=20amount=C2=A0per=20torre?=
 =?utf8?q?nt=20(#867)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

---
 .gitignore |  2 ++
 config.go  | 17 ++++++++++-------
 torrent.go |  2 +-
 3 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/.gitignore b/.gitignore
index d92c6d5b..0c15585d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
 .idea
 *-run.gob
 .envrc*
+.DS_Store
+go.work*
\ No newline at end of file
diff --git a/config.go b/config.go
index 0c029a7e..e2d0ea1e 100644
--- a/config.go
+++ b/config.go
@@ -188,6 +188,8 @@ type ClientConfig struct {
 	ICEServers []string
 
 	DialRateLimiter *rate.Limiter
+
+	PieceHashersPerTorrent int // default: 2
 }
 
 func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
@@ -225,13 +227,14 @@ func NewDefaultClientConfig() *ClientConfig {
 			Preferred:        true,
 			RequirePreferred: false,
 		},
-		CryptoSelector:        mse.DefaultCryptoSelector,
-		CryptoProvides:        mse.AllSupportedCrypto,
-		ListenPort:            42069,
-		Extensions:            defaultPeerExtensionBytes(),
-		AcceptPeerConnections: true,
-		MaxUnverifiedBytes:    64 << 20,
-		DialRateLimiter:       rate.NewLimiter(10, 10),
+		CryptoSelector:         mse.DefaultCryptoSelector,
+		CryptoProvides:         mse.AllSupportedCrypto,
+		ListenPort:             42069,
+		Extensions:             defaultPeerExtensionBytes(),
+		AcceptPeerConnections:  true,
+		MaxUnverifiedBytes:     64 << 20,
+		DialRateLimiter:        rate.NewLimiter(10, 10),
+		PieceHashersPerTorrent: 2,
 	}
 	cc.DhtStartingNodes = func(network string) dht.StartingNodesGetter {
 		return func() ([]dht.Addr, error) { return dht.GlobalBootstrapAddrs(network) }
diff --git a/torrent.go b/torrent.go
index 5f6ddf39..8b474521 100644
--- a/torrent.go
+++ b/torrent.go
@@ -2251,7 +2251,7 @@ func (t *Torrent) onIncompletePiece(piece pieceIndex) {
 }
 
 func (t *Torrent) tryCreateMorePieceHashers() {
-	for !t.closed.IsSet() && t.activePieceHashes < 2 && t.tryCreatePieceHasher() {
+	for !t.closed.IsSet() && t.activePieceHashes < t.cl.config.PieceHashersPerTorrent && t.tryCreatePieceHasher() {
 	}
 }
 
-- 
2.51.0