Skip to content

Commit

Permalink
8286918: Better HttpServer service
Browse files Browse the repository at this point in the history
Reviewed-by: dfuchs, michaelm, ahgross, rhalade
  • Loading branch information
jaikiran authored and slowhog committed Oct 18, 2022
1 parent 400aa2f commit 1553551
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 146 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -30,8 +30,6 @@
import java.nio.channels.*;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import com.sun.net.httpserver.*;
import com.sun.net.httpserver.spi.*;

/**
* encapsulates all the connection specific state for a HTTP/S connection
Expand All @@ -55,14 +53,14 @@ class HttpConnection {
SocketChannel chan;
SelectionKey selectionKey;
String protocol;
long time;
volatile long creationTime; // time this connection was created
long idleStartTime; // absolute time in milli seconds, starting when the connection was marked idle
volatile long reqStartedTime; // time when the request was initiated
volatile long rspStartedTime; // time we started writing the response
int remaining;
boolean closed = false;
Logger logger;

public enum State {IDLE, REQUEST, RESPONSE};
public enum State {IDLE, REQUEST, RESPONSE, NEWLY_ACCEPTED};
volatile State state;

public String toString() {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -44,7 +44,6 @@ class SSLStreams {

SSLContext sslctx;
SocketChannel chan;
TimeSource time;
ServerImpl server;
SSLEngine engine;
EngineWrapper wrapper;
Expand All @@ -56,7 +55,6 @@ class SSLStreams {

SSLStreams (ServerImpl server, SSLContext sslctx, SocketChannel chan) throws IOException {
this.server = server;
this.time= (TimeSource)server;
this.sslctx= sslctx;
this.chan= chan;
InetSocketAddress addr =
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -37,29 +37,35 @@
@SuppressWarnings("removal")
class ServerConfig {

private static final int DEFAULT_CLOCK_TICK = 10000 ; // 10 sec.
private static final int DEFAULT_IDLE_TIMER_SCHEDULE_MILLIS = 10000 ; // 10 sec.

/* These values must be a reasonable multiple of clockTick */
private static final long DEFAULT_IDLE_INTERVAL = 30 ; // 5 min
private static final long DEFAULT_IDLE_INTERVAL_IN_SECS = 30;
private static final int DEFAULT_MAX_CONNECTIONS = -1 ; // no limit on maximum connections
private static final int DEFAULT_MAX_IDLE_CONNECTIONS = 200 ;

private static final long DEFAULT_MAX_REQ_TIME = -1; // default: forever
private static final long DEFAULT_MAX_RSP_TIME = -1; // default: forever
private static final long DEFAULT_TIMER_MILLIS = 1000;
// default timer schedule, in milli seconds, for the timer task that's responsible for
// timing out request/response if max request/response time is configured
private static final long DEFAULT_REQ_RSP_TIMER_TASK_SCHEDULE_MILLIS = 1000;
private static final int DEFAULT_MAX_REQ_HEADERS = 200;
private static final long DEFAULT_DRAIN_AMOUNT = 64 * 1024;

private static int clockTick;
private static long idleInterval;
private static long idleTimerScheduleMillis;
private static long idleIntervalMillis;
// The maximum number of bytes to drain from an inputstream
private static long drainAmount;
// the maximum number of connections that the server will allow to be open
// after which it will no longer "accept()" any new connections, till the
// current connection count goes down due to completion of processing the requests
private static int maxConnections;
private static int maxIdleConnections;
// The maximum number of request headers allowable
private static int maxReqHeaders;
// max time a request or response is allowed to take
private static long maxReqTime;
private static long maxRspTime;
private static long timerMillis;
private static long reqRspTimerScheduleMillis;
private static boolean debug;

// the value of the TCP_NODELAY socket-level option
Expand All @@ -70,11 +76,22 @@ class ServerConfig {
new PrivilegedAction<Void>() {
@Override
public Void run () {
idleInterval = Long.getLong("sun.net.httpserver.idleInterval",
DEFAULT_IDLE_INTERVAL) * 1000;
idleIntervalMillis = Long.getLong("sun.net.httpserver.idleInterval",
DEFAULT_IDLE_INTERVAL_IN_SECS) * 1000;
if (idleIntervalMillis <= 0) {
idleIntervalMillis = DEFAULT_IDLE_INTERVAL_IN_SECS * 1000;
}

idleTimerScheduleMillis = Long.getLong("sun.net.httpserver.clockTick",
DEFAULT_IDLE_TIMER_SCHEDULE_MILLIS);
if (idleTimerScheduleMillis <= 0) {
// ignore zero or negative value and use the default schedule
idleTimerScheduleMillis = DEFAULT_IDLE_TIMER_SCHEDULE_MILLIS;
}

clockTick = Integer.getInteger("sun.net.httpserver.clockTick",
DEFAULT_CLOCK_TICK);
maxConnections = Integer.getInteger(
"jdk.httpserver.maxConnections",
DEFAULT_MAX_CONNECTIONS);

maxIdleConnections = Integer.getInteger(
"sun.net.httpserver.maxIdleConnections",
Expand All @@ -93,8 +110,13 @@ public Void run () {
maxRspTime = Long.getLong("sun.net.httpserver.maxRspTime",
DEFAULT_MAX_RSP_TIME);

timerMillis = Long.getLong("sun.net.httpserver.timerMillis",
DEFAULT_TIMER_MILLIS);
reqRspTimerScheduleMillis = Long.getLong("sun.net.httpserver.timerMillis",
DEFAULT_REQ_RSP_TIMER_TASK_SCHEDULE_MILLIS);
if (reqRspTimerScheduleMillis <= 0) {
// ignore any negative or zero value for this configuration and reset
// to default schedule
reqRspTimerScheduleMillis = DEFAULT_REQ_RSP_TIMER_TASK_SCHEDULE_MILLIS;
}

debug = Boolean.getBoolean("sun.net.httpserver.debug");

Expand Down Expand Up @@ -150,14 +172,34 @@ static boolean debugEnabled() {
return debug;
}

static long getIdleInterval() {
return idleInterval;
/**
* {@return Returns the maximum duration, in milli seconds, a connection can be idle}
*/
static long getIdleIntervalMillis() {
return idleIntervalMillis;
}

/**
* {@return Returns the schedule, in milli seconds, for the timer task that is responsible
* for managing the idle connections}
*/
static long getIdleTimerScheduleMillis() {
return idleTimerScheduleMillis;
}

static int getClockTick() {
return clockTick;
/**
* @return Returns the maximum number of connections that can be open at any given time.
* This method can return a value of 0 or negative to represent that the limit hasn't
* been configured.
*/
static int getMaxConnections() {
return maxConnections;
}

/**
* @return Returns the maximum number of connections that can be idle. This method
* can return a value of 0 or negative.
*/
static int getMaxIdleConnections() {
return maxIdleConnections;
}
Expand All @@ -170,16 +212,30 @@ static int getMaxReqHeaders() {
return maxReqHeaders;
}

/**
* @return Returns the maximum amount of time the server will wait for the request to be read
* completely. This method can return a value of 0 or negative to imply no maximum limit has
* been configured.
*/
static long getMaxReqTime() {
return maxReqTime;
}

/**
* @return Returns the maximum amount of time the server will wait for the response to be generated
* for a request that is being processed. This method can return a value of 0 or negative to
* imply no maximum limit has been configured.
*/
static long getMaxRspTime() {
return maxRspTime;
}

static long getTimerMillis() {
return timerMillis;
/**
* {@return Returns the timer schedule of the task that's responsible for timing out
* request/response that have been running longer than any configured timeout}
*/
static long getReqRspTimerScheduleMillis() {
return reqRspTimerScheduleMillis;
}

static boolean noDelay() {
Expand Down

0 comments on commit 1553551

Please sign in to comment.