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?


	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);
	}

More than a few ways, but…


<?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++;
    }
}

?

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?