]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/leakybucket.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / lib / leakybucket.php
index 778fe580fe6e065eeb06fef7b0368608e55945a4..47e8447704695f958f258d1f21feb16c1b98e80b 100644 (file)
@@ -7,39 +7,39 @@
                if (!CONST_ConnectionBucket_MemcacheServerAddress) return null;
                if (!isset($m))
                {
                if (!CONST_ConnectionBucket_MemcacheServerAddress) return null;
                if (!isset($m))
                {
-                       $m = new Memcached();
-                       $m->addServer(CONST_ConnectionBucket_MemcacheServerAddress, CONST_ConnectionBucket_MemcacheServerPort);
+                       $m = new Memcached();
+                       $m->addServer(CONST_ConnectionBucket_MemcacheServerAddress, CONST_ConnectionBucket_MemcacheServerPort);
                }
                return $m;
        }
 
        function doBucket($asKey, $iRequestCost, $iLeakPerSecond, $iThreshold)
        {
                }
                return $m;
        }
 
        function doBucket($asKey, $iRequestCost, $iLeakPerSecond, $iThreshold)
        {
-               $m = getBucketMemcache();
+               $m = getBucketMemcache();
                if (!$m) return 0;
 
                $iMaxVal = 0;
                if (!$m) return 0;
 
                $iMaxVal = 0;
-               $t = time();
+               $t = time();
 
                foreach($asKey as $sKey)
                {
 
                foreach($asKey as $sKey)
                {
-                       $aCurrentBlock = $m->get($sKey);
-                       if (!$aCurrentBlock)
+                       $aCurrentBlock = $m->get($sKey);
+                       if (!$aCurrentBlock)
                        {
                        {
-                               $aCurrentBlock = array($iRequestCost, $t);
-                       }
+                               $aCurrentBlock = array($iRequestCost, $t, false);
+                       }
                        else
                        {
                                // add RequestCost
                                // remove leak * the time since the last request 
                        else
                        {
                                // add RequestCost
                                // remove leak * the time since the last request 
-                               $aCurrentBlock[0] += $iRequestCost - ($t - $aCurrentBlock[1])*$iLeakPerSecond;
-                               $aCurrentBlock[1] = $t;
+                               $aCurrentBlock[0] += $iRequestCost - ($t - $aCurrentBlock[1])*$iLeakPerSecond;
+                               $aCurrentBlock[1] = $t;
                        }
 
                        }
 
-                       if ($aCurrentBlock[0] <= 0)
+                       if ($aCurrentBlock[0] <= 0)
                        {
                        {
-                               $m->delete($sKey);
-                       }
+                               $m->delete($sKey);
+                       }
                        else
                        {
                                // If we have hit the threshold stop and record this to the block list
                        else
                        {
                                // If we have hit the threshold stop and record this to the block list
@@ -77,7 +77,7 @@
                                        }
                                }
                                // Only keep in memcache until the time it would have expired (to avoid clutering memcache)
                                        }
                                }
                                // Only keep in memcache until the time it would have expired (to avoid clutering memcache)
-                               $m->set($sKey, $aCurrentBlock, $t + 1 + $aCurrentBlock[0]/$iLeakPerSecond);
+                                               $m->set($sKey, $aCurrentBlock, $t + 1 + $aCurrentBlock[0]/$iLeakPerSecond);
                        }
 
                        // Bucket result in the largest bucket we find
                        }
 
                        // Bucket result in the largest bucket we find
                }
 
                return $iMaxVal;
                }
 
                return $iMaxVal;
-        }
+       }
+
+       function isBucketSleeping($asKey)
+       {
+               $m = getBucketMemcache();
+               if (!$m) return false;
+
+               foreach($asKey as $sKey)
+               {
+                       $aCurrentBlock = $m->get($sKey);
+                       if ($aCurrentBlock[2]) return true;
+               }
+               return false;
+       }
+
+       function setBucketSleeping($asKey, $bVal)
+       {
+               $m = getBucketMemcache();
+               if (!$m) return false;
+
+               $iMaxVal = 0;
+               $t = time();
+
+               foreach($asKey as $sKey)
+               {
+                       $aCurrentBlock = $m->get($sKey);
+                       $aCurrentBlock[2] = $bVal;
+                       $m->set($sKey, $aCurrentBlock, $t + 1 + $aCurrentBlock[0]/CONST_ConnectionBucket_LeakRate);
+               }
+               return true;
+       }
 
        function byValue1($a, $b)
        {
 
        function byValue1($a, $b)
        {
 
        function getBucketBlocks()
        {
 
        function getBucketBlocks()
        {
-               $m = getBucketMemcache();
+               $m = getBucketMemcache();
                if (!$m) return null;
                if (!$m) return null;
-               $t = time();
+               $t = time();
                $aBlockedList = $m->get('blockedList', null, $hCasToken);
                if (!$aBlockedList) $aBlockedList = array();
                foreach($aBlockedList as $sKey => $aDetails)
                $aBlockedList = $m->get('blockedList', null, $hCasToken);
                if (!$aBlockedList) $aBlockedList = array();
                foreach($aBlockedList as $sKey => $aDetails)
                        $aBlockedList[$sKey] = array(
                                'totalBlocks' => $aDetails[0],
                                'lastBlockTimestamp' => $aDetails[1],
                        $aBlockedList[$sKey] = array(
                                'totalBlocks' => $aDetails[0],
                                'lastBlockTimestamp' => $aDetails[1],
+                               'isSleeping' => (isset($aCurrentBlock[2])?$aCurrentBlock[2]:false),
                                'currentBucketSize' => $iCurrentBucketSize,
                                'currentlyBlocked' => $iCurrentBucketSize + (CONST_ConnectionBucket_Cost_Reverse) >= CONST_ConnectionBucket_BlockLimit,
                                );
                                'currentBucketSize' => $iCurrentBucketSize,
                                'currentlyBlocked' => $iCurrentBucketSize + (CONST_ConnectionBucket_Cost_Reverse) >= CONST_ConnectionBucket_BlockLimit,
                                );
 
        function clearBucketBlocks()
        {
 
        function clearBucketBlocks()
        {
-               $m = getBucketMemcache();
+               $m = getBucketMemcache();
                if (!$m) return false;
                $m->delete('blockedList');
                return true;
                if (!$m) return false;
                $m->delete('blockedList');
                return true;