Skip to content

Commit

Permalink
Add rate limiting to both clients
Browse files Browse the repository at this point in the history
  • Loading branch information
HackAttack committed Jan 24, 2017
1 parent 15ab420 commit 02943a9
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies {
compile 'org.codehaus.groovy:groovy-json:2.4.7'
compile 'org.codehaus.groovy:groovy-xml:2.4.7'
compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.2'
compile 'com.google.guava:guava:21.0'
testCompile 'junit:junit:4.12'
testCompile 'com.github.tomakehurst:wiremock:2.2.1'
testRuntime 'org.slf4j:slf4j-nop:1.7.16'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class AsyncCloudFlareClient {

private final AsyncHTTPBuilder http

AsyncCloudFlareClient(String apiKey, String email, String url = 'https://api.cloudflare.com/client/v4/') {
AsyncCloudFlareClient(String apiKey, String email, String url = 'https://api.cloudflare.com/client/v4/',
double allowedRequestsPerSecond = 4) {
http = new AsyncHTTPBuilder(uri: url, contentType: ContentType.JSON)
http.client = new RateLimitingHttpClient(http.client, allowedRequestsPerSecond)
http.headers = ['X-Auth-Key': apiKey, 'X-Auth-Email': email, 'User-Agent': 'HackAttack AsyncCloudFlareClient']

// Mimic the response body parsing of RESTClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ class CloudFlareClient {

private final RESTClient http

CloudFlareClient(String apiKey, String email, String url = 'https://api.cloudflare.com/client/v4/') {
CloudFlareClient(String apiKey, String email, String url = 'https://api.cloudflare.com/client/v4/',
double allowedRequestsPerSecond = 4) {
http = new RESTClient(url, ContentType.JSON)
http.client = new RateLimitingHttpClient(http.client, allowedRequestsPerSecond)
http.headers = ['X-Auth-Key': apiKey, 'X-Auth-Email': email, 'User-Agent': 'HackAttack CloudFlareClient']
}

Expand Down
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)
}

}

0 comments on commit 02943a9

Please sign in to comment.