Skip to content

A free open source IT asset/license management system

Fixed #19774 - AxM not respecting 429 backoff+retry

snipe42a17d71929e
diff --git a/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerClient.php b/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerClient.php
index e7ec23c7bd..9ce399fcea 100644
--- a/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerClient.php
+++ b/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerClient.php
@@ -218,9 +218,16 @@ class AppleBusinessManagerClient
}
/**
- * Base HTTP client with resolved bearer token. Retries on 429 with
- * exponential backoff. Apple's rate limit is generous but bursts
- * during a full org pull can trip it briefly.
+ * Base HTTP client with resolved bearer token. On 429 the sleep
+ * callback honors Apple's `Retry-After` header (capped at 2
+ * minutes so a very long server-side cooldown doesn't stall the
+ * sync beyond user patience). Falls back to exponential backoff
+ * when Retry-After is missing from the API response.
+ *
+ * The N+1 per-device enrichment calls (AppleCare, Activation Lock,
+ * MDM details) are still the biggest cost on large pulls - admins
+ * who hit 429 even with this retry logic can trim mappings that
+ * drive those extra per-device fetches.
*/
private function request(): PendingRequest
{
@@ -229,8 +236,24 @@ class AppleBusinessManagerClient
->withToken($this->bearer())
->acceptJson()
->timeout(30)
- ->retry(3, 500, fn (\Exception $e) => $e instanceof \Illuminate\Http\Client\ConnectionException
- || ($e instanceof \Illuminate\Http\Client\RequestException && $e->response->status() === 429));
+ ->retry(
+ times: 8,
+ sleepMilliseconds: function (int $attempt, \Exception $e): int {
+ if ($e instanceof \Illuminate\Http\Client\RequestException && $e->response->status() === 429) {
+ $retryAfter = (int) ($e->response->header('Retry-After') ?: 0);
+ if ($retryAfter > 0) {
+ return min($retryAfter, 120) * 1000;
+ }
+ }
+
+ // Exponential: 500ms, 1s, 2s, 4s, 8s, 16s, 32s, 60s
+ // plus up to 250ms jitter so parallel runs don't
+ // retry in lockstep.
+ return min(60_000, 500 * (2 ** ($attempt - 1))) + random_int(0, 250);
+ },
+ when: fn(\Exception $e) => $e instanceof \Illuminate\Http\Client\ConnectionException
+ || ($e instanceof \Illuminate\Http\Client\RequestException && $e->response->status() === 429),
+ );
}
/**
diff --git a/tests/Feature/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapterTest.php b/tests/Feature/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapterTest.php
index 789d2f7d89..ad55891f57 100644
--- a/tests/Feature/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapterTest.php
+++ b/tests/Feature/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapterTest.php
@@ -369,6 +369,39 @@ class AppleBusinessManagerAdapterTest extends TestCase
$this->assertSame(1, \App\Models\Asset::query()->count());
}
+
+ public function test_retries_on_429_and_honors_retry_after_header()
+ {
+ $adapter = $this->configuredAdapter();
+
+ // Serve 429 once, then 200 on the retry. Http::sequence replays
+ // responses in order per matching URL.
+ Http::fake([
+ 'account.apple.com/*' => Http::response(['access_token' => 'stub-bearer']),
+ 'api-business.apple.com/v1/mdmServers' => Http::sequence()
+ ->push(['detail' => 'rate limited'], 429, ['Retry-After' => '1'])
+ ->push(['data' => []], 200),
+ 'api-business.apple.com/v1/orgDevices*' => Http::response(['data' => []]),
+ ]);
+
+ // The pull run completes without the 429 surfacing. Second call
+ // to /v1/mdmServers (the retry) returned 200. Total of 2 sends
+ // to that endpoint proves the retry actually fired.
+ $records = iterator_to_array($adapter->pull());
+
+ $this->assertSame([], $records);
+
+ $mdmServerHits = 0;
+ Http::assertSent(function ($request) use (&$mdmServerHits) {
+ if (str_contains($request->url(), '/v1/mdmServers') && !str_contains($request->url(), 'relationships')) {
+ $mdmServerHits++;
+ }
+
+ return true;
+ });
+ $this->assertSame(2, $mdmServerHits, 'Client should have retried the 429 once.');
+ }
+
/**
* @param array<int, string>|null $productFamilies null = leave the stored filter alone; [] = force to empty
*/