Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions operating on SOCK_STREAM and buffers. |
| 3 | * |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 23 | #include <common/config.h> |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 24 | #include <common/debug.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 25 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 26 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 27 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 29 | #include <proto/buffers.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | #include <proto/client.h> |
| 31 | #include <proto/fd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | #include <proto/stream_sock.h> |
| 33 | #include <proto/task.h> |
| 34 | |
| 35 | |
| 36 | /* |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 37 | * this function is called on a read event from a stream socket. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 38 | * It returns 0 if we have a high confidence that we will not be |
| 39 | * able to read more data without polling first. Returns non-zero |
| 40 | * otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 42 | int stream_sock_read(int fd) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 43 | __label__ out_wakeup, out_shutdown_r, out_error; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 44 | struct buffer *b = fdtab[fd].cb[DIR_RD].b; |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 45 | int ret, max, retval, cur_read; |
Willy Tarreau | b8949f1 | 2007-03-23 22:39:59 +0100 | [diff] [blame] | 46 | int read_poll = MAX_READ_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | |
| 48 | #ifdef DEBUG_FULL |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 49 | fprintf(stderr,"stream_sock_read : fd=%d, ev=0x%02x, owner=%p\n", fd, fdtab[fd].ev, fdtab[fd].owner); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #endif |
| 51 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 52 | retval = 1; |
| 53 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 54 | /* stop immediately on errors */ |
| 55 | if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 56 | goto out_error; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 57 | |
| 58 | /* stop here if we reached the end of data */ |
| 59 | if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) |
| 60 | goto out_shutdown_r; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 61 | |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 62 | cur_read = 0; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 63 | while (1) { |
| 64 | /* |
| 65 | * 1. compute the maximum block size we can read at once. |
| 66 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 67 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 68 | b->r = b->w = b->lr = b->data; |
| 69 | max = b->rlim - b->data; |
| 70 | } |
| 71 | else if (b->r > b->w) { |
| 72 | max = b->rlim - b->r; |
| 73 | } |
| 74 | else { |
| 75 | max = b->w - b->r; |
| 76 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 77 | * since it means that the rewrite protection has been removed. This |
| 78 | * implies that the if statement can be removed. |
| 79 | */ |
| 80 | if (max > b->rlim - b->data) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 81 | max = b->rlim - b->data; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 82 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 83 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 84 | if (unlikely(max == 0)) { |
| 85 | /* Not anymore room to store data. This should theorically |
| 86 | * never happen, but better safe than sorry ! |
| 87 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 88 | b->flags |= BF_FULL; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 89 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 90 | b->rex = TICK_ETERNITY; |
| 91 | goto out_wakeup; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 92 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 94 | /* |
| 95 | * 2. read the largest possible block |
| 96 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 97 | #ifndef MSG_NOSIGNAL |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 98 | { |
| 99 | int skerr; |
| 100 | socklen_t lskerr = sizeof(skerr); |
| 101 | |
| 102 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 103 | if (ret == -1 || skerr) |
| 104 | ret = -1; |
| 105 | else |
| 106 | ret = recv(fd, b->r, max, 0); |
| 107 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 108 | #else |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 109 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | #endif |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 111 | if (ret > 0) { |
| 112 | b->r += ret; |
| 113 | b->l += ret; |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 114 | cur_read += ret; |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 115 | b->flags |= BF_READ_PARTIAL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 116 | b->flags &= ~BF_EMPTY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 118 | if (b->r == b->data + BUFSIZE) { |
| 119 | b->r = b->data; /* wrap around the buffer */ |
| 120 | } |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 121 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 122 | b->total += ret; |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 123 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 124 | if (b->l >= b->rlim - b->data) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 125 | /* The buffer is now full, there's no point in going through |
| 126 | * the loop again. |
| 127 | */ |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 128 | if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) { |
| 129 | b->xfer_small = 0; |
| 130 | b->xfer_large++; |
| 131 | if (b->xfer_large >= 3) { |
| 132 | /* we call this buffer a fast streamer if it manages |
| 133 | * to be filled in one call 3 consecutive times. |
| 134 | */ |
| 135 | b->flags |= (BF_STREAMER | BF_STREAMER_FAST); |
| 136 | //fputc('+', stderr); |
| 137 | } |
| 138 | } |
| 139 | else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) && |
| 140 | (cur_read <= BUFSIZE / 2)) { |
| 141 | b->xfer_large = 0; |
| 142 | b->xfer_small++; |
| 143 | if (b->xfer_small >= 2) { |
| 144 | /* if the buffer has been at least half full twice, |
| 145 | * we receive faster than we send, so at least it |
| 146 | * is not a "fast streamer". |
| 147 | */ |
| 148 | b->flags &= ~BF_STREAMER_FAST; |
| 149 | //fputc('-', stderr); |
| 150 | } |
| 151 | } |
| 152 | else { |
| 153 | b->xfer_small = 0; |
| 154 | b->xfer_large = 0; |
| 155 | } |
| 156 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 157 | b->flags |= BF_FULL; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 158 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 159 | b->rex = TICK_ETERNITY; |
| 160 | goto out_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 163 | /* if too many bytes were missing from last read, it means that |
| 164 | * it's pointless trying to read again because the system does |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 165 | * not have them in buffers. BTW, if FD_POLL_HUP was present, |
| 166 | * it means that we have reached the end and that the connection |
| 167 | * is closed. |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 168 | */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 169 | if (ret < max) { |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 170 | if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) && |
| 171 | (cur_read <= BUFSIZE / 2)) { |
| 172 | b->xfer_large = 0; |
| 173 | b->xfer_small++; |
| 174 | if (b->xfer_small >= 3) { |
| 175 | /* we have read less than half of the buffer in |
| 176 | * one pass, and this happened at least 3 times. |
| 177 | * This is definitely not a streamer. |
| 178 | */ |
| 179 | b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST); |
| 180 | //fputc('!', stderr); |
| 181 | } |
| 182 | } |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 183 | /* unfortunately, on level-triggered events, POLL_HUP |
| 184 | * is generally delivered AFTER the system buffer is |
| 185 | * empty, so this one might never match. |
| 186 | */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 187 | if (fdtab[fd].ev & FD_POLL_HUP) |
| 188 | goto out_shutdown_r; |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 189 | |
| 190 | /* if a streamer has read few data, it may be because we |
| 191 | * have exhausted system buffers. It's not worth trying |
| 192 | * again. |
| 193 | */ |
| 194 | if (b->flags & BF_STREAMER) |
| 195 | break; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 196 | } |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 197 | |
| 198 | /* generally if we read something smaller than 1 or 2 MSS, |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 199 | * it means that either we have exhausted the system's |
| 200 | * buffers (streamer or question-response protocol) or that |
| 201 | * the connection will be closed. Streamers are easily |
| 202 | * detected so we return early. For other cases, it's still |
| 203 | * better to perform a last read to be sure, because it may |
| 204 | * save one complete poll/read/wakeup cycle in case of shutdown. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 205 | */ |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 206 | if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 207 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 208 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 209 | if (--read_poll <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 210 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 211 | } |
| 212 | else if (ret == 0) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 213 | /* connection closed */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 214 | goto out_shutdown_r; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 215 | } |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 216 | else if (errno == EAGAIN) { |
| 217 | /* Ignore EAGAIN but inform the poller that there is |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 218 | * nothing to read left. But we may have done some work |
| 219 | * justifying to notify the task. |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 220 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 221 | retval = 0; |
| 222 | break; |
| 223 | } |
| 224 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 225 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 226 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 227 | } /* while (1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 228 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 229 | /* |
| 230 | * The only way to get out of this loop is to have stopped reading |
| 231 | * without any error nor close, either by limiting the number of |
| 232 | * loops, or because of an EAGAIN. We only rearm the timer if we |
| 233 | * have at least read something. |
| 234 | */ |
| 235 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 236 | if (tick_isset(b->rex) && b->flags & BF_READ_PARTIAL) |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 237 | b->rex = tick_add_ifset(now_ms, b->rto); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 238 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 239 | if (!(b->flags & BF_READ_ACTIVITY)) |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 240 | goto out_skip_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 241 | out_wakeup: |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame^] | 242 | task_wakeup(fdtab[fd].owner, TASK_WOKEN_IO); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 243 | |
| 244 | out_skip_wakeup: |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 245 | fdtab[fd].ev &= ~FD_POLL_IN; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 246 | return retval; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 247 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 248 | out_shutdown_r: |
| 249 | fdtab[fd].ev &= ~FD_POLL_HUP; |
| 250 | b->flags |= BF_READ_NULL; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 251 | b->rex = TICK_ETERNITY; |
| 252 | goto out_wakeup; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 253 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 254 | out_error: |
| 255 | /* There was an error. we must wakeup the task. No need to clear |
| 256 | * the events, the task will do it. |
| 257 | */ |
| 258 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 259 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 260 | b->rex = TICK_ETERNITY; |
| 261 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | |
| 265 | /* |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 266 | * this function is called on a write event from a stream socket. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 267 | * It returns 0 if we have a high confidence that we will not be |
| 268 | * able to write more data without polling first. Returns non-zero |
| 269 | * otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 270 | */ |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 271 | int stream_sock_write(int fd) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 272 | __label__ out_wakeup, out_error; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 273 | struct buffer *b = fdtab[fd].cb[DIR_WR].b; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 274 | int ret, max, retval; |
| 275 | int write_poll = MAX_WRITE_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 276 | |
| 277 | #ifdef DEBUG_FULL |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 278 | fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 279 | #endif |
| 280 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 281 | retval = 1; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 282 | if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 283 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 284 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 285 | while (1) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 286 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 287 | b->r = b->w = b->lr = b->data; |
| 288 | max = 0; |
| 289 | } |
| 290 | else if (b->r > b->w) { |
| 291 | max = b->r - b->w; |
| 292 | } |
| 293 | else { |
| 294 | max = b->data + BUFSIZE - b->w; |
| 295 | } |
| 296 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 297 | if (max == 0) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 298 | /* may be we have received a connection acknowledgement in TCP mode without data */ |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 299 | if (likely(fdtab[fd].state == FD_STCONN)) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 300 | /* We have no data to send to check the connection, and |
| 301 | * getsockopt() will not inform us whether the connection |
| 302 | * is still pending. So we'll reuse connect() to check the |
| 303 | * state of the socket. This has the advantage of givig us |
| 304 | * the following info : |
| 305 | * - error |
| 306 | * - connecting (EALREADY, EINPROGRESS) |
| 307 | * - connected (EISCONN, 0) |
| 308 | */ |
Willy Tarreau | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame] | 309 | if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 310 | errno = 0; |
| 311 | |
| 312 | if (errno == EALREADY || errno == EINPROGRESS) { |
| 313 | retval = 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 314 | goto out_may_wakeup; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 315 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 316 | |
| 317 | if (errno && errno != EISCONN) |
| 318 | goto out_error; |
| 319 | |
| 320 | /* OK we just need to indicate that we got a connection |
| 321 | * and that we wrote nothing. |
| 322 | */ |
| 323 | b->flags |= BF_WRITE_NULL; |
| 324 | fdtab[fd].state = FD_STREADY; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 327 | /* Funny, we were called to write something but there wasn't |
| 328 | * anything. Theorically we cannot get there, but just in case, |
| 329 | * let's disable the write event and pretend we never came there. |
| 330 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 331 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 332 | b->wex = TICK_ETERNITY; |
| 333 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | #ifndef MSG_NOSIGNAL |
| 337 | { |
| 338 | int skerr; |
| 339 | socklen_t lskerr = sizeof(skerr); |
| 340 | |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 341 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 342 | if (ret == -1 || skerr) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 343 | ret = -1; |
| 344 | else |
| 345 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 346 | } |
| 347 | #else |
| 348 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 349 | #endif |
| 350 | |
| 351 | if (ret > 0) { |
| 352 | b->l -= ret; |
| 353 | b->w += ret; |
| 354 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 355 | b->flags |= BF_WRITE_PARTIAL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 356 | |
| 357 | if (b->l < b->rlim - b->data) |
| 358 | b->flags &= ~BF_FULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 359 | |
| 360 | if (b->w == b->data + BUFSIZE) { |
| 361 | b->w = b->data; /* wrap around the buffer */ |
| 362 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 363 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 364 | if (!b->l) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 365 | b->flags |= BF_EMPTY; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 366 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 367 | b->wex = TICK_ETERNITY; |
| 368 | goto out_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 369 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 370 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 371 | /* if the system buffer is full, don't insist */ |
| 372 | if (ret < max) |
| 373 | break; |
| 374 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 375 | if (--write_poll <= 0) |
| 376 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 377 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 378 | else if (ret == 0 || errno == EAGAIN) { |
| 379 | /* nothing written, just pretend we were never called |
| 380 | * and wait for the socket to be ready. But we may have |
| 381 | * done some work justifying to notify the task. |
| 382 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 383 | retval = 0; |
| 384 | break; |
| 385 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 386 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 387 | goto out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 388 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 389 | } /* while (1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 390 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 391 | /* |
| 392 | * The only way to get out of this loop is to have stopped writing |
| 393 | * without any error, either by limiting the number of loops, or |
| 394 | * because of an EAGAIN. We only rearm the timer if we have at least |
| 395 | * written something. |
| 396 | */ |
| 397 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 398 | if (tick_isset(b->wex) && b->flags & BF_WRITE_PARTIAL) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 399 | b->wex = tick_add_ifset(now_ms, b->wto); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 400 | if (tick_isset(b->wex)) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 401 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 402 | * we refresh it. A solution would be to merge read+write timeouts into a |
| 403 | * unique one, although that needs some study particularly on full-duplex |
| 404 | * TCP connections. */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 405 | if (tick_isset(b->rex) && !(b->flags & BF_SHUTR)) |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 406 | b->rex = b->wex; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 407 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 408 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 409 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 410 | out_may_wakeup: |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 411 | if (!(b->flags & BF_WRITE_ACTIVITY)) |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 412 | goto out_skip_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 413 | out_wakeup: |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame^] | 414 | task_wakeup(fdtab[fd].owner, TASK_WOKEN_IO); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 415 | |
| 416 | out_skip_wakeup: |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 417 | fdtab[fd].ev &= ~FD_POLL_OUT; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 418 | return retval; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 419 | |
| 420 | out_error: |
| 421 | /* There was an error. we must wakeup the task. No need to clear |
| 422 | * the events, the task will do it. |
| 423 | */ |
| 424 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 425 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 426 | b->wex = TICK_ETERNITY; |
| 427 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 430 | |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 431 | /* |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 432 | * This function only has to be called once after a wakeup event during a data |
| 433 | * phase. It controls the file descriptor's status, as well as read and write |
| 434 | * timeouts. |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 435 | */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 436 | int stream_sock_data_check_errors(int fd) |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 437 | { |
| 438 | struct buffer *ib = fdtab[fd].cb[DIR_RD].b; |
| 439 | struct buffer *ob = fdtab[fd].cb[DIR_WR].b; |
| 440 | |
| 441 | DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibl=%d obl=%d\n", |
| 442 | now_ms, __FUNCTION__, |
| 443 | fd, fdtab[fd].owner, |
| 444 | ib, ob, |
| 445 | ib->rex, ob->wex, |
| 446 | ib->flags, ob->flags, |
| 447 | ib->l, ob->l); |
| 448 | |
| 449 | /* Read or write error on the file descriptor */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 450 | if (unlikely(fdtab[fd].state == FD_STERROR)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 451 | //trace_term(t, TT_HTTP_SRV_6); |
| 452 | if (!ob->cons->err_type) { |
| 453 | //ob->cons->err_loc = t->srv; |
| 454 | ob->cons->err_type = SI_ET_DATA_ERR; |
| 455 | } |
| 456 | buffer_shutw(ob); |
| 457 | ob->flags |= BF_WRITE_ERROR; |
| 458 | buffer_shutr(ib); |
| 459 | ib->flags |= BF_READ_ERROR; |
| 460 | |
| 461 | do_close_and_return: |
| 462 | fd_delete(fd); |
| 463 | ob->cons->state = SI_ST_CLO; |
| 464 | return 0; |
| 465 | } |
| 466 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 467 | /* Read timeout */ |
| 468 | if (unlikely(!(ib->flags & (BF_SHUTR|BF_READ_TIMEOUT)) && tick_is_expired(ib->rex, now_ms))) { |
| 469 | //trace_term(t, TT_HTTP_SRV_12); |
| 470 | ib->flags |= BF_READ_TIMEOUT; |
| 471 | if (!ob->cons->err_type) { |
| 472 | //ob->cons->err_loc = t->srv; |
| 473 | ob->cons->err_type = SI_ET_DATA_TO; |
| 474 | } |
| 475 | buffer_shutr(ib); |
| 476 | if (ob->flags & BF_SHUTW) |
| 477 | goto do_close_and_return; |
| 478 | EV_FD_CLR(fd, DIR_RD); |
| 479 | } |
| 480 | |
| 481 | /* Write timeout */ |
| 482 | if (unlikely(!(ob->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) && tick_is_expired(ob->wex, now_ms))) { |
| 483 | //trace_term(t, TT_HTTP_SRV_13); |
| 484 | ob->flags |= BF_WRITE_TIMEOUT; |
| 485 | if (!ob->cons->err_type) { |
| 486 | //ob->cons->err_loc = t->srv; |
| 487 | ob->cons->err_type = SI_ET_DATA_TO; |
| 488 | } |
| 489 | buffer_shutw(ob); |
| 490 | if (ib->flags & BF_SHUTR) |
| 491 | goto do_close_and_return; |
| 492 | |
| 493 | EV_FD_CLR(fd, DIR_WR); |
| 494 | shutdown(fd, SHUT_WR); |
| 495 | } |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Manages a stream_sock connection during its data phase. The buffers are |
| 501 | * examined for various cases of shutdown, then file descriptor and buffers' |
| 502 | * flags are updated accordingly. |
| 503 | */ |
| 504 | int stream_sock_data_update(int fd) |
| 505 | { |
| 506 | struct buffer *ib = fdtab[fd].cb[DIR_RD].b; |
| 507 | struct buffer *ob = fdtab[fd].cb[DIR_WR].b; |
| 508 | |
| 509 | DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibl=%d obl=%d\n", |
| 510 | now_ms, __FUNCTION__, |
| 511 | fd, fdtab[fd].owner, |
| 512 | ib, ob, |
| 513 | ib->rex, ob->wex, |
| 514 | ib->flags, ob->flags, |
| 515 | ib->l, ob->l); |
| 516 | |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 517 | /* Check if we need to close the read side */ |
| 518 | if (!(ib->flags & BF_SHUTR)) { |
| 519 | /* Last read, forced read-shutdown, or other end closed */ |
| 520 | if (ib->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) { |
| 521 | //trace_term(t, TT_HTTP_SRV_10); |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 522 | buffer_shutr(ib); |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 523 | if (ob->flags & BF_SHUTW) { |
| 524 | fd_delete(fd); |
| 525 | ob->cons->state = SI_ST_CLO; |
| 526 | return 0; |
| 527 | } |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 528 | EV_FD_CLR(fd, DIR_RD); |
| 529 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /* Check if we need to close the write side */ |
| 533 | if (!(ob->flags & BF_SHUTW)) { |
| 534 | /* Forced write-shutdown or other end closed with empty buffer. */ |
| 535 | if ((ob->flags & BF_SHUTW_NOW) || |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 536 | (ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) { |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 537 | //trace_term(t, TT_HTTP_SRV_11); |
| 538 | buffer_shutw(ob); |
| 539 | if (ib->flags & BF_SHUTR) { |
| 540 | fd_delete(fd); |
| 541 | ob->cons->state = SI_ST_CLO; |
| 542 | return 0; |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 543 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 544 | EV_FD_CLR(fd, DIR_WR); |
| 545 | shutdown(fd, SHUT_WR); |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 546 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 547 | } |
| 548 | return 0; /* other cases change nothing */ |
| 549 | } |
| 550 | |
| 551 | |
| 552 | /* |
| 553 | * Updates a connected stream_sock file descriptor status and timeouts |
| 554 | * according to the buffers' flags. It should only be called once after the |
| 555 | * buffer flags have settled down, and before they are cleared. It doesn't |
| 556 | * harm to call it as often as desired (it just slightly hurts performance). |
| 557 | */ |
| 558 | int stream_sock_data_finish(int fd) |
| 559 | { |
| 560 | struct buffer *ib = fdtab[fd].cb[DIR_RD].b; |
| 561 | struct buffer *ob = fdtab[fd].cb[DIR_WR].b; |
| 562 | |
| 563 | DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibl=%d obl=%d\n", |
| 564 | now_ms, __FUNCTION__, |
| 565 | fd, fdtab[fd].owner, |
| 566 | ib, ob, |
| 567 | ib->rex, ob->wex, |
| 568 | ib->flags, ob->flags, |
| 569 | ib->l, ob->l); |
| 570 | |
| 571 | /* Check if we need to close the read side */ |
| 572 | if (!(ib->flags & BF_SHUTR)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 573 | /* Read not closed, update FD status and timeout for reads */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 574 | if (ib->flags & (BF_FULL|BF_HIJACK)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 575 | /* stop reading */ |
| 576 | EV_FD_COND_C(fd, DIR_RD); |
| 577 | ib->rex = TICK_ETERNITY; |
| 578 | } |
| 579 | else { |
| 580 | /* (re)start reading and update timeout. Note: we don't recompute the timeout |
| 581 | * everytime we get here, otherwise it would risk never to expire. We only |
| 582 | * update it if is was not yet set, or if we already got some read status. |
| 583 | */ |
| 584 | EV_FD_COND_S(fd, DIR_RD); |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 585 | if (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY) |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 586 | ib->rex = tick_add_ifset(now_ms, ib->rto); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /* Check if we need to close the write side */ |
| 591 | if (!(ob->flags & BF_SHUTW)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 592 | /* Write not closed, update FD status and timeout for writes */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 593 | if ((ob->flags & BF_EMPTY) || |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 594 | (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 595 | /* stop writing */ |
| 596 | EV_FD_COND_C(fd, DIR_WR); |
| 597 | ob->wex = TICK_ETERNITY; |
| 598 | } |
| 599 | else { |
| 600 | /* (re)start writing and update timeout. Note: we don't recompute the timeout |
| 601 | * everytime we get here, otherwise it would risk never to expire. We only |
| 602 | * update it if is was not yet set, or if we already got some write status. |
| 603 | */ |
| 604 | EV_FD_COND_S(fd, DIR_WR); |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 605 | if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 606 | ob->wex = tick_add_ifset(now_ms, ob->wto); |
Willy Tarreau | 21e1be8 | 2008-08-29 11:30:14 +0200 | [diff] [blame] | 607 | if (tick_isset(ob->wex) && tick_isset(ib->rex)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 608 | /* Note: depending on the protocol, we don't know if we're waiting |
| 609 | * for incoming data or not. So in order to prevent the socket from |
| 610 | * expiring read timeouts during writes, we refresh the read timeout, |
| 611 | * except if it was already infinite. |
| 612 | */ |
| 613 | ib->rex = ob->wex; |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 618 | return 0; |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 621 | |
| 622 | /* |
| 623 | * Local variables: |
| 624 | * c-indent-level: 8 |
| 625 | * c-basic-offset: 8 |
| 626 | * End: |
| 627 | */ |