Ejemplo n.º 1
0
// relayAddressesOrder checks the latency to each relay, rounds latency down to
// the closest 50ms, and puts them in buckets of 50ms latency ranges. Then
// shuffles each bucket, and returns all addresses starting with the ones from
// the lowest latency bucket, ending with the highest latency buceket.
func relayAddressesOrder(input []string) []string {
	buckets := make(map[int][]string)

	for _, relay := range input {
		latency, err := osutil.GetLatencyForURL(relay)
		if err != nil {
			latency = time.Hour
		}

		id := int(latency/time.Millisecond) / 50

		buckets[id] = append(buckets[id], relay)
	}

	var ids []int
	for id, bucket := range buckets {
		shuffle(bucket)
		ids = append(ids, id)
	}

	sort.Ints(ids)

	addresses := make([]string, len(input))

	for _, id := range ids {
		addresses = append(addresses, buckets[id]...)
	}

	return addresses
}
Ejemplo n.º 2
0
// relayAddressesSortedByLatency adds local latency to the relay, and sorts them
// by sum latency, and returns the addresses.
func relayAddressesSortedByLatency(input []string) []string {
	relays := make(relayList, len(input))
	for i, relay := range input {
		if latency, err := osutil.GetLatencyForURL(relay); err == nil {
			relays[i] = relayWithLatency{relay, int(latency / time.Millisecond)}
		} else {
			relays[i] = relayWithLatency{relay, int(time.Hour / time.Millisecond)}
		}
	}

	sort.Sort(relays)

	addresses := make([]string, len(relays))
	for i, relay := range relays {
		addresses[i] = relay.relay
	}
	return addresses
}