Skip to content

Commit eaff067

Browse files
committed
Some minor fixes for header behavior and efficiency
1 parent f3b98f4 commit eaff067

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

analytics-core/src/main/java/com/segment/analytics/http/SegmentService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
/** REST interface for the Segment API. */
1212
public interface SegmentService {
1313
@POST
14-
Call<UploadResponse> upload(
15-
@Header("X-Retry-Count") Integer retryCount, @Url HttpUrl uploadUrl, @Body Batch batch);
14+
Call<UploadResponse> upload(@Url HttpUrl uploadUrl, @Body Batch batch);
15+
16+
@POST
17+
Call<UploadResponse> uploadWithRetryCount(
18+
@Header("X-Retry-Count") int retryCount, @Url HttpUrl uploadUrl, @Body Batch batch);
1619
}

analytics/src/main/java/com/segment/analytics/internal/AnalyticsClient.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,12 @@ UploadResult upload(int attempt) {
467467
client.log.print(VERBOSE, "Uploading batch %s.", batch.sequence());
468468

469469
try {
470-
Integer headerRetryCount = attempt <= 1 ? null : Integer.valueOf(attempt - 1);
471-
Call<UploadResponse> call = client.service.upload(headerRetryCount, client.uploadUrl, batch);
470+
Call<UploadResponse> call;
471+
if (attempt <= 1) {
472+
call = client.service.upload(client.uploadUrl, batch);
473+
} else {
474+
call = client.service.uploadWithRetryCount(attempt - 1, client.uploadUrl, batch);
475+
}
472476
Response<UploadResponse> response = call.execute();
473477

474478
if (response.isSuccessful()) {
@@ -484,17 +488,24 @@ UploadResult upload(int attempt) {
484488
}
485489

486490
int status = response.code();
487-
String retryAfterHeader = response.headers().get("Retry-After");
488-
Long retryAfterSeconds = parseRetryAfterSeconds(retryAfterHeader);
489491

490-
if (retryAfterSeconds != null && isStatusRetryAfterEligible(status)) {
492+
if (isStatusRetryAfterEligible(status)) {
493+
String retryAfterHeader = response.headers().get("Retry-After");
494+
Long retryAfterSeconds = parseRetryAfterSeconds(retryAfterHeader);
495+
if (retryAfterSeconds != null) {
496+
client.log.print(
497+
DEBUG,
498+
"Could not upload batch %s due to status %s with Retry-After %s seconds. Retrying after delay.",
499+
batch.sequence(),
500+
status,
501+
retryAfterSeconds);
502+
return new UploadResult(RetryStrategy.RETRY_AFTER, retryAfterSeconds);
503+
}
491504
client.log.print(
492505
DEBUG,
493-
"Could not upload batch %s due to status %s with Retry-After %s seconds. Retrying after delay.",
506+
"Status %s did not have a valid Retry-After header.",
494507
batch.sequence(),
495-
status,
496-
retryAfterSeconds);
497-
return new UploadResult(RetryStrategy.RETRY_AFTER, retryAfterSeconds);
508+
status);
498509
}
499510

500511
if (isStatusRetryWithBackoff(status)) {

0 commit comments

Comments
 (0)