]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add DialForPeerConns config option so you can disable peer conns
authorMatt Joiner <anacrolix@gmail.com>
Thu, 10 Jul 2025 14:12:12 +0000 (00:12 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 10 Jul 2025 14:12:12 +0000 (00:12 +1000)
client.go
config.go
issue211_test.go

index 8f856827081905588e2abbba2ad610c37f7b1355..442b7138b13c81a48aff7dbe06830c2467c10173 100644 (file)
--- a/client.go
+++ b/client.go
@@ -325,11 +325,12 @@ func NewClient(cfg *ClientConfig) (cl *Client, err error) {
        // Check for panics.
        cl.LocalPort()
 
-       for _, _s := range sockets {
-               s := _s // Go is fucking retarded.
+       for _, s := range sockets {
                cl.onClose = append(cl.onClose, func() { go s.Close() })
                if peerNetworkEnabled(parseNetworkString(s.Addr().Network()), cl.config) {
-                       cl.dialers = append(cl.dialers, s)
+                       if cl.config.DialForPeerConns {
+                               cl.dialers = append(cl.dialers, s)
+                       }
                        cl.listeners = append(cl.listeners, s)
                        if cl.config.AcceptPeerConnections {
                                go cl.acceptConnections(s)
@@ -390,6 +391,7 @@ func NewClient(cfg *ClientConfig) (cl *Client, err error) {
 
        cl.webseedRequestTimer = time.AfterFunc(webseedRequestUpdateTimerInterval, cl.updateWebseedRequestsTimerFunc)
 
+       err = cl.checkConfig()
        return
 }
 
@@ -1927,3 +1929,16 @@ func (cl *Client) underWebSeedHttpRequestLimit(key webseedHostKeyHandle) bool {
        panicif.Zero(key)
        return cl.numWebSeedRequests[key] < defaultRequestsPerWebseedHost
 }
+
+// Check for bad arrangements. This is a candidate for an error state check method.
+func (cl *Client) checkConfig() error {
+       if cl.config.DownloadRateLimiter.Limit() == 0 {
+               if len(cl.dialers) != 0 {
+                       return errors.New("download rate limit is zero, but dialers are set")
+               }
+               if len(cl.listeners) != 0 && cl.config.AcceptPeerConnections {
+                       return errors.New("download rate limit is zero, but listening for peer connections")
+               }
+       }
+       return nil
+}
index e07278657f2c9263c45bfa4615cf70bc2ecb7401..55b72ade5d612b97133c207efe1d906fd515e727 100644 (file)
--- a/config.go
+++ b/config.go
@@ -171,6 +171,8 @@ type ClientConfig struct {
        // bit of a special case, since a peer could also be useless if they're just not interested, or
        // we don't intend to obtain all of a torrent's data.
        DropMutuallyCompletePeers bool
+       // Use dialers to obtain connections to regular peers.
+       DialForPeerConns bool
        // Whether to accept peer connections at all.
        AcceptPeerConnections bool
        // Whether a Client should want conns without delegating to any attached Torrents. This is
@@ -239,6 +241,7 @@ func NewDefaultClientConfig() *ClientConfig {
                CryptoProvides:         mse.AllSupportedCrypto,
                ListenPort:             42069,
                Extensions:             defaultPeerExtensionBytes(),
+               DialForPeerConns:       true,
                AcceptPeerConnections:  true,
                MaxUnverifiedBytes:     64 << 20,
                DialRateLimiter:        rate.NewLimiter(10, 10),
index 914db6871eeefb26c0125b981bf4836b6f1f87c6..d8c541da6f95b7d0a0bda48113a93384fb492814 100644 (file)
@@ -20,6 +20,7 @@ func TestDropTorrentWithMmapStorageWhileHashing(t *testing.T) {
        // Ensure the data is present when the torrent is added, and not obtained
        // over the network as the test runs.
        cfg.DownloadRateLimiter = rate.NewLimiter(0, 0)
+       cfg.DialForPeerConns = false
        cl, err := NewClient(cfg)
        require.NoError(t, err)
        defer cl.Close()