Skip to content

Commit

Permalink
automatic deletion of dead client connections
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4110 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
orbiter committed Sep 25, 2007
1 parent 49f1c58 commit 3c74014
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion htroot/Connections_p.java
Expand Up @@ -249,7 +249,7 @@ public static serverObjects respond(httpHeader header, serverObjects post, serve
prop.put("clientList_" + c + "_clientProtocol", (clientConnection.ssl) ? "HTTPS" : "HTTP");
prop.put("clientList_" + c + "_clientLifetime", System.currentTimeMillis() - clientConnection.initTime);
prop.put("clientList_" + c + "_clientTargetHost", clientConnection.adressed_host + ":" + clientConnection.adressed_port);
prop.put("clientList_" + c + "_clientCommand", clientConnection.command);
prop.put("clientList_" + c + "_clientCommand", (clientConnection.command == null) ? "-" : clientConnection.command);
prop.put("clientList_" + c + "_clientID", clientConnection.hashCode());
c++;
}
Expand Down
34 changes: 33 additions & 1 deletion source/de/anomic/http/httpc.java
Expand Up @@ -200,6 +200,7 @@ public boolean verify(String urlHostName, javax.net.ssl.SSLSession session) {
public boolean ssl;
public long initTime;
public String command;
public int timeout;

private int hashIndex;

Expand All @@ -222,6 +223,10 @@ public httpc(
String outgoingByteCountAccounting
) throws IOException {

// remove old connections
checkIdleConnections();

// register new connection
this.hashIndex = objCounter;
objCounter++;
synchronized (activeConnections) {activeConnections.add(this);}
Expand All @@ -230,6 +235,7 @@ public httpc(
this.ssl = ssl;
this.initTime = System.currentTimeMillis();
this.command = null;
this.timeout = timeout;

if ((theRemoteProxyConfig == null) ||
(!theRemoteProxyConfig.useProxy())) {
Expand Down Expand Up @@ -380,7 +386,7 @@ private void initN(
}

// trying to establish a connection to the address
this.socket.connect(address,timeout);
this.socket.connect(address, timeout);

// registering the socket
this.socketOwner = this.registerOpenSocket(this.socket);
Expand Down Expand Up @@ -422,6 +428,32 @@ public long getOutputStreamByteCount() {
return (this.clientOutputByteCount == null)?0:this.clientOutputByteCount.getCount();
}

public static int checkIdleConnections() {
// try to find and close all connections that did not find a target server and are idle waiting for a server socket
Iterator i = httpc.activeConnections.iterator();
int c = 0;
while (i.hasNext()) {
httpc clientConnection = (httpc) i.next();
if ((clientConnection.command == null) && (clientConnection.initTime + clientConnection.timeout > System.currentTimeMillis())) {
// the time-out limit is reached. close the connection
clientConnection.close();
c++;
}
}
return c;
}

public static int closeAllConnections() {
Iterator i = httpc.activeConnections.iterator();
int c = 0;
while (i.hasNext()) {
httpc clientConnection = (httpc) i.next();
clientConnection.close();
c++;
}
return c;
}

public void finalize() {
this.close();
}
Expand Down
1 change: 1 addition & 0 deletions source/de/anomic/plasma/plasmaSwitchboard.java
Expand Up @@ -1785,6 +1785,7 @@ public void close() {
parser.close();
plasmaHTCache.close();
sbQueue.close();
httpc.closeAllConnections();
webStructure.flushCitationReference("crg");
webStructure.close();
log.logConfig("SWITCHBOARD SHUTDOWN STEP 3: sending termination signal to database manager (stand by...)");
Expand Down

0 comments on commit 3c74014

Please sign in to comment.