Wait and Retry logic to avoid RateExceedError in PHP

360 views
Skip to first unread message

faisal hossain

unread,
Jul 28, 2015, 2:53:32 PM7/28/15
to AdWords API Forum
Hi,

I am getting RateExceedError when tring to make large operations in one mutate request (1000 operations in 1 mutate call), the API expert suggests to honor the retryAfterSeconds=86400

Can anyone give me the example error handling code for PHP??

It's very urgent. Thanks in advance

Cheers
Faisal Hossain

Kristopher Windsor

unread,
Jul 28, 2015, 3:26:56 PM7/28/15
to AdWords API Forum, faisa...@gmail.com
  public static function mutateEntities($adwords_user, $service_name, $ops){
    $service
= $adwords_user->GetService($service_name, Utils::ADWORDS_VERSION);

    $opc
= count($ops);
   
if ($opc > 5000)
     
throw new \Exception('Too many operations for ' . $service_name);

   
for ($iii = 0; $iii < 5; $iii++){
     
RateLimiter::throttle($adwords_user->getClientCustomerId(), $opc);
     
try {
        $service
->mutate($ops);
       
break;
     
} catch (\Exception $e){
        $errors
= ( ($e instanceof \SoapFault) ? \ErrorUtils::GetApiErrors($e) : null);
       
if (!Utils::isRetriable($errors))
         
throw new NonRetriableException('Non-retriable error: ' . $e->getMessage()); // here I just check if the error seems to be due to a bad request and if so, do not retry
       
RateLimiter::reportError($adwords_user->getClientCustomerId(), $iii, $errors);
     
}
     
if ($iii + 1 == 5){
       
throw new \Exception('Adwords query failed after max retries. ' . $e->getMessage());
     
}
   
}

 
}


RateLimiter:


 
public static function reportError($account, $attemptNum, $errors = null){
   
if (!$account)
      $account
= self::NO_ACCOUNT;

   
// determine if it is a rate limit issue
    $error_types
= [];
    $rateScope
= $rateName = null;
   
if ($errors)
     
foreach ($errors as $i){
       
@$error_types[ $i->ApiErrorType ]++;
       
if ($i->ApiErrorType == 'RateExceededError')
          list
($rateScope, $rateName) = array($i->rateScope, $i->rateName);
     
}

    error_log
('AdwordsRateLimiter error ' . $account . ' ' . $rateScope . ' ' . $rateName . ' ' . ($rateScope ? 'RATE LIMIT EXCEEDED' : '') . '; ' . json_encode($error_types) );

   
if ($rateScope && $rateName){
      $data
= array('account' => $account, 'err' => '1', 'attemptNum' => $attemptNum, 'rateScope' => $rateScope, 'rateName' => $rateName);
   
} else {
      $data
= array('account' => $account, 'err' => '1', 'attemptNum' => $attemptNum);
   
}
   
// log $data: self::sendRequest($data);
 
}


^^ Here is some code we use for handling errors. While I don't check for retryAfterSeconds, hopefully you can see where that field might be in the error objects.

Hope it helps.

Thanks,

faisal hossain

unread,
Jul 28, 2015, 3:49:54 PM7/28/15
to AdWords API Forum
Many many...thanks @Kristopher for your reply, That someone is me :) on the other thread.I wish your logic and given code will help me out from this problem.

Thanks Again
Reply all
Reply to author
Forward
0 new messages