]> git.openstreetmap.org Git - nominatim.git/commitdiff
add an option for max number of sleeping threads
authorBrian Quinion <openstreetmap@brian.quinion.co.uk>
Sat, 8 Dec 2012 18:55:17 +0000 (18:55 +0000)
committerBrian Quinion <openstreetmap@brian.quinion.co.uk>
Sat, 8 Dec 2012 18:55:17 +0000 (18:55 +0000)
lib/init-website.php
lib/lib.php
settings/settings.php
utils/blocks.php

index 8f04d97d9029832af60cb84e254996726b2b1511..445bbc7ced2e5048f31766c78f6d0b40225c478f 100644 (file)
 
        if ($fBucketVal > CONST_ConnectionBucket_WaitLimit && $fBucketVal < CONST_ConnectionBucket_BlockLimit)
        {
-               sleep(($fBucketVal - CONST_ConnectionBucket_WaitLimit)/CONST_ConnectionBucket_LeakRate);
-               $fBucketVal = doBucket($aBucketKeys, 0, 0, CONST_ConnectionBucket_BlockLimit);
+               $m = getBucketMemcache();
+               $iCurrentSleeping = $m->increment('sleepCounter');
+               if (false === $iCurrentSleeping)
+               {
+                       $m->add('sleepCounter', 0);
+                       $iCurrentSleeping = $m->increment('sleepCounter');
+               }
+               if ($iCurrentSleeping >= CONST_ConnectionBucket_MaxSleeping)
+               {
+                       // Too many threads sleeping already.  This becomes a hard block.
+                       $fBucketVal = doBucket($aBucketKeys, CONST_ConnectionBucket_BlockLimit, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
+               }
+               else
+               {
+                       sleep(($fBucketVal - CONST_ConnectionBucket_WaitLimit)/CONST_ConnectionBucket_LeakRate);
+                       $fBucketVal = doBucket($aBucketKeys, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
+               }
+               $m->decrement('sleepCounter');
        }
 
        if (strpos(CONST_BlockedIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false || $fBucketVal >= CONST_ConnectionBucket_BlockLimit)
index 0af91e2d89a348091d8726bfc151776afb0d165a..91fe21b2c79626b23f087387a1dfb95d0c2bca16 100644 (file)
 
        function getBucketMemcache()
        {
+               static $m;
+
                if (!CONST_ConnectionBucket_MemcacheServerAddress) return null;
-               $m = new Memcached();
-               $m->addServer(CONST_ConnectionBucket_MemcacheServerAddress, CONST_ConnectionBucket_MemcacheServerPort);
+               if (!isset($m))
+               {
+                       $m = new Memcached();
+                       $m->addServer(CONST_ConnectionBucket_MemcacheServerAddress, CONST_ConnectionBucket_MemcacheServerPort);
+               }
                return $m;
        }
 
index 656040358e1bef60018bc03347206eb0cb80c5e2..6bf526caaf8e05cadcec65c6a26eed0be0ffbdbe 100644 (file)
        @define('CONST_ConnectionBucket_LeakRate', 1);
        @define('CONST_ConnectionBucket_BlockLimit', 10);
        @define('CONST_ConnectionBucket_WaitLimit', 6);
+       @define('CONST_ConnectionBucket_MaxSleeping', 10);
        @define('CONST_ConnectionBucket_Cost_Reverse', 1);
        @define('CONST_ConnectionBucket_Cost_Search', 2);
        @define('CONST_ConnectionBucket_Cost_Details', 3);
 
+       // Override this function to add an adjustment factor to the cost
+       // based on server load. e.g. getBlockingProcesses
        if (!function_exists('user_busy_cost'))
        {
                function user_busy_cost()
index 4d32311547d143ad7d9e38e4a84ef080c65e878c..4732abb1e0c2421de17d81019b339057085f6459 100755 (executable)
@@ -23,6 +23,9 @@
 
        if ($aResult['list'])
        {
+               $iCurrentSleeping = $m->get('sleepCounter');
+               echo "\n Sleeping blocks count: $iCurrentSleeping\n";
+
                $aBlocks = getBucketBlocks();
                echo "\n";
                printf(" %-40s | %12s | %7s | %13s | %16s | %31s\n", "Key", "Total Blocks", "Current", "Still Blocked", "Last Req Blocked", "Last Block Time");
@@ -36,5 +39,6 @@
 
        if ($aResult['delete'])
        {
+               $m->set('sleepCounter', 0);
                clearBucketBlocks();
        }