Skip to content

[SDK-715] Apply the network retry policy to online requests - #1115

Open
franco-zalamena-iterable wants to merge 7 commits into
feature/SDK-715-c1-network-failurefrom
feature/SDK-715-c2-retry-policy
Open

franco-zalamena-iterable wants to merge 7 commits into
feature/SDK-715-c1-network-failurefrom
feature/SDK-715-c2-retry-policy

Conversation

@franco-zalamena-iterable

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

Copy link
Copy Markdown
Contributor

📝 Summary

Online requests use the network retry policy when the app enables it and the project's remote autoRetry is on. Otherwise behaviour is unchanged.

🎟️ Jira Ticket: SDK-715

Epic: SDK-454

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

📖 Description

This PR applies the Phase 2 retry policy to online requests. The policy is only active when IterableNetworkRetryPolicy.isRetryEnabled() is true in the app config and the project's remote autoRetry is on. If either is off, the request keeps the legacy retry loop and behaviour is unchanged.

With the policy active, maxAttempts counts total attempts including the first one, the backoff is fixed and totalDeadline is measured from the start of the first attempt. A retry is only approved if it would start at least 500 ms before the deadline, and the deadline is checked again when the retry actually starts.

Server errors (5xx) and the network failures marked in #1114 are retried. 429, other 4xx, codes from 600 and -1, certificate and other TLS errors and other client errors are terminal. A failed 2xx is terminal for both GET and POST, since the server already answered the request, and that includes a 200 whose body breaks mid-read. A 401 with a JWT error code still goes through the existing auth path.

The saved autoRetry is now loaded before a pending push action is processed, so the replayed push open sees it. This only changes the order, and with either switch off behaviour is unchanged.

Requests stored by offline mode are not changed here, that is SDK-715b.

Two things for review. One logical call keeps a single attempt count and deadline across a JWT re-send. The re-send itself is owned by the auth path and is not counted, and the policy Javadoc says so. Also IterableApi.java is at 1990 lines, 10 under checkstyle's 2000 line FileLength limit.

🧪 How to test?

./gradlew :iterableapi:testDebugUnitTest gives 862 tests, 21 skipped and 0 failures. Checkstyle, lint and the androidTest compile are green. The test for when the policy is active runs for a POST and both GET overloads. The dispatcher tests use a manual clock, so there are no sleeps, and the classifier tests pin the 500 ms margin from both sides. One IterableRequestTaskTest case sends 200 headers over a real socket and drops the connection mid-body, for a GET and a POST. It checks each request is sent once, that the response keeps code 200 with no body and is neither a success nor a network failure, and that with the policy on it fails once and is not retried.

🧾 Changelog

Updated the IterableNetworkRetryPolicy entry and added an entry for online requests with the list of retried failures, under Unreleased in CHANGELOG.md.

🤖 Generated with Claude Code

Comment on lines +28 to +31
@Nullable
IterableNetworkRetryPolicy activeNetworkRetryPolicy(@NonNull IterableNetworkRetryPolicy localPolicy) {
return localPolicy.isRetryEnabled() && autoRetryEnabled ? localPolicy : null;
}

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.

This method uses the autoRetry flag to turn on the network retry policy. The JWT retry also uses this flag (IterableTaskRunner.java:171, IterableRequestTask.java:330). Thus, one flag now controls two different features.

This causes three problems:

  • If a project does not use JWT auto retry, network retry cannot start. This is true also when the app turns on the policy.
  • To stop network retry from the server, you must turn off autoRetry. This also changes how the SDK keeps offline requests after a JWT failure.
  • After [SDK-715] Parse remote config fields independently #1112, a server response without autoRetry sets the flag to false. That stops both features.

SDK-715 asks for a separate server switch that can stop network retry. Suggest adding a new remote key and a new saved value for network retry. When the server does not send the key, set the value to on. Then update the CHANGELOG, the Javadoc of IterableNetworkRetryPolicy, the Javadoc of setNetworkRetryPolicy and the PR description.

}
request.copyRetryStateFrom(iterableApiRequest);
requestDispatcher.executeRetry(request, 0);
int retryCount = request.getNetworkRetryPolicy() != null ? request.getNetworkRetryAttempts() : 0;

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.

After a JWT 401, the SDK sends the request again with a new token. This line gives that new request the current attempt count. Then run() adds one to the count (:101). Thus, the 401 and the new request both use attempts from the policy.

Example: maxAttempts is 3. The server sends 401, then 500, then 500. The SDK stops after only one network retry. If maxAttempts is 2, the SDK does no network retry after the JWT refresh.

The PR description says that the SDK does not count the new request. SDK-714 also says that a JWT 401 must not use network retry attempts.

The test JWT re-send continues the attempt count of the same call does not find this problem. It puts the 401 on the last attempt, where both behaviors send the same number of requests.

Do not only pass attempts - 1 here. If the 401 is on the first attempt, the value becomes 0. Then startNetworkRetryAttempt (:87-88) starts the deadline again. The new request must keep the attempt count without that reset. Suggest also adding a test that puts the 401 on the first attempt and counts all the requests that the SDK sends.

Comment on lines +437 to +445
IterableNetworkRetryPolicy policy = iterableApiRequest.getNetworkRetryPolicy();
if (policy != null) {
return new IterableNetworkRetryClassifier(policy).shouldRetry(
IterableNetworkRetryClassifier.classify(response),
retryCount + 1,
iterableApiRequest.getNetworkRetryStartMillis(),
requestDispatcher.elapsedRealtimeMillis()
);
}

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.

When the policy is on, the SDK now retries online POST requests after a timeout or a lost connection. Examples are track, trackPurchase and updateCart. The failure can occur after the SDK sends the full request body (:195-204). At that time, the server can already have recorded the event. These requests do not have an idempotency key. Thus, a slow server response can cause a duplicate event or a duplicate purchase. The old online retry loop did not retry these failures.

SDK-714 asks for retry on timeouts, so this behaviour can be correct. But the PR must state the decision. Suggest one of these:

  • Write in the Javadoc of IterableNetworkRetryPolicy and in the CHANGELOG that the SDK can send an event more than one time.
  • For POST requests, retry only failures that occur before the SDK sends the request. Examples are UnknownHostException, ConnectException and a connect timeout.

When the local policy is enabled and remote autoRetry is on, online and
immediately-executed requests retry 5xx and network failures with the
policy's attempts, fixed interval and deadline. Other failures, including
failed 2xx, are terminal; otherwise the legacy retry loop is unchanged.

A retry is approved only if it would start at least 500 ms before the
deadline, and the deadline is checked again when the retry starts. A JWT
re-send continues the attempt count and deadline start of the call. The
saved autoRetry is loaded before a pending push action is replayed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The last-approval dispatcher test now uses a fixed failure time and has a
twin 1 ms later that must not retry, so a margin of 0 fails. The deadline
Javadoc states that a non-zero deadline under 500 ms never retries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…onse path

classify(IterableApiResponse) delegates non-zero codes to the int overload,
which already treats codes from 600 as terminal like 714. The new tests pin
that and the -1 case, and the int-overload comment now only claims code 0
for connection errors and timeouts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The new GET and POST tests go through OnlineRequestProcessor with the
policy enabled and remote autoRetry on. MockWebServer sends 200 headers
and then drops the connection in the middle of a chunked body. Each test
expects one request on the server, one failure callback and no success
callback. A fixed-length body is not used because the JVM client returns
the cut body without an error, so it never reaches the body read catch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The two processor tests asserted the "Premature EOF" message. The outer
IOException catch keeps the same message, so they stayed green when the
body read catch rethrew. They now accept any failure message.

A new test calls executeApiRequest for GET and POST against the same
server. It expects code 200, no success, no network failure and a null
body. Code 0 would mean the outer catch handled it, and on a device that
can mark the error as a network failure and retry it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The two socket tests for a broken 200 body are folded into the direct
one, which now also hands the real response to a policy request and
checks it fails once with no retry. The 600 and -1 response checks are
a second column in the status code table, and the immediate offline
request tests are one table. The policy getter, zero deadline and auth
retry state tests are merged into their neighbours.

The millisecond loop and the just past approval socket test are gone.
The classifier deadline test still pins the 500 ms margin as a literal.
Test-only change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The active policy test only sent a POST, so both GET overloads could
skip the policy with the whole suite green. It now runs the same rows
for POST and for each GET overload. Test-only change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@franco-zalamena-iterable
franco-zalamena-iterable force-pushed the feature/SDK-715-c2-retry-policy branch from 66348c9 to 4f0afec 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.

2 participants