Skip to content

[SDK-714] Add network retry policy config and classifier - #1111

Open
franco-zalamena-iterable wants to merge 9 commits into
masterfrom
feature/SDK-714-network-retry-policy
Open

franco-zalamena-iterable wants to merge 9 commits into
masterfrom
feature/SDK-714-network-retry-policy

Conversation

@franco-zalamena-iterable

@franco-zalamena-iterable franco-zalamena-iterable commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

📝 Summary

Adds a public IterableNetworkRetryPolicy config and a package-private classifier for retriable request failures. Nothing in the SDK reads the policy yet.

🎟️ Jira Ticket: SDK-714

Stack, in order: #1111 (SDK-714), #1112, #1113, #1114, #1115. This is #1111.

📖 Description

This PR is the first slice of unified network retry and the base of a stack. SDK-715 goes on top of it and applies the policy to requests, then SDK-716 adds the typed public API.

It adds IterableNetworkRetryPolicy, IterableConfig.Builder.setNetworkRetryPolicy and the public getter IterableConfig.getNetworkRetryPolicy(). Retry is off by default, and once enabled the defaults are 5 max attempts, a fixed 2 s backoff and a 60 s total deadline.

The interval and the deadline are in seconds as doubles, set with BackoffPolicy.fixed(double) and setTotalDeadline(double) and read with getInterval() and getTotalDeadline(). Invalid values are logged and ignored instead of throwing, so setMaxAttempts and setTotalDeadline keep the previous value and fixed returns the default 2 s interval. A null policy or backoff policy is logged and treated as the default.

The classifier treats 0 and 5xx as retriable. 429 and the other 4xx are terminal, which matches iOS (SDK-455). A 401 with a JWT error code goes to the existing auth path.

Nothing in the request layer reads the policy yet, so request behaviour is unchanged. The only change to existing code is matchesJWTErrorCodes going from private to package-private, so the classifier can reuse it.

The public docs for the new retry setting will be updated with SDK-715 (#1115, where the policy is applied to requests), not in this PR, since the setting has no effect until then.

A few things to keep in mind for SDK-715. maxAttempts counts total attempts including the first one, while today's loop retries 5xx up to 6 times. Code 0 also covers client failures that are not network related, #1114 marks the network ones and #1115 only retries those. The offline queue treats 429 as transient today, so it will diverge from this classifier.

🧪 How to test?

./gradlew :iterableapi:testDebugUnitTest --rerun gives 832 tests, 21 skipped and 0 failures. Checkstyle, lint and the androidTest compile are green. The classifier tests use injected timestamps, so there are no sleeps.

🧾 Changelog

Added an entry under Unreleased in CHANGELOG.md.

🤖 Generated with Claude Code

@franco-zalamena-iterable
franco-zalamena-iterable requested a review from a team as a code owner November 19, 2026 16:57
// including connection errors and timeouts.
@NonNull
static Outcome classify(int responseCode, @Nullable JSONObject json) {
if (responseCode >= 200 && responseCode < 300) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

classify treats every 2xx as SUCCESS, but IterableRequestTask reports several 2xx outcomes as failures:

  • 201–299 → failure(…, "Received non-200 response") (IterableRequestTask.java:267)
  • 200 with an unparseable or non-object body → failure (:255, :259)
  • 200 with an empty body → failure (:262)
  • 200 where reading the body threw an IOException, e.g. a read timeout → failure (:206-208, :264)

So once SDK-715 wires this in, those failures come back as SUCCESS. Retry decisions are unaffected today, because shouldRetry only acts on RETRIABLE. But the read-timeout case is exactly the kind of failure this policy says it covers, and SDK-716's typed failures would have no category for these failures.

The test at IterableNetworkRetryClassifierTest.kt:36 (299 to Outcome.SUCCESS) locks this in. Code -1 (from getResponseCode()) also falls through to TERMINAL with no comment or test.

Suggest classifying the response the task already builds, which carries the success flag:

@NonNull
static Outcome classify(@NonNull IterableApiResponse response) {
    if (response.success) {
        return Outcome.SUCCESS;
    }
    // ...then the existing code/JSON rules, using response.responseCode and response.responseJson
}

Then replace the 299 case with a failed non-200 2xx, add a failed 200, and add -1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this to #1115 since it needs the response the task builds. There classify takes the IterableApiResponse, only returns SUCCESS when success is true and treats a failed 2xx as terminal, and -1 is a row in statusCodesAreClassified.

Comment on lines +40 to +41
boolean shouldRetry(@NonNull Outcome outcome, int failedAttempt, long firstAttemptStartMillis, long nowMillis) {
if (!policy.isRetryEnabled() || outcome != Outcome.RETRIABLE || failedAttempt >= policy.getMaxAttempts()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

failedAttempt doesn't say where it starts counting. The tests treat it as 1-based (retriesStopAfterMaxAttempts, singleAttemptNeverRetries). The existing request counter is 0-based: IterableRequestDispatcher.java:79 starts the first task at retryCount 0, and retries pass it on. If SDK-715 passes retryCount straight in, the default policy makes 6 attempts instead of 5. With maxAttempts = 1, failedAttempt = 0 passes 0 >= 1 and allows a retry.

Suggest documenting it ("1-based number of the attempt that just failed") and rejecting values below 1, plus a test for 0.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #1115 the caller passes retryCount + 1, so the default policy makes 5 attempts and not 6.

Comment on lines +24 to +25
// Expects the code as IterableRequestTask reports it: 0 for any client-side failure to get a response,
// including connection errors and timeouts.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code 0 isn't only "a client-side failure to get a response". IterableRequestTask maps four exception types to failure(0, …) (:269-281): JSONException, IOException, ArrayIndexOutOfBoundsException and the catch-all Exception. IterableRequestDispatcher.rejectedRequestResponse() (:140-142) also returns 0 when the executor rejects a request.

Only IOException, which includes connect and read timeouts, is a network failure. The others are deterministic. For example, a JSONException from building the GET query (json.getString(key), :127) fails the same way on every retry. SDK-714 asks for "a 5xx, timeout, or connection failure" to be retriable, and SDK-455 FR-2 says "everything else terminal".

The PR body already flags this for SDK-715. But the comment here, the CHANGELOG and the IterableNetworkRetryPolicy Javadoc ("any client-side failure including connection errors and timeouts") commit to the wider rule in public text.

Suggest having the task record whether a code-0 failure came from an IOException (or the dispatcher's rejection), classify only that case as RETRIABLE, and narrow the wording to "connection errors and timeouts". If this moves to SDK-715, is should be made an explicit acceptance criterion there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The narrowing is in the next PRs, #1114 marks which I/O errors are network failures and #1115 makes every other code 0 failure terminal, including the dispatcher's rejection. I also added it as an acceptance criterion on SDK-715.

Adds the public IterableNetworkRetryPolicy and
IterableConfig.Builder.setNetworkRetryPolicy(), with retry disabled by
default (5 attempts, fixed 2s interval, 60s total deadline), plus a
package-private classifier: 5xx and transport failures are retriable,
429 and other 4xx are terminal, JWT 401 goes to the auth path. Nothing
reads the policy yet, so request behaviour is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Reword CHANGELOG and Javadoc: when the policy is not enabled the existing
  request retry behaviour is unchanged; code 0 is any client-side failure to
  get a response
- setBackoffPolicy(null) falls back to the fixed 2000 ms default
- Make the deadline check overflow-safe
- Drop the public BackoffPolicy.Type enum
- Add boundary, custom-config and null-handling tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The public time values are now double seconds, like
setExpiringAuthTokenRefreshPeriod: BackoffPolicy.fixed(double) with
getInterval(), and Builder.setTotalDeadline(double) with
getTotalDeadline(). The millisecond getters stay package-private for
the classifier. The conversion rounds to whole milliseconds and
saturates instead of overflowing.

The Builder's default interval doesn't go through fixed(), since that
initializes the outer class and leaves DEFAULT without a backoff policy
when the Builder loads first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
setMaxAttempts below 1, and a negative, NaN or infinite value in
setTotalDeadline or BackoffPolicy.fixed, now log an error and keep the
previous value instead of throwing, like
setExpiringAuthTokenRefreshPeriod. An invalid fixed() returns the
default 2 second interval. Values above about 10 years are clamped,
and zero is still valid.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The existing backwards clock test still passed with the now < start
guard removed, since the elapsed < 0 check caught it too. Starting at
Long.MAX_VALUE with now at Long.MIN_VALUE wraps elapsed to +1, so only
that guard stops the retry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A positive interval or deadline under half a millisecond rounded to
0 ms, and a 0 ms deadline means no deadline. Any positive value now
converts to at least 1 ms, and zero stays zero.

Also adds a 1.001 second rounding case, since 0.25 and 1.5 are exact in
binary and pass with truncation too. hugeDelayDoesNotOverflowTheDeadlineCheck
is now delayLongerThanTheDeadlineNeverRetries, since the clamp means it
no longer tests an overflow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The 38 policy and classifier tests are now 17. Tests that only changed
one value are rows in a table, and every row they asserted is still
there. The reflection check that the config getter is public and the
re-check of the 2 second default delay are gone, since nothing else
depends on them. Test-only change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@franco-zalamena-iterable
franco-zalamena-iterable force-pushed the feature/SDK-714-network-retry-policy branch from eb051bd to e32db18 Compare November 27, 2026 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants