7068416: Lightweight HTTP Server should support TCP_NODELAY
authorchegar
Thu, 21 Jul 2011 17:28:10 +0100
changeset 10130 254e206a89d8
parent 10129 bf705e446ca1
child 10131 c2ab591608f0
7068416: Lightweight HTTP Server should support TCP_NODELAY Reviewed-by: alanb, michaelm
jdk/src/share/classes/sun/net/httpserver/ServerConfig.java
jdk/src/share/classes/sun/net/httpserver/ServerImpl.java
jdk/test/com/sun/net/httpserver/Test1.java
--- a/jdk/src/share/classes/sun/net/httpserver/ServerConfig.java	Wed Jul 20 12:19:41 2011 -0700
+++ b/jdk/src/share/classes/sun/net/httpserver/ServerConfig.java	Thu Jul 21 17:28:10 2011 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2011, 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
@@ -25,8 +25,6 @@
 
 package sun.net.httpserver;
 
-import com.sun.net.httpserver.*;
-import com.sun.net.httpserver.spi.*;
 import java.util.logging.Logger;
 import java.security.PrivilegedAction;
 
@@ -59,48 +57,46 @@
     static long maxReqTime;
     static long maxRspTime;
     static long timerMillis;
-    static boolean debug = false;
+    static boolean debug;
+
+    // the value of the TCP_NODELAY socket-level option
+    static boolean noDelay;
 
     static {
-
-        idleInterval = ((Long)java.security.AccessController.doPrivileged(
-                new sun.security.action.GetLongAction(
-                "sun.net.httpserver.idleInterval",
-                DEFAULT_IDLE_INTERVAL))).longValue() * 1000;
+        java.security.AccessController.doPrivileged(
+            new PrivilegedAction<Void>() {
+                @Override
+                public Void run () {
+                    idleInterval = Long.getLong("sun.net.httpserver.idleInterval",
+                            DEFAULT_IDLE_INTERVAL) * 1000;
 
-        clockTick = ((Integer)java.security.AccessController.doPrivileged(
-                new sun.security.action.GetIntegerAction(
-                "sun.net.httpserver.clockTick",
-                DEFAULT_CLOCK_TICK))).intValue();
+                    clockTick = Integer.getInteger("sun.net.httpserver.clockTick",
+                            DEFAULT_CLOCK_TICK);
 
-        maxIdleConnections = ((Integer)java.security.AccessController.doPrivileged(
-                new sun.security.action.GetIntegerAction(
-                "sun.net.httpserver.maxIdleConnections",
-                DEFAULT_MAX_IDLE_CONNECTIONS))).intValue();
+                    maxIdleConnections = Integer.getInteger(
+                            "sun.net.httpserver.maxIdleConnections",
+                            DEFAULT_MAX_IDLE_CONNECTIONS);
+
+                    drainAmount = Long.getLong("sun.net.httpserver.drainAmount",
+                            DEFAULT_DRAIN_AMOUNT);
 
-        drainAmount = ((Long)java.security.AccessController.doPrivileged(
-                new sun.security.action.GetLongAction(
-                "sun.net.httpserver.drainAmount",
-                DEFAULT_DRAIN_AMOUNT))).longValue();
+                    maxReqTime = Long.getLong("sun.net.httpserver.maxReqTime",
+                            DEFAULT_MAX_REQ_TIME);
 
-        maxReqTime = ((Long)java.security.AccessController.doPrivileged(
-                new sun.security.action.GetLongAction(
-                "sun.net.httpserver.maxReqTime",
-                DEFAULT_MAX_REQ_TIME))).longValue();
+                    maxRspTime = Long.getLong("sun.net.httpserver.maxRspTime",
+                            DEFAULT_MAX_RSP_TIME);
+
+                    timerMillis = Long.getLong("sun.net.httpserver.timerMillis",
+                            DEFAULT_TIMER_MILLIS);
 
-        maxRspTime = ((Long)java.security.AccessController.doPrivileged(
-                new sun.security.action.GetLongAction(
-                "sun.net.httpserver.maxRspTime",
-                DEFAULT_MAX_RSP_TIME))).longValue();
+                    debug = Boolean.getBoolean("sun.net.httpserver.debug");
+
+                    noDelay = Boolean.getBoolean("sun.net.httpserver.nodelay");
 
-        timerMillis = ((Long)java.security.AccessController.doPrivileged(
-                new sun.security.action.GetLongAction(
-                "sun.net.httpserver.timerMillis",
-                DEFAULT_TIMER_MILLIS))).longValue();
+                    return null;
+                }
+            });
 
-        debug = ((Boolean)java.security.AccessController.doPrivileged(
-                new sun.security.action.GetBooleanAction(
-                "sun.net.httpserver.debug"))).booleanValue();
     }
 
 
@@ -172,4 +168,8 @@
     static long getTimerMillis () {
         return timerMillis;
     }
+
+    static boolean noDelay() {
+        return noDelay;
+    }
 }
--- a/jdk/src/share/classes/sun/net/httpserver/ServerImpl.java	Wed Jul 20 12:19:41 2011 -0700
+++ b/jdk/src/share/classes/sun/net/httpserver/ServerImpl.java	Thu Jul 21 17:28:10 2011 +0100
@@ -27,8 +27,6 @@
 
 import java.net.*;
 import java.io.*;
-import java.nio.*;
-import java.security.*;
 import java.nio.channels.*;
 import java.util.*;
 import java.util.concurrent.*;
@@ -36,7 +34,6 @@
 import java.util.logging.Level;
 import javax.net.ssl.*;
 import com.sun.net.httpserver.*;
-import com.sun.net.httpserver.spi.*;
 import sun.net.httpserver.HttpConnection.State;
 
 /**
@@ -358,6 +355,12 @@
                                 continue;
                             }
                             SocketChannel chan = schan.accept();
+
+                            // Set TCP_NODELAY, if appropriate
+                            if (ServerConfig.noDelay()) {
+                                chan.socket().setTcpNoDelay(true);
+                            }
+
                             if (chan == null) {
                                 continue; /* cancel something ? */
                             }
--- a/jdk/test/com/sun/net/httpserver/Test1.java	Wed Jul 20 12:19:41 2011 -0700
+++ b/jdk/test/com/sun/net/httpserver/Test1.java	Thu Jul 21 17:28:10 2011 +0100
@@ -26,6 +26,7 @@
  * @bug 6270015
  * @run main/othervm Test1
  * @run main/othervm -Dsun.net.httpserver.maxReqTime=10 Test1
+ * @run main/othervm -Dsun.net.httpserver.nodelay=true Test1
  * @summary  Light weight HTTP server
  */
 
@@ -42,6 +43,10 @@
  *      - send/receive large/small file
  *      - chunked encoding
  *      - via http and https
+ *
+ * The test is also run with sun.net.httpserver.nodelay simply to exercise
+ * this option. There is no specific pass or failure related to running with
+ * this option.
  */
 
 public class Test1 extends Test {