mirror of
https://github.com/nginx/nginx.git
synced 2024-11-23 20:19:02 +08:00
Events: available bytes calculation via ioctl(FIONREAD).
This makes it possible to avoid looping for a long time while working with a fast enough peer when data are added to the socket buffer faster than we are able to read and process them (ticket #1431). This is basically what we already do on FreeBSD with kqueue, where information about the number of bytes in the socket buffer is returned by the kevent() call. With other event methods rev->available is now set to -1 when the socket is ready for reading. Later in ngx_recv() and ngx_recv_chain(), if full buffer is received, real number of bytes in the socket buffer is retrieved using ioctl(FIONREAD). Reading more than this number of bytes ensures that even with edge-triggered event methods the event will be triggered again, so it is safe to stop processing of the socket and switch to other connections. Using ioctl(FIONREAD) only after reading a full buffer is an optimization. With this approach we only call ioctl(FIONREAD) when there are at least two recv()/readv() calls.
This commit is contained in:
parent
d2ea226229
commit
fac4c7bdf5
12
auto/unix
12
auto/unix
@ -943,6 +943,18 @@ ngx_feature_test="int i = FIONBIO; printf(\"%d\", i)"
|
||||
. auto/feature
|
||||
|
||||
|
||||
ngx_feature="ioctl(FIONREAD)"
|
||||
ngx_feature_name="NGX_HAVE_FIONREAD"
|
||||
ngx_feature_run=no
|
||||
ngx_feature_incs="#include <sys/ioctl.h>
|
||||
#include <stdio.h>
|
||||
$NGX_INCLUDE_SYS_FILIO_H"
|
||||
ngx_feature_path=
|
||||
ngx_feature_libs=
|
||||
ngx_feature_test="int i = FIONREAD; printf(\"%d\", i)"
|
||||
. auto/feature
|
||||
|
||||
|
||||
ngx_feature="struct tm.tm_gmtoff"
|
||||
ngx_feature_name="NGX_HAVE_GMTOFF"
|
||||
ngx_feature_run=no
|
||||
|
@ -495,6 +495,7 @@ ngx_devpoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
|
||||
|
||||
if ((revents & POLLIN) && rev->active) {
|
||||
rev->ready = 1;
|
||||
rev->available = -1;
|
||||
|
||||
if (flags & NGX_POST_EVENTS) {
|
||||
queue = rev->accept ? &ngx_posted_accept_events
|
||||
|
@ -886,11 +886,10 @@ ngx_epoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
|
||||
if (revents & EPOLLRDHUP) {
|
||||
rev->pending_eof = 1;
|
||||
}
|
||||
|
||||
rev->available = 1;
|
||||
#endif
|
||||
|
||||
rev->ready = 1;
|
||||
rev->available = -1;
|
||||
|
||||
if (flags & NGX_POST_EVENTS) {
|
||||
queue = rev->accept ? &ngx_posted_accept_events
|
||||
|
@ -559,6 +559,7 @@ ngx_eventport_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
|
||||
|
||||
if (revents & POLLIN) {
|
||||
rev->ready = 1;
|
||||
rev->available = -1;
|
||||
|
||||
if (flags & NGX_POST_EVENTS) {
|
||||
queue = rev->accept ? &ngx_posted_accept_events
|
||||
|
@ -370,6 +370,7 @@ ngx_poll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
|
||||
|
||||
ev = c->read;
|
||||
ev->ready = 1;
|
||||
ev->available = -1;
|
||||
|
||||
queue = ev->accept ? &ngx_posted_accept_events
|
||||
: &ngx_posted_events;
|
||||
|
@ -330,6 +330,7 @@ ngx_select_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
|
||||
|
||||
if (found) {
|
||||
ev->ready = 1;
|
||||
ev->available = -1;
|
||||
|
||||
queue = ev->accept ? &ngx_posted_accept_events
|
||||
: &ngx_posted_events;
|
||||
|
@ -380,6 +380,7 @@ ngx_poll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
|
||||
|
||||
ev = c->read;
|
||||
ev->ready = 1;
|
||||
ev->available = -1;
|
||||
|
||||
queue = ev->accept ? &ngx_posted_accept_events
|
||||
: &ngx_posted_events;
|
||||
|
@ -330,6 +330,7 @@ ngx_select_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
|
||||
|
||||
if (found) {
|
||||
ev->ready = 1;
|
||||
ev->available = -1;
|
||||
|
||||
queue = ev->accept ? &ngx_posted_accept_events
|
||||
: &ngx_posted_events;
|
||||
|
@ -91,21 +91,14 @@ struct ngx_event_s {
|
||||
* write: available space in buffer when event is ready
|
||||
* or lowat when event is set with NGX_LOWAT_EVENT flag
|
||||
*
|
||||
* epoll with EPOLLRDHUP:
|
||||
* accept: 1 if accept many, 0 otherwise
|
||||
* read: 1 if there can be data to read, 0 otherwise
|
||||
*
|
||||
* iocp: TODO
|
||||
*
|
||||
* otherwise:
|
||||
* accept: 1 if accept many, 0 otherwise
|
||||
* read: bytes to read when event is ready, -1 if not known
|
||||
*/
|
||||
|
||||
#if (NGX_HAVE_KQUEUE) || (NGX_HAVE_IOCP)
|
||||
int available;
|
||||
#else
|
||||
unsigned available:1;
|
||||
#endif
|
||||
|
||||
ngx_event_handler_pt handler;
|
||||
|
||||
|
@ -60,7 +60,7 @@ ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain, off_t limit)
|
||||
"readv: eof:%d, avail:%d",
|
||||
rev->pending_eof, rev->available);
|
||||
|
||||
if (!rev->available && !rev->pending_eof) {
|
||||
if (rev->available == 0 && !rev->pending_eof) {
|
||||
return NGX_AGAIN;
|
||||
}
|
||||
}
|
||||
@ -165,6 +165,40 @@ ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain, off_t limit)
|
||||
|
||||
#endif
|
||||
|
||||
#if (NGX_HAVE_FIONREAD)
|
||||
|
||||
if (rev->available >= 0) {
|
||||
rev->available -= n;
|
||||
|
||||
/*
|
||||
* negative rev->available means some additional bytes
|
||||
* were received between kernel notification and readv(),
|
||||
* and therefore ev->ready can be safely reset even for
|
||||
* edge-triggered event methods
|
||||
*/
|
||||
|
||||
if (rev->available < 0) {
|
||||
rev->available = 0;
|
||||
rev->ready = 0;
|
||||
}
|
||||
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
||||
"readv: avail:%d", rev->available);
|
||||
|
||||
} else if (n == size) {
|
||||
|
||||
if (ngx_socket_nread(c->fd, &rev->available) == -1) {
|
||||
n = ngx_connection_error(c, ngx_socket_errno,
|
||||
ngx_socket_nread_n " failed");
|
||||
break;
|
||||
}
|
||||
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
||||
"readv: avail:%d", rev->available);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if (NGX_HAVE_EPOLLRDHUP)
|
||||
|
||||
if ((ngx_event_flags & NGX_USE_EPOLL_EVENT)
|
||||
|
@ -57,7 +57,7 @@ ngx_unix_recv(ngx_connection_t *c, u_char *buf, size_t size)
|
||||
"recv: eof:%d, avail:%d",
|
||||
rev->pending_eof, rev->available);
|
||||
|
||||
if (!rev->available && !rev->pending_eof) {
|
||||
if (rev->available == 0 && !rev->pending_eof) {
|
||||
rev->ready = 0;
|
||||
return NGX_AGAIN;
|
||||
}
|
||||
@ -116,6 +116,40 @@ ngx_unix_recv(ngx_connection_t *c, u_char *buf, size_t size)
|
||||
|
||||
#endif
|
||||
|
||||
#if (NGX_HAVE_FIONREAD)
|
||||
|
||||
if (rev->available >= 0) {
|
||||
rev->available -= n;
|
||||
|
||||
/*
|
||||
* negative rev->available means some additional bytes
|
||||
* were received between kernel notification and recv(),
|
||||
* and therefore ev->ready can be safely reset even for
|
||||
* edge-triggered event methods
|
||||
*/
|
||||
|
||||
if (rev->available < 0) {
|
||||
rev->available = 0;
|
||||
rev->ready = 0;
|
||||
}
|
||||
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
||||
"recv: avail:%d", rev->available);
|
||||
|
||||
} else if ((size_t) n == size) {
|
||||
|
||||
if (ngx_socket_nread(c->fd, &rev->available) == -1) {
|
||||
n = ngx_connection_error(c, ngx_socket_errno,
|
||||
ngx_socket_nread_n " failed");
|
||||
break;
|
||||
}
|
||||
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
||||
"recv: avail:%d", rev->available);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if (NGX_HAVE_EPOLLRDHUP)
|
||||
|
||||
if ((ngx_event_flags & NGX_USE_EPOLL_EVENT)
|
||||
|
@ -38,6 +38,13 @@ int ngx_blocking(ngx_socket_t s);
|
||||
|
||||
#endif
|
||||
|
||||
#if (NGX_HAVE_FIONREAD)
|
||||
|
||||
#define ngx_socket_nread(s, n) ioctl(s, FIONREAD, n)
|
||||
#define ngx_socket_nread_n "ioctl(FIONREAD)"
|
||||
|
||||
#endif
|
||||
|
||||
int ngx_tcp_nopush(ngx_socket_t s);
|
||||
int ngx_tcp_push(ngx_socket_t s);
|
||||
|
||||
|
@ -27,6 +27,21 @@ ngx_blocking(ngx_socket_t s)
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
ngx_socket_nread(ngx_socket_t s, int *n)
|
||||
{
|
||||
unsigned long nread;
|
||||
|
||||
if (ioctlsocket(s, FIONREAD, &nread) == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
*n = nread;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
ngx_tcp_push(ngx_socket_t s)
|
||||
{
|
||||
|
@ -31,6 +31,9 @@ int ngx_blocking(ngx_socket_t s);
|
||||
#define ngx_nonblocking_n "ioctlsocket(FIONBIO)"
|
||||
#define ngx_blocking_n "ioctlsocket(!FIONBIO)"
|
||||
|
||||
int ngx_socket_nread(ngx_socket_t s, int *n);
|
||||
#define ngx_socket_nread_n "ioctlsocket(FIONREAD)"
|
||||
|
||||
#define ngx_shutdown_socket shutdown
|
||||
#define ngx_shutdown_socket_n "shutdown()"
|
||||
|
||||
|
@ -273,6 +273,10 @@ typedef int sig_atomic_t;
|
||||
#define NGX_HAVE_SO_SNDLOWAT 0
|
||||
#endif
|
||||
|
||||
#ifndef NGX_HAVE_FIONREAD
|
||||
#define NGX_HAVE_FIONREAD 1
|
||||
#endif
|
||||
|
||||
#define NGX_HAVE_GETADDRINFO 1
|
||||
|
||||
#define ngx_random rand
|
||||
|
@ -51,6 +51,45 @@ ngx_wsarecv(ngx_connection_t *c, u_char *buf, size_t size)
|
||||
return n;
|
||||
}
|
||||
|
||||
#if (NGX_HAVE_FIONREAD)
|
||||
|
||||
if (rev->available >= 0 && bytes > 0) {
|
||||
rev->available -= bytes;
|
||||
|
||||
/*
|
||||
* negative rev->available means some additional bytes
|
||||
* were received between kernel notification and WSARecv(),
|
||||
* and therefore ev->ready can be safely reset even for
|
||||
* edge-triggered event methods
|
||||
*/
|
||||
|
||||
if (rev->available < 0) {
|
||||
rev->available = 0;
|
||||
rev->ready = 0;
|
||||
}
|
||||
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
||||
"WSARecv: avail:%d", rev->available);
|
||||
|
||||
} else if (bytes == size) {
|
||||
|
||||
if (ngx_socket_nread(c->fd, &rev->available) == -1) {
|
||||
n = ngx_connection_error(c, ngx_socket_errno,
|
||||
ngx_socket_nread_n " failed");
|
||||
|
||||
if (n == NGX_ERROR) {
|
||||
rev->error = 1;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
||||
"WSARecv: avail:%d", rev->available);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
if (bytes < size) {
|
||||
rev->ready = 0;
|
||||
}
|
||||
|
@ -94,6 +94,41 @@ ngx_wsarecv_chain(ngx_connection_t *c, ngx_chain_t *chain, off_t limit)
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
#if (NGX_HAVE_FIONREAD)
|
||||
|
||||
if (rev->available >= 0 && bytes > 0) {
|
||||
rev->available -= bytes;
|
||||
|
||||
/*
|
||||
* negative rev->available means some additional bytes
|
||||
* were received between kernel notification and WSARecv(),
|
||||
* and therefore ev->ready can be safely reset even for
|
||||
* edge-triggered event methods
|
||||
*/
|
||||
|
||||
if (rev->available < 0) {
|
||||
rev->available = 0;
|
||||
rev->ready = 0;
|
||||
}
|
||||
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
||||
"WSARecv: avail:%d", rev->available);
|
||||
|
||||
} else if (bytes == size) {
|
||||
|
||||
if (ngx_socket_nread(c->fd, &rev->available) == -1) {
|
||||
rev->error = 1;
|
||||
ngx_connection_error(c, ngx_socket_errno,
|
||||
ngx_socket_nread_n " failed");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
||||
"WSARecv: avail:%d", rev->available);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
if (bytes < size) {
|
||||
rev->ready = 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user