Only announce to DHT if we have dialers or listeners
This commit is contained in:
parent
eec337d4bc
commit
730cebf5aa
39
torrent.go
39
torrent.go
@ -90,7 +90,8 @@ type Torrent struct {
|
|||||||
// active connections if were told about the peer after connecting with
|
// active connections if were told about the peer after connecting with
|
||||||
// them. That encourages us to reconnect to peers that are well known in
|
// them. That encourages us to reconnect to peers that are well known in
|
||||||
// the swarm.
|
// the swarm.
|
||||||
peers prioritizedPeers
|
peers prioritizedPeers
|
||||||
|
// Whether we want to know to know more peers.
|
||||||
wantPeersEvent missinggo.Event
|
wantPeersEvent missinggo.Event
|
||||||
// An announcer for each tracker URL.
|
// An announcer for each tracker URL.
|
||||||
trackerAnnouncers map[string]torrentTrackerAnnouncer
|
trackerAnnouncers map[string]torrentTrackerAnnouncer
|
||||||
@ -1440,19 +1441,33 @@ func (t *Torrent) announceToDht(impliedPort bool, s DhtServer) error {
|
|||||||
|
|
||||||
func (t *Torrent) dhtAnnouncer(s DhtServer) {
|
func (t *Torrent) dhtAnnouncer(s DhtServer) {
|
||||||
cl := t.cl
|
cl := t.cl
|
||||||
|
cl.lock()
|
||||||
|
defer cl.unlock()
|
||||||
for {
|
for {
|
||||||
select {
|
for {
|
||||||
case <-t.closed.LockedChan(cl.locker()):
|
if t.closed.IsSet() {
|
||||||
return
|
return
|
||||||
case <-t.wantPeersEvent.LockedChan(cl.locker()):
|
}
|
||||||
}
|
if !t.wantPeers() {
|
||||||
cl.lock()
|
goto wait
|
||||||
t.numDHTAnnounces++
|
}
|
||||||
cl.unlock()
|
// TODO: Determine if there's a listener on the port we're announcing.
|
||||||
err := t.announceToDht(true, s)
|
if len(cl.dialers) == 0 && len(cl.listeners) == 0 {
|
||||||
if err != nil {
|
goto wait
|
||||||
t.logger.WithValues(log.Warning).Printf("error announcing %q to DHT: %s", t, err)
|
}
|
||||||
|
break
|
||||||
|
wait:
|
||||||
|
cl.event.Wait()
|
||||||
}
|
}
|
||||||
|
func() {
|
||||||
|
t.numDHTAnnounces++
|
||||||
|
cl.unlock()
|
||||||
|
defer cl.lock()
|
||||||
|
err := t.announceToDht(true, s)
|
||||||
|
if err != nil {
|
||||||
|
t.logger.WithValues(log.Warning).Printf("error announcing %q to DHT: %s", t, err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user