addrbook.go
func (a *AddrBook) PickAddress(newBias int) *NetAddress {
	a.mtx.Lock()
	defer a.mtx.Unlock()

	if a.size() == 0 {
		return nil
	}
	if newBias > 100 {
		newBias = 100
	}
	if newBias < 0 {
		newBias = 0
	}

	// Bias between new and old addresses.
	oldCorrelation := math.Sqrt(float64(a.nOld)) * (100.0 - float64(newBias))
	newCorrelation := math.Sqrt(float64(a.nNew)) * float64(newBias)

	if (newCorrelation+oldCorrelation)*a.rand.Float64() < oldCorrelation {
		// pick random Old bucket.
		var bucket map[string]*knownAddress = nil
		for len(bucket) == 0 {
			bucket = a.addrOld[a.rand.Intn(len(a.addrOld))]
		}
		// pick a random ka from bucket.
		randIndex := a.rand.Intn(len(bucket))
		for _, ka := range bucket {
			if randIndex == 0 {
				return ka.Addr
			}
			randIndex--
		}
		PanicSanity("Should not happen")
	} else {
		// pick random New bucket.
		var bucket map[string]*knownAddress = nil
		for len(bucket) == 0 {
			bucket = a.addrNew[a.rand.Intn(len(a.addrNew))]
		}
		// pick a random ka from bucket.
		randIndex := a.rand.Intn(len(bucket))
		for _, ka := range bucket {
			if randIndex == 0 {
				return ka.Addr
			}
			randIndex--
		}
		PanicSanity("Should not happen")
	}
	return nil
}
func (a *AddrBook) PickAddress(newBias int) *NetAddress {
	a.mtx.Lock()
	defer a.mtx.Unlock()

	if a.size() == 0 {
		return nil
	}
	if newBias > 100 {
		newBias = 100
	}
	if newBias < 0 {
		newBias = 0
	}

	// Bias between new and old addresses.
	oldCorrelation := math.Sqrt(float64(a.nOld)) * (100.0 - float64(newBias))
	newCorrelation := math.Sqrt(float64(a.nNew)) * float64(newBias)

	if (newCorrelation+oldCorrelation)*a.rand.Float64() < oldCorrelation {
		// pick random Old bucket.
		var bucket map[string]*knownAddress = nil
		for len(bucket) == 0 {
			bucket = a.addrOld[a.rand.Intn(len(a.addrOld))]
		}
		// pick a random ka from bucket.
		randIndex := a.rand.Intn(len(bucket))
		for _, ka := range bucket {
			if randIndex == 0 {
				return ka.Addr
			}
			randIndex--
		}
		PanicSanity("Should not happen")
	} else {
		// pick random New bucket.
		var bucket map[string]*knownAddress = nil
		for len(bucket) == 0 {
			bucket = a.addrNew[a.rand.Intn(len(a.addrNew))]
		}
		// pick a random ka from bucket.
		randIndex := a.rand.Intn(len(bucket))
		for _, ka := range bucket {
			if randIndex == 0 {
				return ka.Addr
			}
			randIndex--
		}
		PanicSanity("Should not happen")
	}
	return nil
}
func ( a * AddrBook ) PickAddress ( newBias int ) * NetAddress { a . mtx . Lock ( ) defer a . mtx . Unlock ( ) if a . size ( ) == 0 { return nil } if newBias > 100 { newBias = 100 } if newBias < 0 { newBias = 0 } // Bias between new and old addresses. oldCorrelation := math . Sqrt ( float64 ( a . nOld ) ) * ( 100.0 - float64 ( newBias ) ) newCorrelation := math . Sqrt ( float64 ( a . nNew ) ) * float64 ( newBias ) if ( newCorrelation + oldCorrelation ) * a . rand . Float64 ( ) < oldCorrelation { // pick random Old bucket. var bucket map [ string ] * knownAddress = nil for len ( bucket ) == 0 { bucket = a . addrOld [ a . rand . Intn ( len ( a . addrOld ) ) ] } // pick a random ka from bucket. randIndex := a . rand . Intn ( len ( bucket ) ) for _ , ka := range bucket { if randIndex == 0 { return ka . Addr } randIndex -- } PanicSanity ( " " ) } else { // pick random New bucket. var bucket map [ string ] * knownAddress = nil for len ( bucket ) == 0 { bucket = a . addrNew [ a . rand . Intn ( len ( a . addrNew ) ) ] } // pick a random ka from bucket. randIndex := a . rand . Intn ( len ( bucket ) ) for _ , ka := range bucket { if randIndex == 0 { return ka . Addr } randIndex -- } PanicSanity ( " " ) } return nil }
// Pick an address to connect to with new/old bias.
Pick an address to connect to with new / old bias .