Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
jerry-http - Common Java Functionality Copyright (c) 2012-2014, Sandeep Gupta http://sangupta.com/projects/jerry-http Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package com.sangupta.jerry.http;
 
 import java.util.Map;
 
An implementation of the org.apache.http.client.HttpClient that supports rate limiting checks over given hosts/routes. The implementation needs an actual org.apache.http.client.HttpClient implementation which does the magic of all executing all calls.

Author(s):
sangupta
Since:
0.3
 
 public class HttpRateLimitingClient implements HttpClient {

Rate-limit mappings are their associated meta-data is kept here
 
 	private static final Map<StringRateLimitValuesRATE_LIMITED_HOSTS = new HashMap<StringRateLimitValues>();

The actual client instance that does all the magic
 
 	private HttpClient actualClient;

Boolean flag to skip checks if there are no limited hosts defined
 
 	private boolean hasHosts = false;

Constructor - takes an actual implementation of an org.apache.http.client.HttpClient.

Parameters:
actualClient
 
 	public HttpRateLimitingClient(HttpClient actualClient) {
 		this. = actualClient;
 	}

Return the actual client being used by this wrapper.

Returns:
the actual org.apache.http.client.HttpClient being used
 
 	public HttpClient getActualClient() {
 		return this.;
 	}

Add rate limiting around a given host name

Parameters:
hostName
limit
timeUnit
 
 	public void addRateLimiting(String hostNameint limitTimeUnit timeUnit) {
 		if(limit <= 0) {
 			throw new IllegalArgumentException("Rate limit cannot be zero/negative");
 		}
 		
 		long divisor;
 		switch(timeUnit) {
			case :
				divisor = 1000;
				break;
			case :
				divisor = 60 * 1000;
				break;
			case :
				divisor = 60 * 60 * 1000;
				break;
			case :
				divisor = 24 * 60 * 60 * 1000;
				break;
			default:
				throw new IllegalArgumentException("Rate limiting not supported at this level.");
		}
			throw new IllegalStateException("To change the rate limit, remove any current limit and then reset");
		}
		.put(hostName.toLowerCase(), new RateLimitValues(divisorlimit));
		this. = true;
	}

Remove rate limiting around this hostname.

Parameters:
hostName
	public void removeRateLimiting(String hostName) {
	}

Clear all previously set rate limited hosting
	public void removeAllRateLimiting() {
		this. = false;
	}

The method checks if the current rate execution rate is within the prescribed limits or not
	private void assertRateInLimit(HttpUriRequest request) {
		if(!this.) {
			return;
		}
	}

The method checks if the current rate execution rate is within the prescribed limits or not
	private void assertRateInLimit(HttpHost target) {
		if(!this.) {
			return;
		}
	}

The method checks if the current rate execution rate is within the prescribed limits or not
	private void assertRateInLimit(String host) {
		if(values == null) {
			return;
		}
		long block = System.currentTimeMillis() / values.interval;
		if(block == values.block) {
			int currentValue = values.current.intValue();
			if(currentValue == values.limit) {
				throw new HttpRateLimitException("Host is at its limit: " + host);
			}
			// we are in limits
			boolean updated = values.current.compareAndSet(currentValue, 1);
			if(!updated) {
				values.current.incrementAndGet();
			}
else {
			// reset the block
			values.block = block;
			values.current.set(1);
		}
	}

	public HttpParams getParams() {
		return this..getParams();
	}

	}

		return this..execute(request);
	}

		return this..execute(requestcontext);
	}

		return this..execute(targetrequest);
	}

	public HttpResponse execute(HttpHost targetHttpRequest requestHttpContext contextthrows IOExceptionClientProtocolException {
		return this..execute(targetrequestcontext);
	}

	public <T> T execute(HttpUriRequest requestResponseHandler<? extends T> responseHandlerthrows IOExceptionClientProtocolException {
		return this..execute(requestresponseHandler);
	}

	public <T> T execute(HttpUriRequest requestResponseHandler<? extends T> responseHandlerHttpContext contextthrows IOExceptionClientProtocolException {
		return this..execute(requestresponseHandlercontext);
	}

	public <T> T execute(HttpHost targetHttpRequest requestResponseHandler<? extends T> responseHandlerthrows IOExceptionClientProtocolException {
		return this..execute(targetrequestresponseHandler);
	}

	public <T> T execute(HttpHost targetHttpRequest requestResponseHandler<? extends T> responseHandlerHttpContext contextthrows IOExceptionClientProtocolException {
		return this..execute(targetrequestresponseHandlercontext);
	}

Stores rate-limiting values

Author(s):
sangupta
	private static class RateLimitValues {
		final long interval;
		final int limit;
		long block;
		public RateLimitValues(long intervalint limit) {
			this. = interval;
			this. = limit;
		}
	}
New to GrepCode? Check out our FAQ X