commit 6e07994a8bfa4227444ad5b178b4a54cb4ec69da
parent 9fe0e9d41112f2a75ee4d9461f1b93b59131029d
Author: Christian Grothoff <christian@grothoff.org>
Date: Fri, 5 Jul 2013 10:12:10 +0000
-fixing #2899: allow clients to customize MHD_BUF_INC_SIZE via option
Diffstat:
7 files changed, 45 insertions(+), 15 deletions(-)
diff --git a/AUTHORS b/AUTHORS
@@ -22,6 +22,7 @@ Matthew Moore
Colin Caughie <c.caughie@indigovision.com>
David Carvalho <andaris@gmail.com>
David Reiss <dreiss@facebook.com>
+Matt Holiday
Mika Raento <mikie@iki.fi>
Mike Crowe <mac@mcrowe.com>
John Muth <muth@parascale.com>
diff --git a/ChangeLog b/ChangeLog
@@ -1,3 +1,8 @@
+Fri Jul 5 12:05:01 CEST 2013
+ Added MHD_OPTION_CONNECTION_MEMORY_INCREMENT to allow users
+ to specify a custom value for incrementing read buffer
+ sizes (#2899). -MH
+
Fri Jun 28 14:05:15 CEST 2013
If we shutdown connection for reading on POST due to error,
really do not process further requests even if we already
diff --git a/doc/libmicrohttpd.texi b/doc/libmicrohttpd.texi
@@ -509,6 +509,15 @@ result in much benefit, as half of the memory will be typically used
for IO, and TCP buffers are unlikely to support window sizes above 64k
on most systems.
+@item MHD_OPTION_CONNECTION_MEMORY_INCREMENT
+@cindex memory
+Increment to use for growing the read buffer (followed by a
+@code{size_t}). The default is 1024 (bytes). Increasing this value
+will make MHD use memory for reading more aggressively, which can
+reduce the number of @code{recvfrom} calls but may increase the number
+of @code{sendto} calls. The given value must fit within
+MHD_OPTION_CONNECTION_MEMORY_LIMIT.
+
@item MHD_OPTION_CONNECTION_LIMIT
@cindex connection, limiting number of connections
Maximum number of concurrent connections to accept (followed by an
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h
@@ -673,7 +673,13 @@ enum MHD_OPTION
* HTTPS daemon for client authentification.
* This option should be followed by a "const char*" argument.
*/
- MHD_OPTION_HTTPS_MEM_TRUST = 20
+ MHD_OPTION_HTTPS_MEM_TRUST = 20,
+
+ /**
+ * Increment to use for growing the read buffer (followed by a
+ * size_t). Must fit within MHD_OPTION_CONNECTION_MEMORY_LIMIT.
+ */
+ MHD_OPTION_CONNECTION_MEMORY_INCREMENT = 21
};
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
@@ -37,14 +37,6 @@
#include <netinet/tcp.h>
#endif
-/**
- * Minimum size by which MHD tries to increment read/write buffers.
- * We usually begin with half the available pool space for the
- * IO-buffer, but if absolutely needed we additively grow by the
- * number of bytes given here (up to -- theoretically -- the full pool
- * space).
- */
-#define MHD_BUF_INC_SIZE 1024
/**
* Message to transmit when http 1.1 request is received
@@ -1861,7 +1853,7 @@ MHD_connection_handle_read (struct MHD_Connection *connection)
return MHD_YES;
/* make sure "read" has a reasonable number of bytes
in buffer to use per system call (if possible) */
- if (connection->read_buffer_offset + MHD_BUF_INC_SIZE >
+ if (connection->read_buffer_offset + connection->daemon->pool_increment >
connection->read_buffer_size)
try_grow_read_buffer (connection);
if (MHD_NO == do_read (connection))
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
@@ -1305,8 +1305,6 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
struct sockaddr *addr = (struct sockaddr *) &addrstorage;
socklen_t addrlen;
int s;
- int flags;
- int need_fcntl;
int fd;
addrlen = sizeof (addrstorage);
@@ -1315,10 +1313,8 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
return MHD_NO;
#if HAVE_ACCEPT4
s = accept4 (fd, addr, &addrlen, SOCK_CLOEXEC);
- need_fcntl = MHD_NO;
#else
s = ACCEPT (fd, addr, &addrlen);
- need_fcntl = MHD_YES;
#endif
if ((-1 == s) || (addrlen <= 0))
{
@@ -1339,7 +1335,6 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
return MHD_NO;
}
#if !HAVE_ACCEPT4
- if (MHD_YES == need_fcntl)
{
/* make socket non-inheritable */
#ifdef WINDOWS
@@ -1356,6 +1351,8 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
#endif
}
#else
+ int flags;
+
flags = fcntl (s, F_GETFD);
if ( ( (-1 == flags) ||
( (flags != (flags | FD_CLOEXEC)) &&
@@ -2377,6 +2374,9 @@ parse_options_va (struct MHD_Daemon *daemon,
case MHD_OPTION_CONNECTION_MEMORY_LIMIT:
daemon->pool_size = va_arg (ap, size_t);
break;
+ case MHD_OPTION_CONNECTION_MEMORY_INCREMENT:
+ daemon->pool_increment= va_arg (ap, size_t);
+ break;
case MHD_OPTION_CONNECTION_LIMIT:
daemon->max_connections = va_arg (ap, unsigned int);
break;
@@ -2500,6 +2500,7 @@ parse_options_va (struct MHD_Daemon *daemon,
{
/* all options taking 'size_t' */
case MHD_OPTION_CONNECTION_MEMORY_LIMIT:
+ case MHD_OPTION_CONNECTION_MEMORY_INCREMENT:
case MHD_OPTION_THREAD_STACK_SIZE:
if (MHD_YES != parse_options (daemon,
servaddr,
@@ -2786,6 +2787,7 @@ MHD_start_daemon_va (unsigned int flags,
daemon->default_handler_cls = dh_cls;
daemon->max_connections = MHD_MAX_CONNECTIONS_DEFAULT;
daemon->pool_size = MHD_POOL_SIZE_DEFAULT;
+ daemon->pool_increment = MHD_BUF_INC_SIZE;
daemon->unescape_callback = &MHD_http_unescape;
daemon->connection_timeout = 0; /* no timeout */
daemon->wpipe[0] = -1;
diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h
@@ -48,6 +48,16 @@
/**
+ * Minimum size by which MHD tries to increment read/write buffers.
+ * We usually begin with half the available pool space for the
+ * IO-buffer, but if absolutely needed we additively grow by the
+ * number of bytes given here (up to -- theoretically -- the full pool
+ * space).
+ */
+#define MHD_BUF_INC_SIZE 1024
+
+
+/**
* Handler for fatal errors.
*/
extern MHD_PanicCallback mhd_panic;
@@ -1002,6 +1012,11 @@ struct MHD_Daemon
size_t pool_size;
/**
+ * Increment for growth of the per-connection memory pools.
+ */
+ size_t pool_increment;
+
+ /**
* Size of threads created by MHD.
*/
size_t thread_stack_size;