Skip to content

Commit 580ba50

Browse files
author
Stephane Landelle
committed
Format + organize imports on Grizzly and extras
1 parent 04be5a6 commit 580ba50

File tree

86 files changed

+591
-1329
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+591
-1329
lines changed

extras/guava/src/main/java/org/asynchttpclient/extra/RateLimitedThrottleRequestFilter.java

Lines changed: 69 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -21,75 +21,72 @@
2121
* it will only spend another 500 ms waiting for the rate limiter.
2222
*/
2323
public class RateLimitedThrottleRequestFilter implements RequestFilter {
24-
private final static Logger logger = LoggerFactory.getLogger(RateLimitedThrottleRequestFilter.class);
25-
private final Semaphore available;
26-
private final int maxWaitMs;
27-
private final RateLimiter rateLimiter;
28-
29-
public RateLimitedThrottleRequestFilter(int maxConnections, double rateLimitPerSecond) {
30-
this(maxConnections, rateLimitPerSecond, Integer.MAX_VALUE);
31-
}
32-
33-
public RateLimitedThrottleRequestFilter(int maxConnections, double rateLimitPerSecond, int maxWaitMs) {
34-
this.maxWaitMs = maxWaitMs;
35-
this.rateLimiter = RateLimiter.create(rateLimitPerSecond);
36-
available = new Semaphore(maxConnections, true);
37-
}
38-
39-
/**
40-
* {@inheritDoc}
41-
*/
42-
@Override
43-
public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException {
44-
try {
45-
if (logger.isDebugEnabled()) {
46-
logger.debug("Current Throttling Status {}", available.availablePermits());
47-
}
48-
49-
long startOfWait = System.currentTimeMillis();
50-
attemptConcurrencyPermitAcquistion(ctx);
51-
52-
attemptRateLimitedPermitAcquistion(ctx, startOfWait);
53-
} catch (InterruptedException e) {
54-
throw new FilterException(String.format("Interrupted Request %s with AsyncHandler %s", ctx.getRequest(),
55-
ctx.getAsyncHandler()));
56-
}
57-
58-
return new FilterContext.FilterContextBuilder<T>(ctx).asyncHandler(
59-
new AsyncHandlerWrapper<T>(ctx.getAsyncHandler(), available)).build();
60-
}
61-
62-
private <T> void attemptRateLimitedPermitAcquistion(FilterContext<T> ctx, long startOfWait) throws FilterException {
63-
long wait = getMillisRemainingInMaxWait(startOfWait);
64-
65-
if (!rateLimiter.tryAcquire(wait, TimeUnit.MILLISECONDS)) {
66-
throw new FilterException(String.format(
67-
"Wait for rate limit exceeded during processing Request %s with AsyncHandler %s", ctx.getRequest(),
68-
ctx.getAsyncHandler()));
69-
}
70-
}
71-
72-
private <T> void attemptConcurrencyPermitAcquistion(FilterContext<T> ctx) throws InterruptedException,
73-
FilterException {
74-
if (!available.tryAcquire(maxWaitMs, TimeUnit.MILLISECONDS)) {
75-
throw new FilterException(String.format("No slot available for processing Request %s with AsyncHandler %s",
76-
ctx.getRequest(), ctx.getAsyncHandler()));
77-
}
78-
}
79-
80-
private long getMillisRemainingInMaxWait(long startOfWait) {
81-
int MINUTE_IN_MILLIS = 60000;
82-
long durationLeft = maxWaitMs - (System.currentTimeMillis() - startOfWait);
83-
long nonNegativeDuration = Math.max(durationLeft, 0);
84-
85-
// have to reduce the duration because there is a boundary case inside the Guava
86-
// rate limiter where if the duration to wait is near Long.MAX_VALUE, the rate
87-
// limiter's internal calculations can exceed Long.MAX_VALUE resulting in a
88-
// negative number which causes the tryAcquire() method to fail unexpectedly
89-
if (Long.MAX_VALUE - nonNegativeDuration < MINUTE_IN_MILLIS) {
90-
return nonNegativeDuration - MINUTE_IN_MILLIS;
91-
}
92-
93-
return nonNegativeDuration;
94-
}
95-
}
24+
private final static Logger logger = LoggerFactory.getLogger(RateLimitedThrottleRequestFilter.class);
25+
private final Semaphore available;
26+
private final int maxWaitMs;
27+
private final RateLimiter rateLimiter;
28+
29+
public RateLimitedThrottleRequestFilter(int maxConnections, double rateLimitPerSecond) {
30+
this(maxConnections, rateLimitPerSecond, Integer.MAX_VALUE);
31+
}
32+
33+
public RateLimitedThrottleRequestFilter(int maxConnections, double rateLimitPerSecond, int maxWaitMs) {
34+
this.maxWaitMs = maxWaitMs;
35+
this.rateLimiter = RateLimiter.create(rateLimitPerSecond);
36+
available = new Semaphore(maxConnections, true);
37+
}
38+
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
@Override
43+
public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException {
44+
try {
45+
if (logger.isDebugEnabled()) {
46+
logger.debug("Current Throttling Status {}", available.availablePermits());
47+
}
48+
49+
long startOfWait = System.currentTimeMillis();
50+
attemptConcurrencyPermitAcquistion(ctx);
51+
52+
attemptRateLimitedPermitAcquistion(ctx, startOfWait);
53+
} catch (InterruptedException e) {
54+
throw new FilterException(String.format("Interrupted Request %s with AsyncHandler %s", ctx.getRequest(), ctx.getAsyncHandler()));
55+
}
56+
57+
return new FilterContext.FilterContextBuilder<T>(ctx).asyncHandler(new AsyncHandlerWrapper<T>(ctx.getAsyncHandler(), available))
58+
.build();
59+
}
60+
61+
private <T> void attemptRateLimitedPermitAcquistion(FilterContext<T> ctx, long startOfWait) throws FilterException {
62+
long wait = getMillisRemainingInMaxWait(startOfWait);
63+
64+
if (!rateLimiter.tryAcquire(wait, TimeUnit.MILLISECONDS)) {
65+
throw new FilterException(String.format("Wait for rate limit exceeded during processing Request %s with AsyncHandler %s",
66+
ctx.getRequest(), ctx.getAsyncHandler()));
67+
}
68+
}
69+
70+
private <T> void attemptConcurrencyPermitAcquistion(FilterContext<T> ctx) throws InterruptedException, FilterException {
71+
if (!available.tryAcquire(maxWaitMs, TimeUnit.MILLISECONDS)) {
72+
throw new FilterException(String.format("No slot available for processing Request %s with AsyncHandler %s", ctx.getRequest(),
73+
ctx.getAsyncHandler()));
74+
}
75+
}
76+
77+
private long getMillisRemainingInMaxWait(long startOfWait) {
78+
int MINUTE_IN_MILLIS = 60000;
79+
long durationLeft = maxWaitMs - (System.currentTimeMillis() - startOfWait);
80+
long nonNegativeDuration = Math.max(durationLeft, 0);
81+
82+
// have to reduce the duration because there is a boundary case inside the Guava
83+
// rate limiter where if the duration to wait is near Long.MAX_VALUE, the rate
84+
// limiter's internal calculations can exceed Long.MAX_VALUE resulting in a
85+
// negative number which causes the tryAcquire() method to fail unexpectedly
86+
if (Long.MAX_VALUE - nonNegativeDuration < MINUTE_IN_MILLIS) {
87+
return nonNegativeDuration - MINUTE_IN_MILLIS;
88+
}
89+
90+
return nonNegativeDuration;
91+
}
92+
}

extras/jdeferred/src/main/java/org/asynchttpclient/extra/AsyncHttpDeferredObject.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,34 @@
2626
import java.io.IOException;
2727

2828
public class AsyncHttpDeferredObject extends DeferredObject<Response, Throwable, HttpProgress> {
29-
public AsyncHttpDeferredObject(BoundRequestBuilder builder) throws IOException {
30-
builder.execute(new AsyncCompletionHandler<Void>() {
31-
@Override
32-
public Void onCompleted(Response response) throws Exception {
33-
AsyncHttpDeferredObject.this.resolve(response);
34-
return null;
35-
}
36-
37-
@Override
38-
public void onThrowable(Throwable t) {
39-
AsyncHttpDeferredObject.this.reject(t);
40-
}
41-
42-
@Override
43-
public AsyncHandler.STATE onContentWriteProgress(
44-
long amount, long current, long total) {
45-
AsyncHttpDeferredObject.this.notify(new ContentWriteProgress(amount, current, total));
46-
return super.onContentWriteProgress(amount, current, total);
47-
}
48-
49-
@Override
50-
public AsyncHandler.STATE onBodyPartReceived(
51-
HttpResponseBodyPart content) throws Exception {
52-
AsyncHttpDeferredObject.this.notify(new HttpResponseBodyPartProgress(content));
53-
return super.onBodyPartReceived(content);
54-
}
55-
});
56-
}
57-
58-
public static Promise<Response, Throwable, HttpProgress> promise(final BoundRequestBuilder builder) throws IOException {
59-
return new AsyncHttpDeferredObject(builder).promise();
60-
}
29+
public AsyncHttpDeferredObject(BoundRequestBuilder builder) throws IOException {
30+
builder.execute(new AsyncCompletionHandler<Void>() {
31+
@Override
32+
public Void onCompleted(Response response) throws Exception {
33+
AsyncHttpDeferredObject.this.resolve(response);
34+
return null;
35+
}
36+
37+
@Override
38+
public void onThrowable(Throwable t) {
39+
AsyncHttpDeferredObject.this.reject(t);
40+
}
41+
42+
@Override
43+
public AsyncHandler.STATE onContentWriteProgress(long amount, long current, long total) {
44+
AsyncHttpDeferredObject.this.notify(new ContentWriteProgress(amount, current, total));
45+
return super.onContentWriteProgress(amount, current, total);
46+
}
47+
48+
@Override
49+
public AsyncHandler.STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
50+
AsyncHttpDeferredObject.this.notify(new HttpResponseBodyPartProgress(content));
51+
return super.onBodyPartReceived(content);
52+
}
53+
});
54+
}
55+
56+
public static Promise<Response, Throwable, HttpProgress> promise(final BoundRequestBuilder builder) throws IOException {
57+
return new AsyncHttpDeferredObject(builder).promise();
58+
}
6159
}

extras/jdeferred/src/main/java/org/asynchttpclient/extra/ContentWriteProgress.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,30 @@
1616
package org.asynchttpclient.extra;
1717

1818
public class ContentWriteProgress implements HttpProgress {
19-
private final long amount;
20-
private final long current;
21-
private final long total;
19+
private final long amount;
20+
private final long current;
21+
private final long total;
2222

23-
public ContentWriteProgress(long amount, long current, long total) {
24-
this.amount = amount;
25-
this.current = current;
26-
this.total = total;
27-
}
23+
public ContentWriteProgress(long amount, long current, long total) {
24+
this.amount = amount;
25+
this.current = current;
26+
this.total = total;
27+
}
2828

29-
public long getAmount() {
30-
return amount;
31-
}
29+
public long getAmount() {
30+
return amount;
31+
}
3232

33-
public long getCurrent() {
34-
return current;
35-
}
33+
public long getCurrent() {
34+
return current;
35+
}
3636

37-
public long getTotal() {
38-
return total;
39-
}
37+
public long getTotal() {
38+
return total;
39+
}
4040

41-
@Override
42-
public String toString() {
43-
return "ContentWriteProgress [amount=" + amount + ", current="
44-
+ current + ", total=" + total + "]";
45-
}
46-
41+
@Override
42+
public String toString() {
43+
return "ContentWriteProgress [amount=" + amount + ", current=" + current + ", total=" + total + "]";
44+
}
4745
}

extras/jdeferred/src/main/java/org/asynchttpclient/extra/HttpResponseBodyPartProgress.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
import org.asynchttpclient.HttpResponseBodyPart;
1919

2020
public class HttpResponseBodyPartProgress implements HttpProgress {
21-
private final HttpResponseBodyPart part;
21+
private final HttpResponseBodyPart part;
2222

23-
public HttpResponseBodyPartProgress(HttpResponseBodyPart part) {
24-
this.part = part;
25-
}
23+
public HttpResponseBodyPartProgress(HttpResponseBodyPart part) {
24+
this.part = part;
25+
}
2626

27-
public HttpResponseBodyPart getPart() {
28-
return part;
29-
}
30-
31-
@Override
32-
public String toString() {
33-
return "HttpResponseBodyPartProgress [part=" + part + "]";
34-
}
27+
public HttpResponseBodyPart getPart() {
28+
return part;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return "HttpResponseBodyPartProgress [part=" + part + "]";
34+
}
3535
}

0 commit comments

Comments
 (0)