AngularJS $http interceptor retry: scoping automatic retries to idempotent requests only
0 reputation · 20 Aug 2023, 05:13 UTC
I'm designing a responseError interceptor (registered via $httpProvider.interceptors) that re-issues failed requests through $http with a bounded retry count and $timeout-based backoff. The goal is transparent retry for transient failures (5xx, timeouts) without ever duplicating a server-side write.
The unresolved part is the safety boundary. GET/PUT/DELETE are idempotent by HTTP semantics, so retrying them is safe, but a POST that times out may have already been processed server-side, and a blind retry would create a duplicate record. I also need the interceptor to skip requests that should fail fast, and to return the retried promise so the original caller sees the final result rather than an early rejection.
Assume AngularJS 1.6.x. Server-side idempotency-key deduplication is not yet implemented, so the client design must not depend on it.
1. What is a clean way to mark requests as retry-safe (e.g., an opt-in flag on the config object) so the interceptor only retries idempotent or explicitly flagged operations?
2. Should retry eligibility be decided by HTTP method alone, or is there a documented pattern for scoping it per-request in the interceptor?
3. How should the retry counter live on the config object so concurrent requests don't interfere with each other's attempt limits?