浏览代码

Merge pull request #776 from chardan/jfw-wip-775-SOMAXCONN-configurable

make SOMAXCONN configurable (fix 775)
bel2125 6 年之前
父节点
当前提交
3453520a0e
共有 3 个文件被更改,包括 14 次插入8 次删除
  1. 1 0
      resources/civetweb.conf
  2. 12 8
      src/civetweb.c
  3. 1 0
      unittest/private.c

+ 1 - 0
resources/civetweb.conf

@@ -30,3 +30,4 @@ listening_ports 8080
 # url_rewrite_patterns 
 # hide_files_patterns 
 # request_timeout_ms 30000
+# max_connections 100

+ 12 - 8
src/civetweb.c

@@ -882,13 +882,6 @@ typedef int SOCKET;
 
 #endif /* defined(_WIN32) - WINDOWS vs UNIX include block */
 
-/* Maximum queue length for pending connections. This value is passed as
- * parameter to the "listen" socket call. */
-#if !defined(SOMAXCONN)
-/* This symbol may be defined in winsock2.h so this must after that include */
-#define SOMAXCONN (100) /* in pending connections (count) */
-#endif
-
 /* In case our C library is missing "timegm", provide an implementation */
 #if defined(NEED_TIMEGM)
 static inline int
@@ -2364,6 +2357,7 @@ enum {
 	                     * socket option typedef TCP_NODELAY. */
 	MAX_REQUEST_SIZE,
 	LINGER_TIMEOUT,
+	MAX_CONNECTIONS, 
 #if defined(__linux__)
 	ALLOW_SENDFILE_CALL,
 #endif
@@ -2468,6 +2462,7 @@ static const struct mg_option config_options[] = {
     {"tcp_nodelay", MG_CONFIG_TYPE_NUMBER, "0"},
     {"max_request_size", MG_CONFIG_TYPE_NUMBER, "16384"},
     {"linger_timeout_ms", MG_CONFIG_TYPE_NUMBER, NULL},
+    {"max_connections", MG_CONFIG_TYPE_NUMBER, "100"}, 
 #if defined(__linux__)
     {"allow_sendfile_call", MG_CONFIG_TYPE_BOOLEAN, "yes"},
 #endif
@@ -14739,7 +14734,16 @@ set_ports_option(struct mg_context *phys_ctx)
 			continue;
 		}
 
-		if (listen(so.sock, SOMAXCONN) != 0) {
+		const char* const p = phys_ctx->dd.config[MAX_CONNECTIONS];
+		const long opt_max_connections = strtol(p, NULL, 10);
+		if(opt_max_connections > INT_MAX || opt_max_connections < 1) {
+			mg_cry_ctx_internal(phys_ctx, 
+					    "max_connections value \"%s\" is invalid", 
+					    p);
+			continue;
+		}
+
+		if (listen(so.sock, (int)opt_max_connections) != 0) {
 
 			mg_cry_ctx_internal(phys_ctx,
 			                    "cannot listen to %.*s: %d (%s)",

+ 1 - 0
unittest/private.c

@@ -988,6 +988,7 @@ START_TEST(test_config_options)
 	ck_assert_str_eq("keep_alive_timeout_ms",
 	                 config_options[KEEP_ALIVE_TIMEOUT].name);
 	ck_assert_str_eq("linger_timeout_ms", config_options[LINGER_TIMEOUT].name);
+	ck_assert_str_eq("max_connections", config_options[MAX_CONNECTIONS].name);
 	ck_assert_str_eq("ssl_verify_peer",
 	                 config_options[SSL_DO_VERIFY_PEER].name);
 	ck_assert_str_eq("ssl_ca_path", config_options[SSL_CA_PATH].name);