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 | |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 27 | static void __pendconn_free(struct pendconn *p); |
| 28 | |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 29 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 30 | int init_pendconn() |
| 31 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 32 | pool_head_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED); |
| 33 | return pool_head_pendconn != NULL; |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 34 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | |
| 36 | /* returns the effective dynamic maxconn for a server, considering the minconn |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 37 | * 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] | 38 | * expected that 0 < s->minconn <= s->maxconn when this is called. If the |
| 39 | * server is currently warming up, the slowstart is also applied to the |
| 40 | * resulting value, which can be lower than minconn in this case, but never |
| 41 | * less than 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 43 | unsigned int srv_dynamic_maxconn(const struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | { |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 45 | unsigned int max; |
| 46 | |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 47 | if (s->proxy->beconn >= s->proxy->fullconn) |
| 48 | /* no fullconn or proxy is full */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 49 | max = s->maxconn; |
| 50 | else if (s->minconn == s->maxconn) |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 51 | /* static limit */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 52 | max = s->maxconn; |
| 53 | else max = MAX(s->minconn, |
| 54 | s->proxy->beconn * s->maxconn / s->proxy->fullconn); |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 55 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 56 | if ((s->cur_state == SRV_ST_STARTING) && |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 57 | now.tv_sec < s->last_change + s->slowstart && |
| 58 | now.tv_sec >= s->last_change) { |
| 59 | unsigned int ratio; |
Willy Tarreau | 28a9e52 | 2008-09-14 17:43:27 +0200 | [diff] [blame] | 60 | ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart; |
| 61 | max = MAX(1, max * ratio / 100); |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 62 | } |
| 63 | return max; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
Christopher Faulet | f3a55db | 2017-06-09 14:26:38 +0200 | [diff] [blame] | 67 | /* Returns the first pending connection for server <s>, which may be NULL if |
| 68 | * nothing is pending. |
| 69 | */ |
| 70 | static inline struct pendconn *pendconn_from_srv(const struct server *s) { |
| 71 | if (!s->nbpend) |
| 72 | return NULL; |
| 73 | return LIST_ELEM(s->pendconns.n, struct pendconn *, list); |
| 74 | } |
| 75 | |
| 76 | /* Returns the first pending connection for proxy <px>, which may be NULL if |
| 77 | * nothing is pending. |
| 78 | */ |
| 79 | static inline struct pendconn *pendconn_from_px(const struct proxy *px) { |
| 80 | if (!px->nbpend) |
| 81 | return NULL; |
| 82 | |
| 83 | return LIST_ELEM(px->pendconns.n, struct pendconn *, list); |
| 84 | } |
| 85 | |
| 86 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 87 | /* Detaches the next pending connection from either a server or a proxy, and |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 88 | * returns its associated stream. If no pending connection is found, NULL is |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 89 | * returned. Note that neither <srv> nor <px> may be NULL. |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 90 | * Priority is given to the oldest request in the queue if both <srv> and <px> |
| 91 | * have pending requests. This ensures that no request will be left unserved. |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 92 | * The <px> queue is not considered if the server (or a tracked server) is not |
| 93 | * RUNNING, is disabled, or has a null weight (server going down). The <srv> |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 94 | * queue is still considered in this case, because if some connections remain |
| 95 | * there, it means that some requests have been forced there after it was seen |
| 96 | * down (eg: due to option persist). |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 97 | * The stream is immediately marked as "assigned", and both its <srv> and |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 98 | * <srv_conn> are set to <srv>, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | */ |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 100 | static struct stream *pendconn_get_next_strm(struct server *srv, struct proxy *px) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | { |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 102 | struct pendconn *ps, *pp; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 103 | struct stream *strm; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 104 | struct server *rsrv; |
| 105 | |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 106 | rsrv = srv->track; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 107 | if (!rsrv) |
| 108 | rsrv = srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 110 | ps = pendconn_from_srv(srv); |
| 111 | pp = pendconn_from_px(px); |
| 112 | /* we want to get the definitive pendconn in <ps> */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 113 | if (!pp || !srv_currently_usable(rsrv)) { |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 114 | if (!ps) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 115 | return NULL; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 116 | } else { |
| 117 | /* pendconn exists in the proxy queue */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 118 | if (!ps || tv_islt(&pp->strm->logs.tv_request, &ps->strm->logs.tv_request)) |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 119 | ps = pp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 120 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 121 | strm = ps->strm; |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 122 | __pendconn_free(ps); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 123 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 124 | /* we want to note that the stream has now been assigned a server */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 125 | strm->flags |= SF_ASSIGNED; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 126 | strm->target = &srv->obj_type; |
Willy Tarreau | 103e566 | 2017-11-26 18:48:14 +0100 | [diff] [blame] | 127 | __stream_add_srv_conn(strm, srv); |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 128 | HA_ATOMIC_ADD(&srv->served, 1); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 129 | HA_ATOMIC_ADD(&srv->proxy->served, 1); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 130 | if (px->lbprm.server_take_conn) |
| 131 | px->lbprm.server_take_conn(srv); |
| 132 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 133 | return strm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 136 | /* |
| 137 | * Manages a server's connection queue. This function will try to dequeue as |
| 138 | * many pending streams as possible, and wake them up. |
| 139 | */ |
| 140 | void process_srv_queue(struct server *s) |
| 141 | { |
| 142 | struct proxy *p = s->proxy; |
| 143 | int maxconn; |
| 144 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 145 | HA_SPIN_LOCK(PROXY_LOCK, &p->lock); |
| 146 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 147 | |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 148 | /* First, check if we can handle some connections queued at the proxy. We |
| 149 | * will take as many as we can handle. |
| 150 | */ |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 151 | maxconn = srv_dynamic_maxconn(s); |
| 152 | while (s->served < maxconn) { |
| 153 | struct stream *strm = pendconn_get_next_strm(s, p); |
| 154 | |
| 155 | if (strm == NULL) |
| 156 | break; |
| 157 | task_wakeup(strm->task, TASK_WOKEN_RES); |
| 158 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 159 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 160 | HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 163 | /* Adds the stream <strm> to the pending connection list of server <strm>->srv |
| 164 | * 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] | 165 | * are updated accordingly. Returns NULL if no memory is available, otherwise the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 166 | * pendconn itself. If the stream was already marked as served, its flag is |
| 167 | * cleared. It is illegal to call this function with a non-NULL strm->srv_conn. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 168 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 169 | struct pendconn *pendconn_add(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 170 | { |
| 171 | struct pendconn *p; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 172 | struct server *srv; |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 173 | int count; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 174 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 175 | p = pool_alloc(pool_head_pendconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 176 | if (!p) |
| 177 | return NULL; |
| 178 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 179 | strm->pend_pos = p; |
| 180 | p->strm = strm; |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 181 | srv = objt_server(strm->target); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 182 | |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 183 | if ((strm->flags & SF_ASSIGNED) && srv) { |
| 184 | p->srv = srv; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 185 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 186 | LIST_ADDQ(&srv->pendconns, &p->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 187 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 188 | count = HA_ATOMIC_ADD(&srv->nbpend, 1); |
| 189 | strm->logs.srv_queue_size += count; |
| 190 | HA_ATOMIC_UPDATE_MAX(&srv->counters.nbpend_max, count); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 191 | } else { |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 192 | p->srv = NULL; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 193 | HA_SPIN_LOCK(PROXY_LOCK, &strm->be->lock); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 194 | LIST_ADDQ(&strm->be->pendconns, &p->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 195 | HA_SPIN_UNLOCK(PROXY_LOCK, &strm->be->lock); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 196 | count = HA_ATOMIC_ADD(&strm->be->nbpend, 1); |
| 197 | strm->logs.prx_queue_size += count; |
| 198 | HA_ATOMIC_UPDATE_MAX(&strm->be->be_counters.nbpend_max, count); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 199 | } |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 200 | HA_ATOMIC_ADD(&strm->be->totpend, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 201 | return p; |
| 202 | } |
| 203 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 204 | /* Redistribute pending connections when a server goes down. The number of |
| 205 | * connections redistributed is returned. |
| 206 | */ |
| 207 | int pendconn_redistribute(struct server *s) |
| 208 | { |
| 209 | struct pendconn *pc, *pc_bck; |
| 210 | int xferred = 0; |
| 211 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 212 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 213 | list_for_each_entry_safe(pc, pc_bck, &s->pendconns, list) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 214 | struct stream *strm = pc->strm; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 215 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 216 | if ((strm->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 217 | !(strm->flags & SF_FORCE_PRST)) { |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 218 | /* The REDISP option was specified. We will ignore |
| 219 | * cookie and force to balance or use the dispatcher. |
| 220 | */ |
| 221 | |
| 222 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 223 | strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 224 | |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 225 | __pendconn_free(pc); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 226 | task_wakeup(strm->task, TASK_WOKEN_RES); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 227 | xferred++; |
| 228 | } |
| 229 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 230 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 231 | return xferred; |
| 232 | } |
| 233 | |
| 234 | /* Check for pending connections at the backend, and assign some of them to |
| 235 | * the server coming up. The server's weight is checked before being assigned |
| 236 | * connections it may not be able to handle. The total number of transferred |
| 237 | * connections is returned. |
| 238 | */ |
| 239 | int pendconn_grab_from_px(struct server *s) |
| 240 | { |
| 241 | int xferred; |
| 242 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 243 | if (!srv_currently_usable(s)) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 244 | return 0; |
| 245 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 246 | HA_SPIN_LOCK(PROXY_LOCK, &s->proxy->lock); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 247 | for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 248 | struct stream *strm; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 249 | struct pendconn *p; |
| 250 | |
| 251 | p = pendconn_from_px(s->proxy); |
| 252 | if (!p) |
| 253 | break; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 254 | p->strm->target = &s->obj_type; |
| 255 | strm = p->strm; |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 256 | __pendconn_free(p); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 257 | task_wakeup(strm->task, TASK_WOKEN_RES); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 258 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 259 | HA_SPIN_UNLOCK(PROXY_LOCK, &s->proxy->lock); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 260 | return xferred; |
| 261 | } |
| 262 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 263 | /* |
| 264 | * Detaches pending connection <p>, decreases the pending count, and frees |
| 265 | * the pending connection. The connection might have been queued to a specific |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 266 | * server as well as to the proxy. The stream also gets marked unqueued. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 267 | */ |
| 268 | void pendconn_free(struct pendconn *p) |
| 269 | { |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 270 | if (p->srv) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 271 | HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 272 | LIST_DEL(&p->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 273 | HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 274 | HA_ATOMIC_SUB(&p->srv->nbpend, 1); |
| 275 | } |
| 276 | else { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 277 | HA_SPIN_LOCK(SERVER_LOCK, &p->strm->be->lock); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 278 | LIST_DEL(&p->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 279 | HA_SPIN_UNLOCK(SERVER_LOCK, &p->strm->be->lock); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 280 | HA_ATOMIC_SUB(&p->strm->be->nbpend, 1); |
| 281 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 282 | p->strm->pend_pos = NULL; |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 283 | HA_ATOMIC_SUB(&p->strm->be->totpend, 1); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 284 | pool_free(pool_head_pendconn, p); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 287 | /* Lock-free version of pendconn_free. */ |
| 288 | static void __pendconn_free(struct pendconn *p) |
| 289 | { |
| 290 | if (p->srv) { |
| 291 | LIST_DEL(&p->list); |
| 292 | HA_ATOMIC_SUB(&p->srv->nbpend, 1); |
| 293 | } |
| 294 | else { |
| 295 | LIST_DEL(&p->list); |
| 296 | HA_ATOMIC_SUB(&p->strm->be->nbpend, 1); |
| 297 | } |
| 298 | p->strm->pend_pos = NULL; |
| 299 | HA_ATOMIC_SUB(&p->strm->be->totpend, 1); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 300 | pool_free(pool_head_pendconn, p); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 301 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * Local variables: |
| 305 | * c-indent-level: 8 |
| 306 | * c-basic-offset: 8 |
| 307 | * End: |
| 308 | */ |