SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2012
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Avoid Infinite Loop w/ Function Calling Self

    I'm using Google maps to plot coords. I have a switch construct to delay and resend requests if Google replies with a status of OVER_QUERY_LIMIT.

    Coded as-is, with setGeocode calling itself, I'm worried about triggering an infinite loop.

    Is there a way to kill the first process after setGeocode makes the secondary call to itself?

    Code:
    	public function setGeocode()
    	{
    	    $xml = simplexml_load_file($this->request_url) or die("url not loading");
    	    $status = $xml->status;
    	    if ($status == 'OK') {
    		  $lat = $xml->result->geometry->location->lat;
    		  $lng = $xml->result->geometry->location->lng;
    		} elseif($status == 'ZERO_RESULTS') {
    			echo 'No results retured';				
    		} elseif($status == 'OVER_QUERY_LIMIT') {
    			echo 'Over query limit';	
    			usleep(50000);//if we hit query limit take a 5 sec timout then run http request again
    			$this->setGedocode();
    		} elseif($status == 'REQUEST_DENIED') {
    			echo 'Request denied';	
    			return;
    		} elseif($status == 'INVALID_REQUEST') {
    			echo 'Not enough enough provided';	
    			return;
    		}
    		$this->lat  = $lat;
    		$this->lng = $lng;
    	
    		usleep(10000);
    	}

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    More than a few ways, but...

    Code:
    <?php
    class Bacon
    {
        public function fry($isRecursive)
        {
            if( ! $isRecursive && 'RATE_LIMITED' == $status)
            {
                $this->fry(true);
            }
        }
    }
    
    
    
    
    
    
    class Bacon
    {
        protected
            $calls = 0;
    
    
        public function fry()
        {
            if(0 == $this->calls || 'RATE_LIMITED' == $status)
            {
                $this->fry();
            }
            
            $this->calls++;
        }
    }
    ?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2012
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. I'll keep these methods in my tool box. Both I imagine will come in handy.

    Looking back at my original code, and assuming one of these queries eventually did execute successfully, say on the 50th try, would this mean I'd have 50 executions of setGeocode in queue, or will the current execution die as soon as the next one is called?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •