| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | namespace Aws\Retry;
 | 
        
           |  |  | 3 |   | 
        
           |  |  | 4 | use Aws\Exception\AwsException;
 | 
        
           |  |  | 5 | use Aws\ResultInterface;
 | 
        
           |  |  | 6 |   | 
        
           |  |  | 7 | /**
 | 
        
           |  |  | 8 |  * @internal
 | 
        
           |  |  | 9 |  */
 | 
        
           |  |  | 10 | class QuotaManager
 | 
        
           |  |  | 11 | {
 | 
        
           |  |  | 12 |     private $availableCapacity;
 | 
        
           |  |  | 13 |     private $capacityAmount;
 | 
        
           |  |  | 14 |     private $initialRetryTokens;
 | 
        
           |  |  | 15 |     private $maxCapacity;
 | 
        
           |  |  | 16 |     private $noRetryIncrement;
 | 
        
           |  |  | 17 |     private $retryCost;
 | 
        
           |  |  | 18 |     private $timeoutRetryCost;
 | 
        
           |  |  | 19 |   | 
        
           |  |  | 20 |     public function __construct($config = [])
 | 
        
           |  |  | 21 |     {
 | 
        
           |  |  | 22 |         $this->initialRetryTokens = isset($config['initial_retry_tokens'])
 | 
        
           |  |  | 23 |             ? $config['initial_retry_tokens']
 | 
        
           |  |  | 24 |             : 500;
 | 
        
           |  |  | 25 |         $this->noRetryIncrement = isset($config['no_retry_increment'])
 | 
        
           |  |  | 26 |             ? $config['no_retry_increment']
 | 
        
           |  |  | 27 |             : 1;
 | 
        
           |  |  | 28 |         $this->retryCost = isset($config['retry_cost'])
 | 
        
           |  |  | 29 |             ? $config['retry_cost']
 | 
        
           |  |  | 30 |             : 5;
 | 
        
           |  |  | 31 |         $this->timeoutRetryCost = isset($config['timeout_retry_cost'])
 | 
        
           |  |  | 32 |             ? $config['timeout_retry_cost']
 | 
        
           |  |  | 33 |             : 10;
 | 
        
           |  |  | 34 |         $this->maxCapacity = $this->initialRetryTokens;
 | 
        
           |  |  | 35 |         $this->availableCapacity = $this->initialRetryTokens;
 | 
        
           |  |  | 36 |     }
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 |     public function hasRetryQuota($result)
 | 
        
           |  |  | 39 |     {
 | 
        
           |  |  | 40 |         if ($result instanceof AwsException && $result->isConnectionError()) {
 | 
        
           |  |  | 41 |             $this->capacityAmount = $this->timeoutRetryCost;
 | 
        
           |  |  | 42 |         } else {
 | 
        
           |  |  | 43 |             $this->capacityAmount = $this->retryCost;
 | 
        
           |  |  | 44 |         }
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |         if ($this->capacityAmount > $this->availableCapacity) {
 | 
        
           |  |  | 47 |             return false;
 | 
        
           |  |  | 48 |         }
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |         $this->availableCapacity -= $this->capacityAmount;
 | 
        
           |  |  | 51 |         return true;
 | 
        
           |  |  | 52 |     }
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |     public function releaseToQuota($result)
 | 
        
           |  |  | 55 |     {
 | 
        
           |  |  | 56 |         if ($result instanceof AwsException) {
 | 
        
           |  |  | 57 |             $statusCode = (int) $result->getStatusCode();
 | 
        
           |  |  | 58 |         } elseif ($result instanceof ResultInterface) {
 | 
        
           |  |  | 59 |             $statusCode = isset($result['@metadata']['statusCode'])
 | 
        
           |  |  | 60 |                 ? (int) $result['@metadata']['statusCode']
 | 
        
           |  |  | 61 |                 : null;
 | 
        
           |  |  | 62 |         }
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |         if (!empty($statusCode) && $statusCode >= 200 && $statusCode < 300) {
 | 
        
           |  |  | 65 |             if (isset($this->capacityAmount)) {
 | 
        
           |  |  | 66 |                 $amount = $this->capacityAmount;
 | 
        
           |  |  | 67 |                 $this->availableCapacity += $amount;
 | 
        
           |  |  | 68 |                 unset($this->capacityAmount);
 | 
        
           |  |  | 69 |             } else {
 | 
        
           |  |  | 70 |                 $amount = $this->noRetryIncrement;
 | 
        
           |  |  | 71 |                 $this->availableCapacity += $amount;
 | 
        
           |  |  | 72 |             }
 | 
        
           |  |  | 73 |             $this->availableCapacity = min(
 | 
        
           |  |  | 74 |                 $this->availableCapacity,
 | 
        
           |  |  | 75 |                 $this->maxCapacity
 | 
        
           |  |  | 76 |             );
 | 
        
           |  |  | 77 |         }
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 |         return (isset($amount) ? $amount : 0);
 | 
        
           |  |  | 80 |     }
 | 
        
           |  |  | 81 |   | 
        
           |  |  | 82 |     public function getAvailableCapacity()
 | 
        
           |  |  | 83 |     {
 | 
        
           |  |  | 84 |         return $this->availableCapacity;
 | 
        
           |  |  | 85 |     }
 | 
        
           |  |  | 86 | }
 |