2
0
mirror of synced 2025-02-24 06:38:14 +00:00

Simplify (*Torrent).gotMetainfo (#581)

This commit is contained in:
YenForYang 2021-09-12 20:41:11 -05:00 committed by GitHub
parent 1bb6724020
commit 5c440e8929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 12 deletions

View File

@ -1112,6 +1112,7 @@ func (cl *Client) newTorrent(ih metainfo.Hash, specStorage storage.ClientImpl) (
L: cl.locker(), L: cl.locker(),
}, },
webSeeds: make(map[string]*Peer), webSeeds: make(map[string]*Peer),
gotMetainfoC: make(chan struct{}),
} }
t.networkingEnabled.Set() t.networkingEnabled.Set()
t._pendingPieces.NewSet = priorityBitmapStableNewSet t._pendingPieces.NewSet = priorityBitmapStableNewSet

18
t.go
View File

@ -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. // 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. // TODO: We shouldn't need to lock to take a channel here, if the event is only ever set.
t.cl.lock() t.nameMu.RLock()
defer t.cl.unlock() ret = t.gotMetainfoC
return t.gotMetainfo.C() t.nameMu.RUnlock()
return
} }
// Returns the metainfo info dictionary, or nil if it's not yet available. // Returns the metainfo info dictionary, or nil if it's not yet available.
func (t *Torrent) Info() *metainfo.Info { func (t *Torrent) Info() (info *metainfo.Info) {
t.cl.lock() t.nameMu.RLock()
defer t.cl.unlock() info = t.info
return t.info t.nameMu.RUnlock()
return
} }
// Returns a Reader bound to the torrent's data. All read calls block until the data requested is // Returns a Reader bound to the torrent's data. All read calls block until the data requested is

View File

@ -122,8 +122,8 @@ type Torrent struct {
metadataCompletedChunks []bool metadataCompletedChunks []bool
metadataChanged sync.Cond metadataChanged sync.Cond
// Set when .Info is obtained. // Closed when .Info is obtained.
gotMetainfo missinggo.Event gotMetainfoC chan struct{}
readers map[*reader]struct{} readers map[*reader]struct{}
_readerNowPieces bitmap.Bitmap _readerNowPieces bitmap.Bitmap
@ -305,10 +305,11 @@ func (t *Torrent) addPeer(p PeerInfo) (added bool) {
} }
func (t *Torrent) invalidateMetadata() { func (t *Torrent) invalidateMetadata() {
for i := range t.metadataCompletedChunks { for i := 0; i < len(t.metadataCompletedChunks); i++ {
t.metadataCompletedChunks[i] = false t.metadataCompletedChunks[i] = false
} }
t.nameMu.Lock() t.nameMu.Lock()
t.gotMetainfoC = make(chan struct{})
t.info = nil t.info = nil
t.nameMu.Unlock() t.nameMu.Unlock()
} }
@ -438,7 +439,7 @@ func (t *Torrent) onSetInfo() {
} }
} }
t.cl.event.Broadcast() t.cl.event.Broadcast()
t.gotMetainfo.Set() close(t.gotMetainfoC)
t.updateWantPeersEvent() t.updateWantPeersEvent()
t.pendingRequests = make(map[Request]int) t.pendingRequests = make(map[Request]int)
t.tryCreateMorePieceHashers() t.tryCreateMorePieceHashers()