dht: Message only the most likely peers

This commit is contained in:
Matt Joiner 2014-07-24 13:45:18 +10:00
parent 8ff1c6f34a
commit cd7a758acd
1 changed files with 11 additions and 2 deletions

View File

@ -626,7 +626,7 @@ func (s *Server) GetPeers(infoHash string) (ps *peerStream, err error) {
done := make(chan struct{}) done := make(chan struct{})
pending := 0 pending := 0
s.mu.Lock() s.mu.Lock()
for _, n := range s.nodes { for _, n := range s.closestGoodNodes(160, infoHash) {
var t *transaction var t *transaction
t, err = s.getPeers(n.addr, infoHash) t, err = s.getPeers(n.addr, infoHash)
if err != nil { if err != nil {
@ -727,13 +727,22 @@ func (s *Server) Bootstrap() (err error) {
} }
s.mu.Lock() s.mu.Lock()
// log.Printf("now have %d nodes", len(s.nodes)) // log.Printf("now have %d nodes", len(s.nodes))
if len(s.nodes) >= 8*160 { if s.numGoodNodes() >= 160 {
break break
} }
} }
return return
} }
func (s *Server) numGoodNodes() (num int) {
for _, n := range s.nodes {
if n.Good() {
num++
}
}
return
}
func (s *Server) NumNodes() int { func (s *Server) NumNodes() int {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()