simplified idle duration to start counting when connection is released

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@672969 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sam Berlin 2008-07-01 01:09:50 +00:00
parent ea0e787fde
commit bf6371eae6
3 changed files with 9 additions and 36 deletions

View File

@ -36,7 +36,7 @@ import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
/** /**
* Handler that incapsulates the process of generating a response object * Handler that encapsulates the process of generating a response object
* from a {@link HttpResponse}. * from a {@link HttpResponse}.
* *
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>

View File

@ -249,9 +249,12 @@ public interface ManagedClientConnection extends
; ;
/** /**
* Sets the duration that this connection can remain idle * Sets the duration that this connection can remain idle before it is
* before it is reused. * reused. The connection should not be used again if this time elapses. The
* The connection should not be used again if this time elapses. * idle duration must be reset after each request sent over this connection.
* The elapsed time starts counting when the connection is released, which
* is typically after the headers (and any response body, if present) is
* fully consumed.
*/ */
void setIdleDuration(long duration, TimeUnit unit); void setIdleDuration(long duration, TimeUnit unit);

View File

@ -99,9 +99,6 @@ public abstract class AbstractClientConnAdapter
/** True if the connection has been aborted. */ /** True if the connection has been aborted. */
private volatile boolean aborted; private volatile boolean aborted;
/** The last time this processed incoming headers. */
private volatile long lastHeadersRead;
/** The duration this is valid for while idle (in ms). */ /** The duration this is valid for while idle (in ms). */
private volatile long duration; private volatile long duration;
@ -259,8 +256,6 @@ public abstract class AbstractClientConnAdapter
assertValid(conn); assertValid(conn);
unmarkReusable(); unmarkReusable();
duration = Long.MAX_VALUE; // Reset duration per request.
lastHeadersRead = System.currentTimeMillis();
return conn.receiveResponseHeader(); return conn.receiveResponseHeader();
} }
@ -361,39 +356,14 @@ public abstract class AbstractClientConnAdapter
if(duration > 0) { if(duration > 0) {
this.duration = unit.toMillis(duration); this.duration = unit.toMillis(duration);
} else { } else {
this.duration = Long.MAX_VALUE; this.duration = -1;
}
}
/**
* Returns the amount of time remaining that this connection
* can be reused. If negative, the connection should not
* be reused. If Long.MAX_VALUE, there is no suggested
* duration.
*
* The remaining time is the elapsed time between now,
* the time the last headers were processed, and the duration
* given from {@link #setIdleDuration(long, TimeUnit)}.
*/
protected long getRemainingIdleDuration() {
if(duration == Long.MAX_VALUE) {
return Long.MAX_VALUE;
} else {
long elapsedAlready = System.currentTimeMillis() - lastHeadersRead;
return duration - elapsedAlready;
} }
} }
// non-javadoc, see interface ConnectionReleaseTrigger // non-javadoc, see interface ConnectionReleaseTrigger
public void releaseConnection() { public void releaseConnection() {
if (connManager != null) { if (connManager != null) {
long remainingTime = getRemainingIdleDuration(); connManager.releaseConnection(this, duration, TimeUnit.MILLISECONDS);
if(remainingTime <= 0) {
unmarkReusable();
} else if(remainingTime == Long.MAX_VALUE) {
remainingTime = -1;
}
connManager.releaseConnection(this, remainingTime, TimeUnit.MILLISECONDS);
} }
} }