Add new config - number_of_hosts

This commit is contained in:
Ben Adrian Sarmiento
2024-06-17 17:27:07 +02:00
parent f33c2411e0
commit ad06d8fea3
3 changed files with 39 additions and 39 deletions

View File

@@ -69,13 +69,12 @@ func (r *IPRepository) NetworkTest(forceRun bool) {
r.writeLatencyFile(ipv6latencyFile, r.ipv6latencyMap)
}
func (r *IPRepository) GetOptimalHosts(ipv6 bool) []string {
func (r *IPRepository) GetOptimalHosts(numberOfHosts int, ipv6 bool) []string {
latencyMap := r.ipv4latencyMap
if ipv6 {
latencyMap = r.ipv6latencyMap
}
// Convert the latency map to a slice of key-value pairs
type kv struct {
Key string
Value float64
@@ -86,20 +85,12 @@ func (r *IPRepository) GetOptimalHosts(ipv6 bool) []string {
kvList = append(kvList, kv{k, v})
}
// Sort the slice by latency values
sort.Slice(kvList, func(i, j int) bool {
return kvList[i].Value < kvList[j].Value
})
// Calculate the number of hosts to return (top 50%)
n := len(kvList) / 5
if len(kvList)%5 != 0 {
n++
}
// Collect the keys of the top 50% hosts
var optimalHosts []string
for i := 0; i < n; i++ {
for i := 0; i < numberOfHosts && i < len(kvList); i++ {
optimalHosts = append(optimalHosts, kvList[i].Key)
}