]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Simplify (*Torrent).gotMetainfo (#581)
authorYenForYang <YenForYang@users.noreply.github.com>
Mon, 13 Sep 2021 01:41:11 +0000 (20:41 -0500)
committerGitHub <noreply@github.com>
Mon, 13 Sep 2021 01:41:11 +0000 (11:41 +1000)
client.go
t.go
torrent.go

index 5ff6acc4539527314f20e328f9889e4a354743bc..e47aa0822cc52b3ecfc4199ad75fbe83a8a97f5b 100644 (file)
--- a/client.go
+++ b/client.go
@@ -1112,6 +1112,7 @@ func (cl *Client) newTorrent(ih metainfo.Hash, specStorage storage.ClientImpl) (
                        L: cl.locker(),
                },
                webSeeds: make(map[string]*Peer),
+               gotMetainfoC: make(chan struct{}),
        }
        t.networkingEnabled.Set()
        t._pendingPieces.NewSet = priorityBitmapStableNewSet
diff --git a/t.go b/t.go
index 153264e928d5c62823c0ff222bb65f39f82dc7be..b42a3f0f2c1c29f0ec9d2bbf5f19f6c2aca135ce 100644 (file)
--- a/t.go
+++ b/t.go
@@ -16,18 +16,20 @@ func (t *Torrent) InfoHash() metainfo.Hash {
 }
 
 // Returns a channel that is closed when the info (.Info()) for the torrent has become available.
-func (t *Torrent) GotInfo() <-chan struct{} {
+func (t *Torrent) GotInfo() (ret <-chan struct{}) {
        // TODO: We shouldn't need to lock to take a channel here, if the event is only ever set.
-       t.cl.lock()
-       defer t.cl.unlock()
-       return t.gotMetainfo.C()
+       t.nameMu.RLock()
+       ret = t.gotMetainfoC
+       t.nameMu.RUnlock()
+       return
 }
 
 // Returns the metainfo info dictionary, or nil if it's not yet available.
-func (t *Torrent) Info() *metainfo.Info {
-       t.cl.lock()
-       defer t.cl.unlock()
-       return t.info
+func (t *Torrent) Info() (info *metainfo.Info) {
+       t.nameMu.RLock()
+       info = t.info
+       t.nameMu.RUnlock()
+       return
 }
 
 // Returns a Reader bound to the torrent's data. All read calls block until the data requested is
index 27ad3e1ed5f88fb4ca7a3761c3b8175b4bf6075c..0481d5649808c68639dea2dc6877f787de204d35 100644 (file)
@@ -122,8 +122,8 @@ type Torrent struct {
        metadataCompletedChunks []bool
        metadataChanged         sync.Cond
 
-       // Set when .Info is obtained.
-       gotMetainfo missinggo.Event
+       // Closed when .Info is obtained.
+       gotMetainfoC chan struct{}
 
        readers                map[*reader]struct{}
        _readerNowPieces       bitmap.Bitmap
@@ -305,10 +305,11 @@ func (t *Torrent) addPeer(p PeerInfo) (added bool) {
 }
 
 func (t *Torrent) invalidateMetadata() {
-       for i := range t.metadataCompletedChunks {
+       for i := 0; i < len(t.metadataCompletedChunks); i++ {
                t.metadataCompletedChunks[i] = false
        }
        t.nameMu.Lock()
+       t.gotMetainfoC = make(chan struct{})
        t.info = nil
        t.nameMu.Unlock()
 }
@@ -438,7 +439,7 @@ func (t *Torrent) onSetInfo() {
                }
        }
        t.cl.event.Broadcast()
-       t.gotMetainfo.Set()
+       close(t.gotMetainfoC)
        t.updateWantPeersEvent()
        t.pendingRequests = make(map[Request]int)
        t.tryCreateMorePieceHashers()