Explorar o código

Add option keep_alive_timeout_ms

bel %!s(int64=8) %!d(string=hai) anos
pai
achega
edb56cdca5
Modificáronse 2 ficheiros con 26 adicións e 0 borrados
  1. 18 0
      docs/UserManual.md
  2. 8 0
      src/civetweb.c

+ 18 - 0
docs/UserManual.md

@@ -356,6 +356,24 @@ Timeout for network read and network write operations, in milliseconds.
 If a client intends to keep long-running connection, either increase this
 value or (better) use keep-alive messages.
 
+### keep\_alive\_timeout\_ms `500` or `0`
+Idle timeout between two requests in one keep-alive connection.
+If keep alive is enabled, multiple requests using the same connection 
+are possible. This reduces the overhead for opening and closing connections
+when loading several resources from one server, but it also blocks one port
+and one thread at the server during the lifetime of this connection.
+Unfortunately, browsers do not close the keep-alive connection after loading
+all resources required to show a website.
+The server closes a keep-alive connection, if there is no additional request
+from the client during this timeout.
+
+Note: if enable\_keep\_alive is set to `no` the value of 
+keep\_alive\_timeout\_ms should be set to `0`, if enable\_keep\_alive is set 
+to `yes`, the value of keep\_alive\_timeout\_ms must be >0.
+Currently keep\_alive\_timeout\_ms is ignored if enable\_keep\_alive is no,
+but future versions my drop the enable\_keep\_alive configuration value and
+automatically use keep-alive if keep\_alive\_timeout\_ms is not 0.
+
 ### lua\_preload\_file
 This configuration option can be used to specify a Lua script file, which
 is executed before the actual web page script (Lua script, Lua server page

+ 8 - 0
src/civetweb.c

@@ -1409,6 +1409,7 @@ enum {
 	REWRITE,
 	HIDE_FILES,
 	REQUEST_TIMEOUT,
+	KEEP_ALIVE_TIMEOUT,
 	SSL_DO_VERIFY_PEER,
 	SSL_CA_PATH,
 	SSL_CA_FILE,
@@ -1491,6 +1492,7 @@ static struct mg_option config_options[] = {
     {"url_rewrite_patterns", CONFIG_TYPE_STRING, NULL},
     {"hide_files_patterns", CONFIG_TYPE_EXT_PATTERN, NULL},
     {"request_timeout_ms", CONFIG_TYPE_NUMBER, "30000"},
+    {"keep_alive_timeout_ms", CONFIG_TYPE_NUMBER, "500"},
     {"ssl_verify_peer", CONFIG_TYPE_BOOLEAN, "no"},
     {"ssl_ca_path", CONFIG_TYPE_DIRECTORY, NULL},
     {"ssl_ca_file", CONFIG_TYPE_FILE, NULL},
@@ -7713,6 +7715,12 @@ read_request(FILE *fp,
 	} else {
 		request_timeout = -1.0;
 	}
+	if (conn->handled_requests > 0) {
+		if (conn->ctx->config[KEEP_ALIVE_TIMEOUT]) {
+			request_timeout =
+			    atof(conn->ctx->config[KEEP_ALIVE_TIMEOUT]) / 1000.0;
+		}
+	}
 
 	request_len = get_request_len(buf, *nread);