mirror of
https://github.com/HeyPuter/firefox.git
synced 2026-11-27 18:01:44 -10:00
======== https://github.com/mozilla-services/android-sync/commit/cd7edfa0b5d3ee8c4344af1183c6e31a584757ef Author: Nick Alexander <[email protected]> Bug 1140812 - Part 3: React to Backoff and Retry-After headers. ======== https://github.com/mozilla-services/android-sync/commit/8581f5a572d387f7540088210a5b745574dcf6af Author: Nick Alexander <[email protected]> Date: Fri Mar 27 15:30:33 2015 -0700 Bug 1140812 - Part 2: Include request in HTTP response observation callbacks. This allows to only handle responses from certain hosts. ======== https://github.com/mozilla-services/android-sync/commit/05b50325dbd262bd69df27f1ed023ecca250cc8a Author: Nick Alexander <[email protected]> Date: Fri Mar 27 14:47:38 2015 -0700 Bug 1140812 - Part 1: Generalize from one to many HTTP response observers. CopyOnWriteArrayList is a reasonable choice here: we have few writes but many iterations. See http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html ======== https://github.com/mozilla-services/android-sync/commit/5950219343910cbf9921f0e232562ab2d12b2387 Author: Nick Alexander <[email protected]> Date: Fri Mar 27 16:04:07 2015 -0700 Bug 1140812 - Pre: Modernize backoffInSeconds. Sync uses X-Backoff; newer services, including Reading List, use Backoff.
116 lines
3.3 KiB
Java
116 lines
3.3 KiB
Java
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
package org.mozilla.gecko.sync.net;
|
|
|
|
import org.mozilla.gecko.sync.Utils;
|
|
|
|
import ch.boye.httpclientandroidlib.HttpResponse;
|
|
|
|
public class SyncResponse extends MozResponse {
|
|
public SyncResponse(HttpResponse res) {
|
|
super(res);
|
|
}
|
|
|
|
/**
|
|
* @return A number of seconds, or -1 if the 'X-Weave-Backoff' header was not
|
|
* present.
|
|
*/
|
|
public int weaveBackoffInSeconds() throws NumberFormatException {
|
|
return this.getIntegerHeader("x-weave-backoff");
|
|
}
|
|
|
|
/**
|
|
* @return A number of seconds, or -1 if the 'X-Backoff' header was not
|
|
* present.
|
|
*/
|
|
public int xBackoffInSeconds() throws NumberFormatException {
|
|
return this.getIntegerHeader("x-backoff");
|
|
}
|
|
|
|
/**
|
|
* Extract a number of seconds, or -1 if none of the specified headers were present.
|
|
*
|
|
* @param includeRetryAfter
|
|
* if <code>true</code>, the Retry-After header is excluded. This is
|
|
* useful for processing non-error responses where a Retry-After
|
|
* header would be unexpected.
|
|
* @return the maximum of the three possible backoff headers, in seconds.
|
|
*/
|
|
public int totalBackoffInSeconds(boolean includeRetryAfter) {
|
|
int retryAfterInSeconds = -1;
|
|
if (includeRetryAfter) {
|
|
try {
|
|
retryAfterInSeconds = retryAfterInSeconds();
|
|
} catch (NumberFormatException e) {
|
|
}
|
|
}
|
|
|
|
int weaveBackoffInSeconds = -1;
|
|
try {
|
|
weaveBackoffInSeconds = weaveBackoffInSeconds();
|
|
} catch (NumberFormatException e) {
|
|
}
|
|
|
|
int backoffInSeconds = -1;
|
|
try {
|
|
backoffInSeconds = xBackoffInSeconds();
|
|
} catch (NumberFormatException e) {
|
|
}
|
|
|
|
int totalBackoff = Math.max(retryAfterInSeconds, Math.max(backoffInSeconds, weaveBackoffInSeconds));
|
|
if (totalBackoff < 0) {
|
|
return -1;
|
|
} else {
|
|
return totalBackoff;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return A number of milliseconds, or -1 if neither the 'Retry-After',
|
|
* 'X-Backoff', or 'X-Weave-Backoff' header were present.
|
|
*/
|
|
public long totalBackoffInMilliseconds() {
|
|
long totalBackoff = totalBackoffInSeconds(true);
|
|
if (totalBackoff < 0) {
|
|
return -1;
|
|
} else {
|
|
return 1000 * totalBackoff;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The timestamp returned from a Sync server is a decimal number of seconds,
|
|
* e.g., 1323393518.04.
|
|
*
|
|
* We want milliseconds since epoch.
|
|
*
|
|
* @return milliseconds since the epoch, as a long, or -1 if the header
|
|
* was missing or invalid.
|
|
*/
|
|
public long normalizedWeaveTimestamp() {
|
|
String h = "x-weave-timestamp";
|
|
if (!this.hasHeader(h)) {
|
|
return -1;
|
|
}
|
|
|
|
return Utils.decimalSecondsToMilliseconds(this.response.getFirstHeader(h).getValue());
|
|
}
|
|
|
|
public int weaveRecords() throws NumberFormatException {
|
|
return this.getIntegerHeader("x-weave-records");
|
|
}
|
|
|
|
public int weaveQuotaRemaining() throws NumberFormatException {
|
|
return this.getIntegerHeader("x-weave-quota-remaining");
|
|
}
|
|
|
|
public String weaveAlert() {
|
|
if (this.hasHeader("x-weave-alert")) {
|
|
return this.response.getFirstHeader("x-weave-alert").getValue();
|
|
}
|
|
return null;
|
|
}
|
|
}
|