Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Retry/V3/QuotaManager.php

raw symbols SCIP
1 <?php
2 namespace Aws\Retry\V3;
3
4 /**
5 * Retry-quota manager for the AWS_NEW_RETRIES_2026 opt-in path. Implements
6 * the throttling-aware token-bucket model described in the specs.
7 *
8 * @internal
9 */
10 class QuotaManager
11 {
12 private int $availableCapacity;
13 private readonly int $maxCapacity;
14 private readonly int $noRetryIncrement;
15 private readonly int $retryCost;
16 private readonly int $throttlingRetryCost;
17
18 public function __construct(array $config = [])
19 {
20 $initialTokens = $config['initial_retry_tokens'] ?? 500;
21 $this->noRetryIncrement = $config['no_retry_increment'] ?? 1;
22 $this->retryCost = $config['retry_cost'] ?? 14;
23 $this->throttlingRetryCost = $config['throttling_retry_cost'] ?? 5;
24 $this->maxCapacity = $initialTokens;
25 $this->availableCapacity = $initialTokens;
26 }
27
28 /**
29 * Attempts to acquire retry quota.
30 *
31 * @param bool $isThrottling Whether the error is a throttling error.
32 *
33 * @return int|false The capacity used, or false if insufficient quota.
34 */
35 public function acquireRetryQuota(bool $isThrottling): int|false
36 {
37 $cost = $isThrottling ? $this->throttlingRetryCost : $this->retryCost;
38
39 if ($cost > $this->availableCapacity) {
40 return false;
41 }
42
43 $this->availableCapacity -= $cost;
44
45 return $cost;
46 }
47
48 /**
49 * Releases quota back to the pool.
50 *
51 * @param int|null $capacityUsed The capacity to release. If null, uses
52 * the no_retry_increment value.
53 */
54 public function releaseQuota(?int $capacityUsed): void
55 {
56 $amount = $capacityUsed ?? $this->noRetryIncrement;
57
58 $this->availableCapacity = min(
59 $this->availableCapacity + $amount,
60 $this->maxCapacity
61 );
62 }
63
64 public function getAvailableCapacity(): int
65 {
66 return $this->availableCapacity;
67 }
68 }