]> Git Repo - VerusCoin.git/commitdiff
Fix issue #717 where if addrman is starved of addresses (e.g. on testnet)
authorSimon <[email protected]>
Fri, 13 May 2016 22:48:10 +0000 (15:48 -0700)
committerSimon <[email protected]>
Fri, 13 May 2016 23:00:22 +0000 (16:00 -0700)
the Select_() function will loop endlessly trying to find an address,
and therefore eat up 100% cpu time on the 'opencon' thread.

Solution is to (1) add a delay to the loop and (2) restrict the number
of attempts to find an address.  On exiting the loop, we return
to an outer loop in net.cpp which will sleep, add seed nodes and
calcualte new addresses.

src/addrman.cpp

index c41ee3f9fce566cc77a80629ba4e8f386161e691..82c4ea2770bb6b644abb9d0773a3cd9f65689a90 100644 (file)
@@ -336,11 +336,14 @@ CAddrInfo CAddrMan::Select_()
     if (size() == 0)
         return CAddrInfo();
 
+    // Track number of attempts to find a table entry, before giving up
+    int nRetries = 0;
+
     // Use a 50% chance for choosing between tried and new table entries.
     if (nTried > 0 && (nNew == 0 || GetRandInt(2) == 0)) {
         // use a tried node
         double fChanceFactor = 1.0;
-        while (1) {
+        while (nRetries++ < ADDRMAN_TRIED_BUCKET_COUNT) {
             int nKBucket = GetRandInt(ADDRMAN_TRIED_BUCKET_COUNT);
             int nKBucketPos = GetRandInt(ADDRMAN_BUCKET_SIZE);
             if (vvTried[nKBucket][nKBucketPos] == -1)
@@ -351,11 +354,12 @@ CAddrInfo CAddrMan::Select_()
             if (GetRandInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30))
                 return info;
             fChanceFactor *= 1.2;
+            MilliSleep(100);
         }
     } else {
         // use a new node
         double fChanceFactor = 1.0;
-        while (1) {
+        while (nRetries++ < ADDRMAN_NEW_BUCKET_COUNT) {
             int nUBucket = GetRandInt(ADDRMAN_NEW_BUCKET_COUNT);
             int nUBucketPos = GetRandInt(ADDRMAN_BUCKET_SIZE);
             if (vvNew[nUBucket][nUBucketPos] == -1)
@@ -366,8 +370,11 @@ CAddrInfo CAddrMan::Select_()
             if (GetRandInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30))
                 return info;
             fChanceFactor *= 1.2;
+            MilliSleep(100);
         }
     }
+    
+    return CAddrInfo();
 }
 
 #ifdef DEBUG_ADDRMAN
This page took 0.028989 seconds and 4 git commands to generate.