HTTPCLIENT-824: Use only one immutable copy of HTTP protocol processor instead creating a new copy for every request execution

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@825464 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2009-10-15 11:14:40 +00:00
parent 01b7d5dc40
commit 0b815c2505
1 changed files with 68 additions and 35 deletions

View File

@ -66,6 +66,7 @@ import org.apache.http.protocol.DefaultedHttpContext;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpProcessor; import org.apache.http.protocol.HttpProcessor;
import org.apache.http.protocol.HttpRequestExecutor; import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.protocol.ImmutableHttpProcessor;
/** /**
* Base class for {@link HttpClient} implementations. This class acts as * Base class for {@link HttpClient} implementations. This class acts as
@ -186,9 +187,12 @@ public abstract class AbstractHttpClient implements HttpClient {
@GuardedBy("this") @GuardedBy("this")
private AuthSchemeRegistry supportedAuthSchemes; private AuthSchemeRegistry supportedAuthSchemes;
/** The HTTP processor. */ /** The HTTP protocol processor and its immutable copy. */
@GuardedBy("this") @GuardedBy("this")
private BasicHttpProcessor httpProcessor; private BasicHttpProcessor mutableProcessor;
@GuardedBy("this")
private ImmutableHttpProcessor protocolProcessor;
/** The request retry handler. */ /** The request retry handler. */
@GuardedBy("this") @GuardedBy("this")
@ -482,25 +486,31 @@ public abstract class AbstractHttpClient implements HttpClient {
protected synchronized final BasicHttpProcessor getHttpProcessor() { protected synchronized final BasicHttpProcessor getHttpProcessor() {
if (httpProcessor == null) { if (mutableProcessor == null) {
httpProcessor = createHttpProcessor(); mutableProcessor = createHttpProcessor();
} }
return httpProcessor; return mutableProcessor;
} }
public synchronized void addResponseInterceptor(final HttpResponseInterceptor itcp) { private synchronized final HttpProcessor getProtocolProcessor() {
getHttpProcessor().addInterceptor(itcp); if (protocolProcessor == null) {
// Get mutable HTTP processor
BasicHttpProcessor proc = getHttpProcessor();
// and create an immutable copy of it
int reqc = proc.getRequestInterceptorCount();
HttpRequestInterceptor[] reqinterceptors = new HttpRequestInterceptor[reqc];
for (int i = 0; i < reqc; i++) {
reqinterceptors[i] = proc.getRequestInterceptor(i);
} }
int resc = proc.getResponseInterceptorCount();
HttpResponseInterceptor[] resinterceptors = new HttpResponseInterceptor[resc];
public synchronized void addResponseInterceptor(final HttpResponseInterceptor itcp, int index) { for (int i = 0; i < resc; i++) {
getHttpProcessor().addInterceptor(itcp, index); resinterceptors[i] = proc.getResponseInterceptor(i);
} }
protocolProcessor = new ImmutableHttpProcessor(reqinterceptors, resinterceptors);
}
public synchronized HttpResponseInterceptor getResponseInterceptor(int index) { return protocolProcessor;
return getHttpProcessor().getResponseInterceptor(index);
} }
@ -509,23 +519,8 @@ public abstract class AbstractHttpClient implements HttpClient {
} }
public synchronized void clearResponseInterceptors() { public synchronized HttpResponseInterceptor getResponseInterceptor(int index) {
getHttpProcessor().clearResponseInterceptors(); return getHttpProcessor().getResponseInterceptor(index);
}
public synchronized void removeResponseInterceptorByClass(Class<? extends HttpResponseInterceptor> clazz) {
getHttpProcessor().removeResponseInterceptorByClass(clazz);
}
public synchronized void addRequestInterceptor(final HttpRequestInterceptor itcp) {
getHttpProcessor().addInterceptor(itcp);
}
public synchronized void addRequestInterceptor(final HttpRequestInterceptor itcp, int index) {
getHttpProcessor().addInterceptor(itcp, index);
} }
@ -539,13 +534,51 @@ public abstract class AbstractHttpClient implements HttpClient {
} }
public synchronized void addResponseInterceptor(final HttpResponseInterceptor itcp) {
getHttpProcessor().addInterceptor(itcp);
protocolProcessor = null;
}
public synchronized void addResponseInterceptor(final HttpResponseInterceptor itcp, int index) {
getHttpProcessor().addInterceptor(itcp, index);
protocolProcessor = null;
}
public synchronized void clearResponseInterceptors() {
getHttpProcessor().clearResponseInterceptors();
protocolProcessor = null;
}
public synchronized void removeResponseInterceptorByClass(Class<? extends HttpResponseInterceptor> clazz) {
getHttpProcessor().removeResponseInterceptorByClass(clazz);
protocolProcessor = null;
}
public synchronized void addRequestInterceptor(final HttpRequestInterceptor itcp) {
getHttpProcessor().addInterceptor(itcp);
protocolProcessor = null;
}
public synchronized void addRequestInterceptor(final HttpRequestInterceptor itcp, int index) {
getHttpProcessor().addInterceptor(itcp, index);
protocolProcessor = null;
}
public synchronized void clearRequestInterceptors() { public synchronized void clearRequestInterceptors() {
getHttpProcessor().clearRequestInterceptors(); getHttpProcessor().clearRequestInterceptors();
protocolProcessor = null;
} }
public synchronized void removeRequestInterceptorByClass(Class<? extends HttpRequestInterceptor> clazz) { public synchronized void removeRequestInterceptorByClass(Class<? extends HttpRequestInterceptor> clazz) {
getHttpProcessor().removeRequestInterceptorByClass(clazz); getHttpProcessor().removeRequestInterceptorByClass(clazz);
protocolProcessor = null;
} }
public final HttpResponse execute(HttpUriRequest request) public final HttpResponse execute(HttpUriRequest request)
@ -628,7 +661,7 @@ public abstract class AbstractHttpClient implements HttpClient {
getConnectionReuseStrategy(), getConnectionReuseStrategy(),
getConnectionKeepAliveStrategy(), getConnectionKeepAliveStrategy(),
getRoutePlanner(), getRoutePlanner(),
getHttpProcessor().copy(), getProtocolProcessor(),
getHttpRequestRetryHandler(), getHttpRequestRetryHandler(),
getRedirectHandler(), getRedirectHandler(),
getTargetAuthenticationHandler(), getTargetAuthenticationHandler(),