aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2017-10-01 16:21:06 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2017-10-01 16:21:06 +0300
commit5ac497e212525a12209a3c1007a129ae85055cf7 (patch)
treeb5ab68e70ca971dd9838b74fec70e79e5d0e5832
parent6c49371c2b1e31abef4251bf4dbc514d7da53bdd (diff)
downloadlibmicrohttpd-5ac497e212525a12209a3c1007a129ae85055cf7.tar.gz
libmicrohttpd-5ac497e212525a12209a3c1007a129ae85055cf7.zip
Sending with sendfile: use chunks so MHD will be able to process other connections too.
-rw-r--r--src/microhttpd/connection.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index daa49824..d80eabc5 100644
--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -238,12 +238,18 @@ sendfile_adapter (struct MHD_Connection *connection)
238#else /* HAVE_SENDFILE64 */ 238#else /* HAVE_SENDFILE64 */
239 off64_t offset; 239 off64_t offset;
240#endif /* HAVE_SENDFILE64 */ 240#endif /* HAVE_SENDFILE64 */
241 const bool used_thr_p_c = (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION));
242 size_t send_size;
241 mhd_assert (MHD_resp_sender_sendfile == connection->resp_sender); 243 mhd_assert (MHD_resp_sender_sendfile == connection->resp_sender);
242 244
243 offsetu64 = connection->response_write_position + connection->response->fd_off; 245 offsetu64 = connection->response_write_position + connection->response->fd_off;
244 left = connection->response->total_size - connection->response_write_position; 246 left = connection->response->total_size - connection->response_write_position;
245 if (left > SSIZE_MAX) 247 /* Do not allow system to stick sending on single fast connection:
246 left = SSIZE_MAX; 248 * use 128KiB chunks (2MiB for thread-per-connection). */
249 if (!used_thr_p_c)
250 send_size = (left > 0x20000) ? 0x20000 : (size_t) left;
251 else
252 send_size = (left > 0x200000) ? 0x200000 : (size_t) left;
247#ifndef HAVE_SENDFILE64 253#ifndef HAVE_SENDFILE64
248 if ((uint64_t)OFF_T_MAX < offsetu64) 254 if ((uint64_t)OFF_T_MAX < offsetu64)
249 { /* Retry to send with standard 'send()'. */ 255 { /* Retry to send with standard 'send()'. */
@@ -254,7 +260,7 @@ sendfile_adapter (struct MHD_Connection *connection)
254 ret = sendfile (connection->socket_fd, 260 ret = sendfile (connection->socket_fd,
255 file_fd, 261 file_fd,
256 &offset, 262 &offset,
257 left); 263 send_size);
258#else /* HAVE_SENDFILE64 */ 264#else /* HAVE_SENDFILE64 */
259 if ((uint64_t)OFF64_T_MAX < offsetu64) 265 if ((uint64_t)OFF64_T_MAX < offsetu64)
260 { /* Retry to send with standard 'send()'. */ 266 { /* Retry to send with standard 'send()'. */
@@ -265,7 +271,7 @@ sendfile_adapter (struct MHD_Connection *connection)
265 ret = sendfile64 (connection->socket_fd, 271 ret = sendfile64 (connection->socket_fd,
266 file_fd, 272 file_fd,
267 &offset, 273 &offset,
268 left); 274 send_size);
269#endif /* HAVE_SENDFILE64 */ 275#endif /* HAVE_SENDFILE64 */
270 if (0 > ret) 276 if (0 > ret)
271 { 277 {
@@ -292,7 +298,7 @@ sendfile_adapter (struct MHD_Connection *connection)
292 return MHD_ERR_AGAIN_; 298 return MHD_ERR_AGAIN_;
293 } 299 }
294#ifdef EPOLL_SUPPORT 300#ifdef EPOLL_SUPPORT
295 else if (left > (uint64_t)ret) 301 else if (send_size > (size_t)ret)
296 connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY; 302 connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY;
297#endif /* EPOLL_SUPPORT */ 303#endif /* EPOLL_SUPPORT */
298 return ret; 304 return ret;