Skip to content

Commit 12c1c3f

Browse files
martinuyRealCLanger
authored andcommitted
8286918: Better HttpServer service
Reviewed-by: andrew Backport-of: 5a9dc12e012f69b9c32788dec5b6ecdb065f791f
1 parent ae66dce commit 12c1c3f

File tree

6 files changed

+305
-145
lines changed

6 files changed

+305
-145
lines changed

src/jdk.httpserver/share/classes/sun/net/httpserver/HttpConnection.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,8 +30,6 @@
3030
import java.nio.channels.*;
3131
import java.lang.System.Logger;
3232
import java.lang.System.Logger.Level;
33-
import com.sun.net.httpserver.*;
34-
import com.sun.net.httpserver.spi.*;
3533

3634
/**
3735
* encapsulates all the connection specific state for a HTTP/S connection
@@ -55,14 +53,14 @@ class HttpConnection {
5553
SocketChannel chan;
5654
SelectionKey selectionKey;
5755
String protocol;
58-
long time;
59-
volatile long creationTime; // time this connection was created
56+
long idleStartTime; // absolute time in milli seconds, starting when the connection was marked idle
57+
volatile long reqStartedTime; // time when the request was initiated
6058
volatile long rspStartedTime; // time we started writing the response
6159
int remaining;
6260
boolean closed = false;
6361
Logger logger;
6462

65-
public enum State {IDLE, REQUEST, RESPONSE};
63+
public enum State {IDLE, REQUEST, RESPONSE, NEWLY_ACCEPTED};
6664
volatile State state;
6765

6866
public String toString() {

src/jdk.httpserver/share/classes/sun/net/httpserver/SSLStreams.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -44,7 +44,6 @@ class SSLStreams {
4444

4545
SSLContext sslctx;
4646
SocketChannel chan;
47-
TimeSource time;
4847
ServerImpl server;
4948
SSLEngine engine;
5049
EngineWrapper wrapper;
@@ -56,7 +55,6 @@ class SSLStreams {
5655

5756
SSLStreams (ServerImpl server, SSLContext sslctx, SocketChannel chan) throws IOException {
5857
this.server = server;
59-
this.time= (TimeSource)server;
6058
this.sslctx= sslctx;
6159
this.chan= chan;
6260
InetSocketAddress addr =

src/jdk.httpserver/share/classes/sun/net/httpserver/ServerConfig.java

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -37,29 +37,35 @@
3737
@SuppressWarnings("removal")
3838
class ServerConfig {
3939

40-
private static final int DEFAULT_CLOCK_TICK = 10000 ; // 10 sec.
40+
private static final int DEFAULT_IDLE_TIMER_SCHEDULE_MILLIS = 10000 ; // 10 sec.
4141

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

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

52-
private static int clockTick;
53-
private static long idleInterval;
54+
private static long idleTimerScheduleMillis;
55+
private static long idleIntervalMillis;
5456
// The maximum number of bytes to drain from an inputstream
5557
private static long drainAmount;
58+
// the maximum number of connections that the server will allow to be open
59+
// after which it will no longer "accept()" any new connections, till the
60+
// current connection count goes down due to completion of processing the requests
61+
private static int maxConnections;
5662
private static int maxIdleConnections;
5763
// The maximum number of request headers allowable
5864
private static int maxReqHeaders;
5965
// max time a request or response is allowed to take
6066
private static long maxReqTime;
6167
private static long maxRspTime;
62-
private static long timerMillis;
68+
private static long reqRspTimerScheduleMillis;
6369
private static boolean debug;
6470

6571
// the value of the TCP_NODELAY socket-level option
@@ -70,11 +76,22 @@ class ServerConfig {
7076
new PrivilegedAction<Void>() {
7177
@Override
7278
public Void run () {
73-
idleInterval = Long.getLong("sun.net.httpserver.idleInterval",
74-
DEFAULT_IDLE_INTERVAL) * 1000;
79+
idleIntervalMillis = Long.getLong("sun.net.httpserver.idleInterval",
80+
DEFAULT_IDLE_INTERVAL_IN_SECS) * 1000;
81+
if (idleIntervalMillis <= 0) {
82+
idleIntervalMillis = DEFAULT_IDLE_INTERVAL_IN_SECS * 1000;
83+
}
84+
85+
idleTimerScheduleMillis = Long.getLong("sun.net.httpserver.clockTick",
86+
DEFAULT_IDLE_TIMER_SCHEDULE_MILLIS);
87+
if (idleTimerScheduleMillis <= 0) {
88+
// ignore zero or negative value and use the default schedule
89+
idleTimerScheduleMillis = DEFAULT_IDLE_TIMER_SCHEDULE_MILLIS;
90+
}
7591

76-
clockTick = Integer.getInteger("sun.net.httpserver.clockTick",
77-
DEFAULT_CLOCK_TICK);
92+
maxConnections = Integer.getInteger(
93+
"jdk.httpserver.maxConnections",
94+
DEFAULT_MAX_CONNECTIONS);
7895

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

96-
timerMillis = Long.getLong("sun.net.httpserver.timerMillis",
97-
DEFAULT_TIMER_MILLIS);
113+
reqRspTimerScheduleMillis = Long.getLong("sun.net.httpserver.timerMillis",
114+
DEFAULT_REQ_RSP_TIMER_TASK_SCHEDULE_MILLIS);
115+
if (reqRspTimerScheduleMillis <= 0) {
116+
// ignore any negative or zero value for this configuration and reset
117+
// to default schedule
118+
reqRspTimerScheduleMillis = DEFAULT_REQ_RSP_TIMER_TASK_SCHEDULE_MILLIS;
119+
}
98120

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

@@ -150,14 +172,34 @@ static boolean debugEnabled() {
150172
return debug;
151173
}
152174

153-
static long getIdleInterval() {
154-
return idleInterval;
175+
/**
176+
* {@return Returns the maximum duration, in milli seconds, a connection can be idle}
177+
*/
178+
static long getIdleIntervalMillis() {
179+
return idleIntervalMillis;
180+
}
181+
182+
/**
183+
* {@return Returns the schedule, in milli seconds, for the timer task that is responsible
184+
* for managing the idle connections}
185+
*/
186+
static long getIdleTimerScheduleMillis() {
187+
return idleTimerScheduleMillis;
155188
}
156189

157-
static int getClockTick() {
158-
return clockTick;
190+
/**
191+
* @return Returns the maximum number of connections that can be open at any given time.
192+
* This method can return a value of 0 or negative to represent that the limit hasn't
193+
* been configured.
194+
*/
195+
static int getMaxConnections() {
196+
return maxConnections;
159197
}
160198

199+
/**
200+
* @return Returns the maximum number of connections that can be idle. This method
201+
* can return a value of 0 or negative.
202+
*/
161203
static int getMaxIdleConnections() {
162204
return maxIdleConnections;
163205
}
@@ -170,16 +212,30 @@ static int getMaxReqHeaders() {
170212
return maxReqHeaders;
171213
}
172214

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

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

181-
static long getTimerMillis() {
182-
return timerMillis;
233+
/**
234+
* {@return Returns the timer schedule of the task that's responsible for timing out
235+
* request/response that have been running longer than any configured timeout}
236+
*/
237+
static long getReqRspTimerScheduleMillis() {
238+
return reqRspTimerScheduleMillis;
183239
}
184240

185241
static boolean noDelay() {

0 commit comments

Comments
 (0)