2
0
mirror of synced 2025-02-22 21:58:24 +00:00

Remove relevant webtorrent offers when closing Torrent

(cherry picked from commit 73a0b5e4d2fe679aac31d87171537c70f91f46ee)
This commit is contained in:
Matt Joiner 2022-07-11 15:02:24 +10:00
parent f120b93e1c
commit aedf2583cc
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
3 changed files with 21 additions and 6 deletions

View File

@ -61,6 +61,7 @@ type Torrent struct {
userOnWriteChunkErr func(error) userOnWriteChunkErr func(error)
closed chansync.SetOnce closed chansync.SetOnce
onClose []func()
infoHash metainfo.Hash infoHash metainfo.Hash
pieces []Piece pieces []Piece
@ -865,6 +866,9 @@ func (t *Torrent) close(wg *sync.WaitGroup) (err error) {
err = errors.New("already closed") err = errors.New("already closed")
return return
} }
for _, f := range t.onClose {
f()
}
if t.storage != nil { if t.storage != nil {
wg.Add(1) wg.Add(1)
go func() { go func() {
@ -1614,11 +1618,10 @@ func (t *Torrent) runHandshookConnLoggingErr(pc *PeerConn) {
} }
func (t *Torrent) startWebsocketAnnouncer(u url.URL) torrentTrackerAnnouncer { func (t *Torrent) startWebsocketAnnouncer(u url.URL) torrentTrackerAnnouncer {
wtc, release := t.cl.websocketTrackers.Get(u.String()) wtc, release := t.cl.websocketTrackers.Get(u.String(), t.infoHash)
go func() { // This needs to run before the Torrent is dropped from the Client, to prevent a new webtorrent.TrackerClient for
<-t.closed.Done() // the same info hash before the old one is cleaned up.
release() t.onClose = append(t.onClose, release)
}()
wst := websocketTrackerStatus{u, wtc} wst := websocketTrackerStatus{u, wtc}
go func() { go func() {
err := wtc.Announce(tracker.Started, t.infoHash) err := wtc.Announce(tracker.Started, t.infoHash)

View File

@ -187,6 +187,17 @@ func (tc *TrackerClient) closeUnusedOffers() {
tc.outboundOffers = nil tc.outboundOffers = nil
} }
func (tc *TrackerClient) CloseOffersForInfohash(infoHash [20]byte) {
tc.mu.Lock()
defer tc.mu.Unlock()
for key, offer := range tc.outboundOffers {
if offer.infoHash == infoHash {
offer.peerConnection.Close()
delete(tc.outboundOffers, key)
}
}
}
func (tc *TrackerClient) Announce(event tracker.AnnounceEvent, infoHash [20]byte) error { func (tc *TrackerClient) Announce(event tracker.AnnounceEvent, infoHash [20]byte) error {
metrics.Add("outbound announces", 1) metrics.Add("outbound announces", 1)
var randOfferId [20]byte var randOfferId [20]byte

View File

@ -42,7 +42,7 @@ type websocketTrackers struct {
Proxy http.ProxyFunc Proxy http.ProxyFunc
} }
func (me *websocketTrackers) Get(url string) (*webtorrent.TrackerClient, func()) { func (me *websocketTrackers) Get(url string, infoHash [20]byte) (*webtorrent.TrackerClient, func()) {
me.mu.Lock() me.mu.Lock()
defer me.mu.Unlock() defer me.mu.Unlock()
value, ok := me.clients[url] value, ok := me.clients[url]
@ -74,6 +74,7 @@ func (me *websocketTrackers) Get(url string) (*webtorrent.TrackerClient, func())
return &value.TrackerClient, func() { return &value.TrackerClient, func() {
me.mu.Lock() me.mu.Lock()
defer me.mu.Unlock() defer me.mu.Unlock()
value.TrackerClient.CloseOffersForInfohash(infoHash)
value.refCount-- value.refCount--
if value.refCount == 0 { if value.refCount == 0 {
value.TrackerClient.Close() value.TrackerClient.Close()