Skip to content

Commit

Permalink
Merge remote branch 'fork/RetryCarefulWire'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Apr 15, 2015
2 parents f58fe13 + 88f2d76 commit 02d1b1b
Show file tree
Hide file tree
Showing 4 changed files with 346 additions and 8 deletions.
67 changes: 61 additions & 6 deletions src/main/java/com/jcabi/github/wire/CarefulWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -72,6 +73,7 @@
@Immutable
@ToString
@EqualsAndHashCode(of = { "origin", "threshold" })
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class CarefulWire implements Wire {

/**
Expand Down Expand Up @@ -115,13 +117,9 @@ public Response send(
) throws IOException {
final Response resp = this.origin
.send(req, home, method, headers, content);
final int remaining = Integer.parseInt(
resp.headers().get("X-RateLimit-Remaining").get(0)
);
final int remaining = this.remainingHeader(resp);
if (remaining < this.threshold) {
final long reset = Long.parseLong(
resp.headers().get("X-RateLimit-Reset").get(0)
);
final long reset = this.resetHeader(resp);
final long now = TimeUnit.MILLISECONDS
.toSeconds(System.currentTimeMillis());
if (reset > now) {
Expand All @@ -142,4 +140,61 @@ public Response send(
}
return resp;
}

/**
* Get the header with the given name from the response.
* If there is no such header, returns null.
* @param resp Response to get header from
* @param headername Name of header to get
* @return The value of the first header with the given name, or null.
*/
private String headerOrNull(
@NotNull(message = "response can't be NULL")
final Response resp,
@NotNull(message = "header name can't be NULL")
final String headername) {
final List<String> values = resp.headers().get(headername);
String value = null;
if (values != null && !values.isEmpty()) {
value = values.get(0);
}
return value;
}

/**
* Returns the value of the X-RateLimit-Remaining header.
* If there is no such header, returns Integer.MAX_VALUE (no rate limit).
* @param resp Response to get header from
* @return Number of requests remaining before the rate limit will be hit
*/
private int remainingHeader(
@NotNull(message = "response can't be NULL")
final Response resp) {
final String remainingstr = this.headerOrNull(
resp,
"X-RateLimit-Remaining"
);
int remaining = Integer.MAX_VALUE;
if (remainingstr != null) {
remaining = Integer.parseInt(remainingstr);
}
return remaining;
}

/**
* Returns the value of the X-RateLimit-Reset header.
* If there is no such header, returns 0 (reset immediately).
* @param resp Response to get header from
* @return Timestamp (in seconds) at which the rate limit will reset
*/
private long resetHeader(
@NotNull(message = "response can't be NULL")
final Response resp) {
final String resetstr = this.headerOrNull(resp, "X-RateLimit-Reset");
long reset = 0;
if (resetstr != null) {
reset = Long.parseLong(resetstr);
}
return reset;
}
}
105 changes: 105 additions & 0 deletions src/main/java/com/jcabi/github/wire/RetryCarefulWire.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Copyright (c) 2013-2015, jcabi.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the jcabi.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcabi.github.wire;

import com.jcabi.aspects.Immutable;
import com.jcabi.http.Request;
import com.jcabi.http.Response;
import com.jcabi.http.Wire;
import com.jcabi.http.wire.RetryWire;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;
import javax.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/**
* Wire that waits if the number of remaining requests per hour is less than
* a given threshold, and in the event of {@link IOException} retries a few
* times before giving up and rethrowing the exception.
*
* <p>Just a wrapper for a {@link com.jcabi.http.wire.RetryWire} that wraps a
* {@link com.jcabi.github.wire.CarefulWire} that wraps the underlying wire.
*
* <p>You can use {@code RetryCarefulWire} with a
* {@link com.jcabi.github.Github} object:
* <pre>
* {@code
* Github github = new RtGithub(
* new RtGithub().entry().through(RetryCarefulWire.class, 50)
* );
* }
* </pre>
*
* @author Chris Rebert ([email protected])
* @version $Id$
* @since 0.23
*/
@Immutable
@ToString
@EqualsAndHashCode(of = { "real" })
public final class RetryCarefulWire implements Wire {
/**
* RetryWire which we're merely wrapping.
*/
private final transient Wire real;

/**
* Public ctor.
*
* @param wire Original wire
* @param threshold Threshold of number of remaining requests, below which
* requests are blocked until reset
*/
public RetryCarefulWire(@NotNull(message = "wire can't be NULL")
final Wire wire, final int threshold) {
this.real = new RetryWire(new CarefulWire(wire, threshold));
}

/**
* {@inheritDoc}
* @checkstyle ParameterNumber (6 lines)
*/
@Override
@NotNull(message = "response can't be NULL")
public Response send(
@NotNull(message = "req can't be NULL") final Request req,
@NotNull(message = "home can't be NULL") final String home,
@NotNull(message = "method can't be NULL")final String method,
@NotNull(message = "headers can't be NULL")
final Collection<Map.Entry<String, String>> headers,
@NotNull(message = "content can't be NULL")
final InputStream content
) throws IOException {
return this.real.send(req, home, method, headers, content);
}
}
51 changes: 49 additions & 2 deletions src/test/java/com/jcabi/github/wire/CarefulWireTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
* @version $Id$
*/
public final class CarefulWireTest {
/**
* HTTP 200 status reason.
*/
private static final String OK = "OK";
/**
* Name of GitHub's number-of-requests-remaining rate limit header.
*/
private static final String REMAINING_HEADER = "X-RateLimit-Remaining";

/**
* CarefulWire can wait until the limit reset.
Expand All @@ -57,13 +65,52 @@ public void waitUntilReset() throws IOException {
.toSeconds(System.currentTimeMillis()) + 5L;
new FakeRequest()
.withStatus(HttpURLConnection.HTTP_OK)
.withReason("OK")
.withHeader("X-RateLimit-Remaining", "9")
.withReason(OK)
.withHeader(REMAINING_HEADER, "9")
.withHeader("X-RateLimit-Reset", String.valueOf(reset))
.through(CarefulWire.class, threshold)
.fetch();
final long now = TimeUnit.MILLISECONDS
.toSeconds(System.currentTimeMillis());
MatcherAssert.assertThat(now, Matchers.greaterThanOrEqualTo(reset));
}

/**
* CarefulWire can tolerate the lack the X-RateLimit-Remaining header.
* @throws IOException If some problem inside
*/
@Test
public void tolerateMissingRateLimitRemainingHeader() throws IOException {
final int threshold = 10;
// @checkstyle MagicNumber (1 lines)
new FakeRequest()
.withStatus(HttpURLConnection.HTTP_OK)
.withReason(OK)
.through(CarefulWire.class, threshold)
.fetch();
MatcherAssert.assertThat(
"Did not crash when X-RateLimit-Remaining header was absent",
true
);
}

/**
* CarefulWire can tolerate the lack the X-RateLimit-Reset header.
* @throws IOException If some problem inside
*/
@Test
public void tolerateMissingRateLimitResetHeader() throws IOException {
final int threshold = 8;
// @checkstyle MagicNumber (1 lines)
new FakeRequest()
.withStatus(HttpURLConnection.HTTP_OK)
.withReason(OK)
.withHeader(REMAINING_HEADER, "7")
.through(CarefulWire.class, threshold)
.fetch();
MatcherAssert.assertThat(
"Did not crash when X-RateLimit-Reset header was absent",
true
);
}
}
Loading

0 comments on commit 02d1b1b

Please sign in to comment.