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,9 +96,40 @@ func GetPeers(room, infoHash string, numWant uint, seeding bool) (peersv4, peers
shard := shards[shardIndex(h)]
shard.RLock()
if seeding {
peersv4, peersv6 = getPeersFromSwarm(numWant, shard.swarms[h].leechers)
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--
}
} else {
peersv4, peersv6 = getPeersFromSwarm(numWant, shard.swarms[h].seeders)
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)
@ -106,21 +137,6 @@ func GetPeers(room, infoHash string, numWant uint, seeding bool) (peersv4, peers
return
}
func getPeersFromSwarm(numWant uint, peers map[serializedPeer]int64) (peersv4, peersv6 []byte) {
for serialized := range peers {
if numWant == 0 {
break
}
if bytes.HasPrefix([]byte(serialized), v4InV6Prefix) {
peersv4 = append(peersv4, serialized[12:]...)
} else {
peersv6 = append(peersv6, serialized...)
}
numWant--
}
return
}
func GetStats(room, infoHash string) (numSeeders, numLeechers int) {
h := sha1.Sum([]byte(room + infoHash))
shard := shards[shardIndex(h)]