-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15ab420
commit 02943a9
Showing
4 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/groovy/com/mhackner/cloudflare/RateLimitingHttpClient.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.mhackner.cloudflare | ||
|
||
import com.google.common.util.concurrent.RateLimiter | ||
|
||
import org.apache.http.HttpHost | ||
import org.apache.http.HttpRequest | ||
import org.apache.http.HttpResponse | ||
import org.apache.http.client.ClientProtocolException | ||
import org.apache.http.client.HttpClient | ||
import org.apache.http.client.ResponseHandler | ||
import org.apache.http.client.methods.HttpUriRequest | ||
import org.apache.http.conn.ClientConnectionManager | ||
import org.apache.http.params.HttpParams | ||
import org.apache.http.protocol.HttpContext | ||
|
||
class RateLimitingHttpClient implements HttpClient { | ||
|
||
private final HttpClient delegate | ||
private final RateLimiter rateLimiter | ||
|
||
RateLimitingHttpClient(HttpClient delegate, double qps) { | ||
this.delegate = delegate | ||
rateLimiter = RateLimiter.create(qps) | ||
} | ||
|
||
@Override | ||
HttpParams getParams() { | ||
delegate.params | ||
} | ||
|
||
@Override | ||
ClientConnectionManager getConnectionManager() { | ||
delegate.connectionManager | ||
} | ||
|
||
@Override | ||
HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException { | ||
rateLimiter.acquire() | ||
delegate.execute(request) | ||
|
||
} | ||
|
||
@Override | ||
HttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException, ClientProtocolException { | ||
rateLimiter.acquire() | ||
delegate.execute(request, context) | ||
} | ||
|
||
@Override | ||
HttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException { | ||
rateLimiter.acquire() | ||
delegate.execute(target, request) | ||
} | ||
|
||
@Override | ||
HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) | ||
throws IOException, ClientProtocolException { | ||
rateLimiter.acquire() | ||
delegate.execute(target, request, context) | ||
} | ||
|
||
@Override | ||
<T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) | ||
throws IOException, ClientProtocolException { | ||
rateLimiter.acquire() | ||
delegate.execute(request, responseHandler) | ||
} | ||
|
||
@Override | ||
<T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) | ||
throws IOException, ClientProtocolException { | ||
rateLimiter.acquire() | ||
delegate.execute(request, responseHandler, context) | ||
} | ||
|
||
@Override | ||
<T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler) | ||
throws IOException, ClientProtocolException { | ||
rateLimiter.acquire() | ||
delegate.execute(target, request, responseHandler) | ||
} | ||
|
||
@Override | ||
<T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) | ||
throws IOException, ClientProtocolException { | ||
rateLimiter.acquire() | ||
delegate.execute(target, request, responseHandler, context) | ||
} | ||
|
||
} |