Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nginx/nginx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxim Dounin <mdounin@mdounin.ru>2019-10-17 16:02:19 +0300
committerMaxim Dounin <mdounin@mdounin.ru>2019-10-17 16:02:19 +0300
commitfac4c7bdf53ee7d8fec6568f1e9fecefcde6feba (patch)
tree89ced1f3ff8283bc70f825c48083e1055cdc9135 /src/os/win32
parentd2ea226229f4039953af48bb7e0599c076f01c1f (diff)
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.
Diffstat (limited to 'src/os/win32')
-rw-r--r--src/os/win32/ngx_socket.c15
-rw-r--r--src/os/win32/ngx_socket.h3
-rw-r--r--src/os/win32/ngx_win32_config.h4
-rw-r--r--src/os/win32/ngx_wsarecv.c39
-rw-r--r--src/os/win32/ngx_wsarecv_chain.c35
5 files changed, 96 insertions, 0 deletions
diff --git a/src/os/win32/ngx_socket.c b/src/os/win32/ngx_socket.c
index 05a39f4cc..b1b4afb8a 100644
--- a/src/os/win32/ngx_socket.c
+++ b/src/os/win32/ngx_socket.c
@@ -28,6 +28,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)
{
return 0;
diff --git a/src/os/win32/ngx_socket.h b/src/os/win32/ngx_socket.h
index f8a453d56..ab56bc8b3 100644
--- a/src/os/win32/ngx_socket.h
+++ b/src/os/win32/ngx_socket.h
@@ -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()"
diff --git a/src/os/win32/ngx_win32_config.h b/src/os/win32/ngx_win32_config.h
index 4824d05a8..96156870d 100644
--- a/src/os/win32/ngx_win32_config.h
+++ b/src/os/win32/ngx_win32_config.h
@@ -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
diff --git a/src/os/win32/ngx_wsarecv.c b/src/os/win32/ngx_wsarecv.c
index 1925f0b17..ac883107b 100644
--- a/src/os/win32/ngx_wsarecv.c
+++ b/src/os/win32/ngx_wsarecv.c
@@ -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;
}
diff --git a/src/os/win32/ngx_wsarecv_chain.c b/src/os/win32/ngx_wsarecv_chain.c
index 2598e091c..87f023911 100644
--- a/src/os/win32/ngx_wsarecv_chain.c
+++ b/src/os/win32/ngx_wsarecv_chain.c
@@ -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;
}