Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Queue management functions. |
| 3 | * |
Willy Tarreau | ac68c5d | 2009-10-04 23:12:44 +0200 | [diff] [blame] | 4 | * Copyright 2000-2009 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 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 13 | #include <common/config.h> |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 14 | #include <common/memory.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 15 | #include <common/time.h> |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 16 | #include <common/hathreads.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 17 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | #include <proto/queue.h> |
| 19 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 20 | #include <proto/stream.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 21 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | #include <proto/task.h> |
| 23 | |
| 24 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 25 | struct pool_head *pool_head_pendconn; |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 26 | |
| 27 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 28 | int init_pendconn() |
| 29 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 30 | pool_head_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED); |
| 31 | return pool_head_pendconn != NULL; |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 32 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | |
| 34 | /* returns the effective dynamic maxconn for a server, considering the minconn |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 35 | * and the proxy's usage relative to its dynamic connections limit. It is |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 36 | * expected that 0 < s->minconn <= s->maxconn when this is called. If the |
| 37 | * server is currently warming up, the slowstart is also applied to the |
| 38 | * resulting value, which can be lower than minconn in this case, but never |
| 39 | * less than 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 41 | unsigned int srv_dynamic_maxconn(const struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | { |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 43 | unsigned int max; |
| 44 | |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 45 | if (s->proxy->beconn >= s->proxy->fullconn) |
| 46 | /* no fullconn or proxy is full */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 47 | max = s->maxconn; |
| 48 | else if (s->minconn == s->maxconn) |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 49 | /* static limit */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 50 | max = s->maxconn; |
| 51 | else max = MAX(s->minconn, |
| 52 | s->proxy->beconn * s->maxconn / s->proxy->fullconn); |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 53 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 54 | if ((s->cur_state == SRV_ST_STARTING) && |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 55 | now.tv_sec < s->last_change + s->slowstart && |
| 56 | now.tv_sec >= s->last_change) { |
| 57 | unsigned int ratio; |
Willy Tarreau | 28a9e52 | 2008-09-14 17:43:27 +0200 | [diff] [blame] | 58 | ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart; |
| 59 | max = MAX(1, max * ratio / 100); |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 60 | } |
| 61 | return max; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 64 | /* Remove the pendconn from the server/proxy queue. At this stage, the |
| 65 | * connection is not really dequeued. It will be done during the |
| 66 | * process_stream. This function must be called by function owning the locks on |
| 67 | * the pendconn _AND_ the server/proxy. It also decreases the pending count. |
| 68 | * |
| 69 | * The caller must own the lock on the pendconn _AND_ the queue containing the |
| 70 | * pendconn. The pendconn must still be queued. |
Christopher Faulet | f3a55db | 2017-06-09 14:26:38 +0200 | [diff] [blame] | 71 | */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 72 | static void pendconn_unlink(struct pendconn *p) |
| 73 | { |
| 74 | if (p->srv) |
| 75 | p->srv->nbpend--; |
| 76 | else |
| 77 | p->px->nbpend--; |
| 78 | HA_ATOMIC_SUB(&p->px->totpend, 1); |
| 79 | LIST_DEL(&p->list); |
| 80 | LIST_INIT(&p->list); |
Christopher Faulet | f3a55db | 2017-06-09 14:26:38 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 83 | /* Process the next pending connection from either a server or a proxy, and |
| 84 | * returns 0 on success. If no pending connection is found, 1 is returned. |
| 85 | * Note that neither <srv> nor <px> may be NULL. Priority is given to the |
| 86 | * oldest request in the queue if both <srv> and <px> have pending |
| 87 | * requests. This ensures that no request will be left unserved. The <px> queue |
| 88 | * is not considered if the server (or a tracked server) is not RUNNING, is |
| 89 | * disabled, or has a null weight (server going down). The <srv> queue is still |
| 90 | * considered in this case, because if some connections remain there, it means |
| 91 | * that some requests have been forced there after it was seen down (eg: due to |
| 92 | * option persist). The stream is immediately marked as "assigned", and both |
| 93 | * its <srv> and <srv_conn> are set to <srv>. |
| 94 | * |
| 95 | * This function must only be called if the server queue _AND_ the proxy queue |
| 96 | * are locked. Today it is only called by process_srv_queue. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 97 | */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 98 | static int pendconn_process_next_strm(struct server *srv, struct proxy *px) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 100 | struct pendconn *p = NULL; |
| 101 | struct server *rsrv; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 102 | |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 103 | rsrv = srv->track; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 104 | if (!rsrv) |
| 105 | rsrv = srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 106 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 107 | if (srv->nbpend) { |
| 108 | list_for_each_entry(p, &srv->pendconns, list) { |
| 109 | if (!HA_SPIN_TRYLOCK(PENDCONN_LOCK, &p->lock)) |
| 110 | goto ps_found; |
| 111 | } |
| 112 | p = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 114 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 115 | ps_found: |
| 116 | if (srv_currently_usable(rsrv) && px->nbpend) { |
| 117 | struct pendconn *pp; |
| 118 | |
| 119 | list_for_each_entry(pp, &px->pendconns, list) { |
| 120 | /* If the server pendconn is older than the proxy one, |
| 121 | * we process the server one. */ |
| 122 | if (p && !tv_islt(&pp->strm->logs.tv_request, &p->strm->logs.tv_request)) |
| 123 | goto pendconn_found; |
| 124 | |
| 125 | if (!HA_SPIN_TRYLOCK(PENDCONN_LOCK, &pp->lock)) { |
| 126 | /* Let's switch from the server pendconn to the |
| 127 | * proxy pendconn. Don't forget to unlock the |
| 128 | * server pendconn, if any. */ |
| 129 | if (p) |
| 130 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
| 131 | p = pp; |
| 132 | goto pendconn_found; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (!p) |
| 138 | return 1; |
| 139 | |
| 140 | pendconn_found: |
| 141 | pendconn_unlink(p); |
| 142 | p->strm_flags |= SF_ASSIGNED; |
| 143 | p->srv = srv; |
| 144 | |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 145 | HA_ATOMIC_ADD(&srv->served, 1); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 146 | HA_ATOMIC_ADD(&srv->proxy->served, 1); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 147 | if (px->lbprm.server_take_conn) |
| 148 | px->lbprm.server_take_conn(srv); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 149 | __stream_add_srv_conn(p->strm, srv); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 150 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 151 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
| 152 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
| 153 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 156 | /* Manages a server's connection queue. This function will try to dequeue as |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 157 | * many pending streams as possible, and wake them up. |
| 158 | */ |
| 159 | void process_srv_queue(struct server *s) |
| 160 | { |
| 161 | struct proxy *p = s->proxy; |
| 162 | int maxconn; |
| 163 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 164 | HA_SPIN_LOCK(PROXY_LOCK, &p->lock); |
| 165 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 166 | maxconn = srv_dynamic_maxconn(s); |
| 167 | while (s->served < maxconn) { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 168 | if (pendconn_process_next_strm(s, p)) |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 169 | break; |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 170 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 171 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 172 | HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 175 | /* Adds the stream <strm> to the pending connection list of server <strm>->srv |
| 176 | * or to the one of <strm>->proxy if srv is NULL. All counters and back pointers |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 177 | * are updated accordingly. Returns NULL if no memory is available, otherwise the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 178 | * pendconn itself. If the stream was already marked as served, its flag is |
| 179 | * cleared. It is illegal to call this function with a non-NULL strm->srv_conn. |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 180 | * |
| 181 | * This function must be called by the stream itself, so in the context of |
| 182 | * process_stream. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 183 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 184 | struct pendconn *pendconn_add(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 185 | { |
| 186 | struct pendconn *p; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 187 | struct proxy *px; |
| 188 | struct server *srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 189 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 190 | p = pool_alloc(pool_head_pendconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 191 | if (!p) |
| 192 | return NULL; |
| 193 | |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 194 | srv = objt_server(strm->target); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 195 | px = strm->be; |
| 196 | |
| 197 | p->srv = NULL; |
| 198 | p->px = px; |
| 199 | p->strm = strm; |
| 200 | p->strm_flags = strm->flags; |
| 201 | HA_SPIN_INIT(&p->lock); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 202 | |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 203 | if ((strm->flags & SF_ASSIGNED) && srv) { |
| 204 | p->srv = srv; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 205 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 206 | srv->nbpend++; |
| 207 | strm->logs.srv_queue_size += srv->nbpend; |
| 208 | if (srv->nbpend > srv->counters.nbpend_max) |
| 209 | srv->counters.nbpend_max = srv->nbpend; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 210 | LIST_ADDQ(&srv->pendconns, &p->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 211 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 212 | } |
| 213 | else { |
| 214 | HA_SPIN_LOCK(PROXY_LOCK, &px->lock); |
| 215 | px->nbpend++; |
| 216 | strm->logs.prx_queue_size += px->nbpend; |
| 217 | if (px->nbpend > px->be_counters.nbpend_max) |
| 218 | px->be_counters.nbpend_max = px->nbpend; |
| 219 | LIST_ADDQ(&px->pendconns, &p->list); |
| 220 | HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 221 | } |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 222 | HA_ATOMIC_ADD(&px->totpend, 1); |
| 223 | strm->pend_pos = p; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 224 | return p; |
| 225 | } |
| 226 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 227 | /* Redistribute pending connections when a server goes down. The number of |
| 228 | * connections redistributed is returned. |
| 229 | */ |
| 230 | int pendconn_redistribute(struct server *s) |
| 231 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 232 | struct pendconn *p, *pback; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 233 | int xferred = 0; |
| 234 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 235 | /* The REDISP option was specified. We will ignore cookie and force to |
| 236 | * balance or use the dispatcher. */ |
| 237 | if ((s->proxy->options & (PR_O_REDISP|PR_O_PERSIST)) != PR_O_REDISP) |
| 238 | return 0; |
| 239 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 240 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 241 | list_for_each_entry_safe(p, pback, &s->pendconns, list) { |
| 242 | if (p->strm_flags & SF_FORCE_PRST) |
| 243 | continue; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 244 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 245 | if (HA_SPIN_TRYLOCK(PENDCONN_LOCK, &p->lock)) |
| 246 | continue; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 247 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 248 | /* it's left to the dispatcher to choose a server */ |
| 249 | pendconn_unlink(p); |
| 250 | p->strm_flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 251 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 252 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
| 253 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 254 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 255 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 256 | return xferred; |
| 257 | } |
| 258 | |
| 259 | /* Check for pending connections at the backend, and assign some of them to |
| 260 | * the server coming up. The server's weight is checked before being assigned |
| 261 | * connections it may not be able to handle. The total number of transferred |
| 262 | * connections is returned. |
| 263 | */ |
| 264 | int pendconn_grab_from_px(struct server *s) |
| 265 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 266 | struct pendconn *p, *pback; |
| 267 | int maxconn, xferred = 0; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 268 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 269 | if (!srv_currently_usable(s)) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 270 | return 0; |
| 271 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 272 | HA_SPIN_LOCK(PROXY_LOCK, &s->proxy->lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 273 | maxconn = srv_dynamic_maxconn(s); |
| 274 | list_for_each_entry_safe(p, pback, &s->proxy->pendconns, list) { |
| 275 | if (s->maxconn && s->served + xferred >= maxconn) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 276 | break; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 277 | |
| 278 | if (HA_SPIN_TRYLOCK(PENDCONN_LOCK, &p->lock)) |
| 279 | continue; |
| 280 | |
| 281 | pendconn_unlink(p); |
| 282 | p->srv = s; |
| 283 | |
| 284 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
| 285 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
| 286 | xferred++; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 287 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 288 | HA_SPIN_UNLOCK(PROXY_LOCK, &s->proxy->lock); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 289 | return xferred; |
| 290 | } |
| 291 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 292 | /* Try to dequeue pending connection attached to the stream <strm>. It must |
| 293 | * always exists here. If the pendconn is still linked to the server or the |
| 294 | * proxy queue, nothing is done and the function returns 1. Otherwise, |
| 295 | * <strm>->flags and <strm>->target are updated, the pendconn is released and 0 |
| 296 | * is returned. |
| 297 | * |
| 298 | * This function must be called by the stream itself, so in the context of |
| 299 | * process_stream. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 300 | */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 301 | int pendconn_dequeue(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 302 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 303 | struct pendconn *p; |
| 304 | |
| 305 | if (unlikely(!strm->pend_pos)) { |
| 306 | /* unexpected case because it is called by the stream itself and |
| 307 | * only the stream can release a pendconn. So it is only |
| 308 | * possible if a pendconn is released by someone else or if the |
| 309 | * stream is supposed to be queued but without its associated |
| 310 | * pendconn. In both cases it is a bug! */ |
| 311 | abort(); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 312 | } |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 313 | p = strm->pend_pos; |
| 314 | HA_SPIN_LOCK(PENDCONN_LOCK, &p->lock); |
| 315 | |
| 316 | /* the pendconn is still linked to the server/proxy queue, so unlock it |
| 317 | * and go away. */ |
| 318 | if (!LIST_ISEMPTY(&p->list)) { |
| 319 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
| 320 | return 1; |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 321 | } |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 322 | |
| 323 | /* the pendconn must be dequeued now */ |
| 324 | if (p->srv) |
| 325 | strm->target = &p->srv->obj_type; |
| 326 | |
| 327 | strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
| 328 | strm->flags |= p->strm_flags & (SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
| 329 | strm->pend_pos = NULL; |
| 330 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 331 | pool_free(pool_head_pendconn, p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 332 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 335 | /* Release the pending connection <p>, and decreases the pending count if |
| 336 | * needed. The connection might have been queued to a specific server as well as |
| 337 | * to the proxy. The stream also gets marked unqueued. <p> must always be |
| 338 | * defined here. So it is the caller responsibility to check its existance. |
| 339 | * |
| 340 | * This function must be called by the stream itself, so in the context of |
| 341 | * process_stream. |
| 342 | */ |
| 343 | void pendconn_free(struct pendconn *p) |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 344 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 345 | struct stream *strm = p->strm; |
| 346 | |
| 347 | HA_SPIN_LOCK(PENDCONN_LOCK, &p->lock); |
| 348 | |
| 349 | /* The pendconn was already unlinked, just release it. */ |
| 350 | if (LIST_ISEMPTY(&p->list)) |
| 351 | goto release; |
| 352 | |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 353 | if (p->srv) { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 354 | HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock); |
| 355 | p->srv->nbpend--; |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 356 | LIST_DEL(&p->list); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 357 | HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 358 | } |
| 359 | else { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 360 | HA_SPIN_LOCK(PROXY_LOCK, &p->px->lock); |
| 361 | p->px->nbpend--; |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 362 | LIST_DEL(&p->list); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 363 | HA_SPIN_UNLOCK(PROXY_LOCK, &p->px->lock); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 364 | } |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 365 | HA_ATOMIC_SUB(&p->px->totpend, 1); |
| 366 | |
| 367 | release: |
| 368 | strm->pend_pos = NULL; |
| 369 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 370 | pool_free(pool_head_pendconn, p); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 371 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 372 | |
| 373 | /* |
| 374 | * Local variables: |
| 375 | * c-indent-level: 8 |
| 376 | * c-basic-offset: 8 |
| 377 | * End: |
| 378 | */ |