Skip to content

Commit

Permalink
Backport 47624f6fc699aa66c58587460ce7f39fce5a86c7
Browse files Browse the repository at this point in the history
  • Loading branch information
duke committed Jul 16, 2024
1 parent a1e7701 commit 036efa2
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions src/jdk.httpserver/share/classes/sun/net/httpserver/ServerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Timer;
Expand Down Expand Up @@ -516,14 +515,15 @@ public void run() {

key.cancel();
chan.configureBlocking (true);
// check if connection is being closed
if (newlyAcceptedConnections.remove(conn)
|| idleConnections.remove(conn)) {
// was either a newly accepted connection or an idle
// connection. In either case, we mark that the request
// has now started on this connection.
requestStarted(conn);
handle (chan, conn);
}
handle (chan, conn);
} else {
assert false : "Unexpected non-readable key:" + key;
}
Expand Down Expand Up @@ -984,35 +984,30 @@ void responseCompleted (HttpConnection c) {
*/
class IdleTimeoutTask extends TimerTask {
public void run () {
ArrayList<HttpConnection> toClose = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
synchronized (idleConnections) {
final Iterator<HttpConnection> it = idleConnections.iterator();
while (it.hasNext()) {
final HttpConnection c = it.next();
if (currentTime - c.idleStartTime >= IDLE_INTERVAL) {
toClose.add(c);
it.remove();
}
}
}
closeConnections(idleConnections, IDLE_INTERVAL);
// if any newly accepted connection has been idle (i.e. no byte has been sent on that
// connection during the configured idle timeout period) then close it as well
synchronized (newlyAcceptedConnections) {
final Iterator<HttpConnection> it = newlyAcceptedConnections.iterator();
while (it.hasNext()) {
final HttpConnection c = it.next();
if (currentTime - c.idleStartTime >= NEWLY_ACCEPTED_CONN_IDLE_INTERVAL) {
toClose.add(c);
it.remove();
}
closeConnections(newlyAcceptedConnections, NEWLY_ACCEPTED_CONN_IDLE_INTERVAL);
}

private void closeConnections(Set<HttpConnection> connections, long idleInterval) {
long currentTime = System.currentTimeMillis();
ArrayList<HttpConnection> toClose = new ArrayList<>();

connections.forEach(c -> {
if (currentTime - c.idleStartTime >= idleInterval) {
toClose.add(c);
}
}
});
for (HttpConnection c : toClose) {
allConnections.remove(c);
c.close();
if (logger.isLoggable(Level.TRACE)) {
logger.log(Level.TRACE, "Closed idle connection " + c);
// check if connection still idle
if (currentTime - c.idleStartTime >= idleInterval &&
connections.remove(c)) {
allConnections.remove(c);
c.close();
if (logger.isLoggable(Level.TRACE)) {
logger.log(Level.TRACE, "Closed idle connection " + c);
}
}
}
}
Expand Down

0 comments on commit 036efa2

Please sign in to comment.