comparison src/os/unix/ngx_readv_chain.c @ 7583:efd71d49bde0

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.
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 17 Oct 2019 16:02:19 +0300
parents f7849bfb6d21
children 7f5e3595caff
comparison
equal deleted inserted replaced
7582:70749256af79 7583:efd71d49bde0
58 if (ngx_event_flags & NGX_USE_EPOLL_EVENT) { 58 if (ngx_event_flags & NGX_USE_EPOLL_EVENT) {
59 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, 59 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
60 "readv: eof:%d, avail:%d", 60 "readv: eof:%d, avail:%d",
61 rev->pending_eof, rev->available); 61 rev->pending_eof, rev->available);
62 62
63 if (!rev->available && !rev->pending_eof) { 63 if (rev->available == 0 && !rev->pending_eof) {
64 return NGX_AGAIN; 64 return NGX_AGAIN;
65 } 65 }
66 } 66 }
67 67
68 #endif 68 #endif
159 159
160 rev->available = 0; 160 rev->available = 0;
161 } 161 }
162 162
163 return n; 163 return n;
164 }
165
166 #endif
167
168 #if (NGX_HAVE_FIONREAD)
169
170 if (rev->available >= 0) {
171 rev->available -= n;
172
173 /*
174 * negative rev->available means some additional bytes
175 * were received between kernel notification and readv(),
176 * and therefore ev->ready can be safely reset even for
177 * edge-triggered event methods
178 */
179
180 if (rev->available < 0) {
181 rev->available = 0;
182 rev->ready = 0;
183 }
184
185 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
186 "readv: avail:%d", rev->available);
187
188 } else if (n == size) {
189
190 if (ngx_socket_nread(c->fd, &rev->available) == -1) {
191 n = ngx_connection_error(c, ngx_socket_errno,
192 ngx_socket_nread_n " failed");
193 break;
194 }
195
196 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
197 "readv: avail:%d", rev->available);
164 } 198 }
165 199
166 #endif 200 #endif
167 201
168 #if (NGX_HAVE_EPOLLRDHUP) 202 #if (NGX_HAVE_EPOLLRDHUP)