share leechers with leechers, why not

This commit is contained in:
Michał Adamski 2020-12-07 00:52:51 +01:00
parent 9e7f58b987
commit 42b408b5a3

View file

@ -96,18 +96,7 @@ func GetPeers(room, infoHash string, numWant uint, seeding bool) (peersv4, peers
shard := shards[shardIndex(h)] shard := shards[shardIndex(h)]
shard.RLock() shard.RLock()
if seeding { if seeding {
peersv4, peersv6 = getPeersFromSwarm(numWant, shard.swarms[h].leechers) for serialized := range shard.swarms[h].leechers {
} else {
peersv4, peersv6 = getPeersFromSwarm(numWant, shard.swarms[h].seeders)
}
numSeeders = len(shard.swarms[h].seeders)
numLeechers = len(shard.swarms[h].leechers)
shard.RUnlock()
return
}
func getPeersFromSwarm(numWant uint, peers map[serializedPeer]int64) (peersv4, peersv6 []byte) {
for serialized := range peers {
if numWant == 0 { if numWant == 0 {
break break
} }
@ -118,6 +107,33 @@ func getPeersFromSwarm(numWant uint, peers map[serializedPeer]int64) (peersv4, p
} }
numWant-- numWant--
} }
} else {
for serialized := range shard.swarms[h].seeders {
if numWant == 0 {
break
}
if bytes.HasPrefix([]byte(serialized), v4InV6Prefix) {
peersv4 = append(peersv4, serialized[12:]...)
} else {
peersv6 = append(peersv6, serialized...)
}
numWant--
}
for serialized := range shard.swarms[h].leechers {
if numWant == 0 {
break
}
if bytes.HasPrefix([]byte(serialized), v4InV6Prefix) {
peersv4 = append(peersv4, serialized[12:]...)
} else {
peersv6 = append(peersv6, serialized...)
}
numWant--
}
}
numSeeders = len(shard.swarms[h].seeders)
numLeechers = len(shard.swarms[h].leechers)
shard.RUnlock()
return return
} }