blob: 57fd087bfe10084a6c7d8cda50b3f8a1ed28abf6 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Queue management functions.
3 *
Willy Tarreauac68c5d2009-10-04 23:12:44 +02004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 Tarreau6bdd05c2018-07-25 15:21:00 +020013/* Short explanation on the locking, which is far from being trivial : a
14 * pendconn is a list element which necessarily is associated with an existing
15 * stream. It has pendconn->strm always valid. A pendconn may only be in one of
16 * these three states :
17 * - unlinked : in this case it is an empty list head ;
18 * - linked into the server's queue ;
19 * - linked into the proxy's queue.
20 *
21 * A stream does not necessarily have such a pendconn. Thus the pendconn is
22 * designated by the stream->pend_pos pointer. This results in some properties :
23 * - pendconn->strm->pend_pos is never NULL for any valid pendconn
24 * - if LIST_ISEMPTY(pendconn->list) is true, the element is unlinked,
25 * otherwise it necessarily belongs to one of the other lists ; this may
26 * not be atomically checked under threads though ;
27 * - pendconn->px is never NULL if pendconn->list is not empty
Willy Tarreau88930dd2018-07-26 07:38:54 +020028 * - pendconn->srv is never NULL if pendconn->list is in the server's queue,
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020029 * and is always NULL if pendconn->list is in the backend's queue or empty.
Willy Tarreau88930dd2018-07-26 07:38:54 +020030 * - pendconn->target is NULL while the element is queued, and points to the
31 * assigned server when the pendconn is picked.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020032 *
33 * Threads complicate the design a little bit but rules remain simple :
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020034 * - the server's queue lock must be held at least when manipulating the
35 * server's queue, which is when adding a pendconn to the queue and when
36 * removing a pendconn from the queue. It protects the queue's integrity.
37 *
38 * - the proxy's queue lock must be held at least when manipulating the
39 * proxy's queue, which is when adding a pendconn to the queue and when
40 * removing a pendconn from the queue. It protects the queue's integrity.
41 *
Willy Tarreau3201e4e2018-07-26 08:23:24 +020042 * - both locks are compatible and may be held at the same time.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020043 *
44 * - a pendconn_add() is only performed by the stream which will own the
45 * pendconn ; the pendconn is allocated at this moment and returned ; it is
46 * added to either the server or the proxy's queue while holding this
Willy Tarreau3201e4e2018-07-26 08:23:24 +020047 * queue's lock.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020048 *
49 * - the pendconn is then met by a thread walking over the proxy or server's
50 * queue with the respective lock held. This lock is exclusive and the
51 * pendconn can only appear in one queue so by definition a single thread
52 * may find this pendconn at a time.
53 *
54 * - the pendconn is unlinked either by its own stream upon success/abort/
55 * free, or by another one offering it its server slot. This is achieved by
56 * pendconn_process_next_strm() under either the server or proxy's lock,
57 * pendconn_redistribute() under the server's lock, pendconn_grab_from_px()
58 * under the proxy's lock, or pendconn_unlink() under either the proxy's or
59 * the server's lock depending on the queue the pendconn is attached to.
60 *
61 * - no single operation except the pendconn initialisation prior to the
Willy Tarreau3201e4e2018-07-26 08:23:24 +020062 * insertion are performed without eithre a queue lock held or the element
63 * being unlinked and visible exclusively to its stream.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020064 *
Willy Tarreau88930dd2018-07-26 07:38:54 +020065 * - pendconn_grab_from_px() and pendconn_process_next_strm() assign ->target
66 * so that the stream knows what server to work with (via
67 * pendconn_dequeue() which sets it on strm->target).
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020068 *
69 * - a pendconn doesn't switch between queues, it stays where it is.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020070 */
71
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020072#include <common/config.h>
Willy Tarreaue4d7e552007-05-13 20:19:55 +020073#include <common/memory.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020074#include <common/time.h>
Christopher Faulet8ba59142017-06-27 15:43:53 +020075#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020076
Willy Tarreaubaaee002006-06-26 02:48:02 +020077#include <proto/queue.h>
78#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020079#include <proto/stream.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010080#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020081#include <proto/task.h>
82
83
Willy Tarreaubafbe012017-11-24 17:34:44 +010084struct pool_head *pool_head_pendconn;
Willy Tarreaue4d7e552007-05-13 20:19:55 +020085
86/* perform minimal intializations, report 0 in case of error, 1 if OK. */
87int init_pendconn()
88{
Willy Tarreaubafbe012017-11-24 17:34:44 +010089 pool_head_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED);
90 return pool_head_pendconn != NULL;
Willy Tarreaue4d7e552007-05-13 20:19:55 +020091}
Willy Tarreaubaaee002006-06-26 02:48:02 +020092
93/* returns the effective dynamic maxconn for a server, considering the minconn
Willy Tarreau86034312006-12-29 00:10:33 +010094 * and the proxy's usage relative to its dynamic connections limit. It is
Willy Tarreau9909fc12007-11-30 17:42:05 +010095 * expected that 0 < s->minconn <= s->maxconn when this is called. If the
96 * server is currently warming up, the slowstart is also applied to the
97 * resulting value, which can be lower than minconn in this case, but never
98 * less than 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200100unsigned int srv_dynamic_maxconn(const struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101{
Willy Tarreau9909fc12007-11-30 17:42:05 +0100102 unsigned int max;
103
Willy Tarreau86034312006-12-29 00:10:33 +0100104 if (s->proxy->beconn >= s->proxy->fullconn)
105 /* no fullconn or proxy is full */
Willy Tarreau9909fc12007-11-30 17:42:05 +0100106 max = s->maxconn;
107 else if (s->minconn == s->maxconn)
Willy Tarreau86034312006-12-29 00:10:33 +0100108 /* static limit */
Willy Tarreau9909fc12007-11-30 17:42:05 +0100109 max = s->maxconn;
110 else max = MAX(s->minconn,
111 s->proxy->beconn * s->maxconn / s->proxy->fullconn);
Willy Tarreau86034312006-12-29 00:10:33 +0100112
Emeric Brun52a91d32017-08-31 14:41:55 +0200113 if ((s->cur_state == SRV_ST_STARTING) &&
Willy Tarreau9909fc12007-11-30 17:42:05 +0100114 now.tv_sec < s->last_change + s->slowstart &&
115 now.tv_sec >= s->last_change) {
116 unsigned int ratio;
Willy Tarreau28a9e522008-09-14 17:43:27 +0200117 ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart;
118 max = MAX(1, max * ratio / 100);
Willy Tarreau9909fc12007-11-30 17:42:05 +0100119 }
120 return max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121}
122
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100123/* Remove the pendconn from the server/proxy queue. At this stage, the
124 * connection is not really dequeued. It will be done during the
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200125 * process_stream. It also decreases the pending count.
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100126 *
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200127 * The caller must own the lock on the queue containing the pendconn. The
128 * pendconn must still be queued.
Christopher Fauletf3a55db2017-06-09 14:26:38 +0200129 */
Willy Tarreau9624fae2018-07-25 08:04:20 +0200130static void __pendconn_unlink(struct pendconn *p)
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100131{
132 if (p->srv)
133 p->srv->nbpend--;
134 else
135 p->px->nbpend--;
136 HA_ATOMIC_SUB(&p->px->totpend, 1);
137 LIST_DEL(&p->list);
138 LIST_INIT(&p->list);
Christopher Fauletf3a55db2017-06-09 14:26:38 +0200139}
140
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200141/* Locks the queue the pendconn element belongs to. This relies on both p->px
142 * and p->srv to be properly initialized (which is always the case once the
143 * element has been added).
144 */
145static inline void pendconn_queue_lock(struct pendconn *p)
146{
147 if (p->srv)
148 HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock);
149 else
150 HA_SPIN_LOCK(PROXY_LOCK, &p->px->lock);
151}
152
153/* Unlocks the queue the pendconn element belongs to. This relies on both p->px
154 * and p->srv to be properly initialized (which is always the case once the
155 * element has been added).
156 */
157static inline void pendconn_queue_unlock(struct pendconn *p)
158{
159 if (p->srv)
160 HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock);
161 else
162 HA_SPIN_UNLOCK(PROXY_LOCK, &p->px->lock);
163}
164
Willy Tarreau9624fae2018-07-25 08:04:20 +0200165/* Removes the pendconn from the server/proxy queue. At this stage, the
166 * connection is not really dequeued. It will be done during process_stream().
167 * This function takes all the required locks for the operation. The caller is
168 * responsible for ensuring that <p> is valid and still in the queue. Use
169 * pendconn_cond_unlink() if unsure. When the locks are already held, please
170 * use __pendconn_unlink() instead.
171 */
172void pendconn_unlink(struct pendconn *p)
173{
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200174 pendconn_queue_lock(p);
Willy Tarreau9624fae2018-07-25 08:04:20 +0200175
176 __pendconn_unlink(p);
177
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200178 pendconn_queue_unlock(p);
Willy Tarreau9624fae2018-07-25 08:04:20 +0200179}
180
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100181/* Process the next pending connection from either a server or a proxy, and
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100182 * returns a strictly positive value on success (see below). If no pending
183 * connection is found, 0 is returned. Note that neither <srv> nor <px> may be
184 * NULL. Priority is given to the oldest request in the queue if both <srv> and
185 * <px> have pending requests. This ensures that no request will be left
186 * unserved. The <px> queue is not considered if the server (or a tracked
187 * server) is not RUNNING, is disabled, or has a null weight (server going
188 * down). The <srv> queue is still considered in this case, because if some
189 * connections remain there, it means that some requests have been forced there
190 * after it was seen down (eg: due to option persist). The stream is
191 * immediately marked as "assigned", and both its <srv> and <srv_conn> are set
192 * to <srv>.
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100193 *
194 * This function must only be called if the server queue _AND_ the proxy queue
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100195 * are locked. Today it is only called by process_srv_queue. When a pending
196 * connection is dequeued, this function returns 1 if the pending connection can
197 * be handled by the current thread, else it returns 2.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200198 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100199static int pendconn_process_next_strm(struct server *srv, struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200200{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100201 struct pendconn *p = NULL;
202 struct server *rsrv;
Willy Tarreaud132f742010-08-06 10:08:23 +0200203
Willy Tarreau44267702011-10-28 15:35:33 +0200204 rsrv = srv->track;
Willy Tarreaud132f742010-08-06 10:08:23 +0200205 if (!rsrv)
206 rsrv = srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200207
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200208 p = NULL;
209 if (srv->nbpend)
210 p = LIST_ELEM(srv->pendconns.n, struct pendconn *, list);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200211
Willy Tarreaua8694652018-08-07 10:44:58 +0200212 if (srv_currently_usable(rsrv) && px->nbpend &&
213 (!(srv->flags & SRV_F_BACKUP) ||
214 (!px->srv_act &&
215 (srv == px->lbprm.fbck || (px->options & PR_O_USE_ALL_BK))))) {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100216 struct pendconn *pp;
217
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200218 pp = LIST_ELEM(px->pendconns.n, struct pendconn *, list);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100219
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200220 /* If the server pendconn is older than the proxy one,
221 * we process the server one.
222 */
223 if (p && !tv_islt(&pp->strm->logs.tv_request, &p->strm->logs.tv_request))
224 goto pendconn_found;
225
226 /* Let's switch from the server pendconn to the proxy pendconn */
227 p = pp;
228 goto pendconn_found;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100229 }
230
231 if (!p)
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100232 return 0;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100233
234 pendconn_found:
Willy Tarreau9624fae2018-07-25 08:04:20 +0200235 __pendconn_unlink(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100236 p->strm_flags |= SF_ASSIGNED;
Willy Tarreau88930dd2018-07-26 07:38:54 +0200237 p->target = srv;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100238
Christopher Faulet29f77e82017-06-08 14:04:45 +0200239 HA_ATOMIC_ADD(&srv->served, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200240 HA_ATOMIC_ADD(&srv->proxy->served, 1);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200241 if (px->lbprm.server_take_conn)
242 px->lbprm.server_take_conn(srv);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100243 __stream_add_srv_conn(p->strm, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200244
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100245 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100246
Olivier Houchardecfe6732018-07-26 18:47:27 +0200247 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200248}
249
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100250/* Manages a server's connection queue. This function will try to dequeue as
Christopher Faulet87566c92017-06-06 10:34:51 +0200251 * many pending streams as possible, and wake them up.
252 */
253void process_srv_queue(struct server *s)
254{
255 struct proxy *p = s->proxy;
Olivier Houchardecfe6732018-07-26 18:47:27 +0200256 int maxconn;
Christopher Faulet87566c92017-06-06 10:34:51 +0200257
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100258 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
259 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet87566c92017-06-06 10:34:51 +0200260 maxconn = srv_dynamic_maxconn(s);
261 while (s->served < maxconn) {
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100262 int ret = pendconn_process_next_strm(s, p);
263 if (!ret)
Christopher Faulet87566c92017-06-06 10:34:51 +0200264 break;
Christopher Faulet87566c92017-06-06 10:34:51 +0200265 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100266 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
267 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Christopher Faulet87566c92017-06-06 10:34:51 +0200268}
269
Willy Tarreau87b09662015-04-03 00:22:06 +0200270/* Adds the stream <strm> to the pending connection list of server <strm>->srv
271 * or to the one of <strm>->proxy if srv is NULL. All counters and back pointers
Willy Tarreaubaaee002006-06-26 02:48:02 +0200272 * are updated accordingly. Returns NULL if no memory is available, otherwise the
Willy Tarreau87b09662015-04-03 00:22:06 +0200273 * pendconn itself. If the stream was already marked as served, its flag is
274 * cleared. It is illegal to call this function with a non-NULL strm->srv_conn.
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100275 *
276 * This function must be called by the stream itself, so in the context of
277 * process_stream.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200278 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200279struct pendconn *pendconn_add(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200280{
281 struct pendconn *p;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100282 struct proxy *px;
283 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200284
Willy Tarreaubafbe012017-11-24 17:34:44 +0100285 p = pool_alloc(pool_head_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200286 if (!p)
287 return NULL;
288
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200289 if (strm->flags & SF_ASSIGNED)
290 srv = objt_server(strm->target);
291 else
292 srv = NULL;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100293
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200294 px = strm->be;
Willy Tarreau88930dd2018-07-26 07:38:54 +0200295 p->target = NULL;
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200296 p->srv = srv;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100297 p->px = px;
298 p->strm = strm;
299 p->strm_flags = strm->flags;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200300
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200301 pendconn_queue_lock(p);
302
303 if (srv) {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100304 srv->nbpend++;
305 strm->logs.srv_queue_size += srv->nbpend;
306 if (srv->nbpend > srv->counters.nbpend_max)
307 srv->counters.nbpend_max = srv->nbpend;
Willy Tarreau827aee92011-03-10 16:55:02 +0100308 LIST_ADDQ(&srv->pendconns, &p->list);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100309 }
310 else {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100311 px->nbpend++;
312 strm->logs.prx_queue_size += px->nbpend;
313 if (px->nbpend > px->be_counters.nbpend_max)
314 px->be_counters.nbpend_max = px->nbpend;
315 LIST_ADDQ(&px->pendconns, &p->list);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200316 }
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200317 strm->pend_pos = p;
318
319 pendconn_queue_unlock(p);
320
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100321 HA_ATOMIC_ADD(&px->totpend, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200322 return p;
323}
324
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200325/* Redistribute pending connections when a server goes down. The number of
326 * connections redistributed is returned.
327 */
328int pendconn_redistribute(struct server *s)
329{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100330 struct pendconn *p, *pback;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200331 int xferred = 0;
332
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100333 /* The REDISP option was specified. We will ignore cookie and force to
334 * balance or use the dispatcher. */
335 if ((s->proxy->options & (PR_O_REDISP|PR_O_PERSIST)) != PR_O_REDISP)
336 return 0;
337
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100338 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100339 list_for_each_entry_safe(p, pback, &s->pendconns, list) {
340 if (p->strm_flags & SF_FORCE_PRST)
341 continue;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200342
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100343 /* it's left to the dispatcher to choose a server */
Willy Tarreau9624fae2018-07-25 08:04:20 +0200344 __pendconn_unlink(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100345 p->strm_flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200346
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100347 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200348 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100349 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200350 return xferred;
351}
352
353/* Check for pending connections at the backend, and assign some of them to
354 * the server coming up. The server's weight is checked before being assigned
355 * connections it may not be able to handle. The total number of transferred
356 * connections is returned.
357 */
358int pendconn_grab_from_px(struct server *s)
359{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100360 struct pendconn *p, *pback;
361 int maxconn, xferred = 0;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200362
Emeric Brun52a91d32017-08-31 14:41:55 +0200363 if (!srv_currently_usable(s))
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200364 return 0;
365
Willy Tarreaua8694652018-08-07 10:44:58 +0200366 /* if this is a backup server and there are active servers or at
367 * least another backup server was elected, then this one must
368 * not dequeue requests from the proxy.
369 */
370 if ((s->flags & SRV_F_BACKUP) &&
371 (s->proxy->srv_act ||
372 ((s != s->proxy->lbprm.fbck) && !(s->proxy->options & PR_O_USE_ALL_BK))))
373 return 0;
374
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100375 HA_SPIN_LOCK(PROXY_LOCK, &s->proxy->lock);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100376 maxconn = srv_dynamic_maxconn(s);
377 list_for_each_entry_safe(p, pback, &s->proxy->pendconns, list) {
378 if (s->maxconn && s->served + xferred >= maxconn)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200379 break;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100380
Willy Tarreau9624fae2018-07-25 08:04:20 +0200381 __pendconn_unlink(p);
Willy Tarreau88930dd2018-07-26 07:38:54 +0200382 p->target = s;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100383
384 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100385 xferred++;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200386 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100387 HA_SPIN_UNLOCK(PROXY_LOCK, &s->proxy->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200388 return xferred;
389}
390
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100391/* Try to dequeue pending connection attached to the stream <strm>. It must
392 * always exists here. If the pendconn is still linked to the server or the
393 * proxy queue, nothing is done and the function returns 1. Otherwise,
394 * <strm>->flags and <strm>->target are updated, the pendconn is released and 0
395 * is returned.
396 *
397 * This function must be called by the stream itself, so in the context of
398 * process_stream.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200399 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100400int pendconn_dequeue(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200401{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100402 struct pendconn *p;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200403 int is_unlinked;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100404
405 if (unlikely(!strm->pend_pos)) {
406 /* unexpected case because it is called by the stream itself and
407 * only the stream can release a pendconn. So it is only
408 * possible if a pendconn is released by someone else or if the
409 * stream is supposed to be queued but without its associated
410 * pendconn. In both cases it is a bug! */
411 abort();
Christopher Faulet8ba59142017-06-27 15:43:53 +0200412 }
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100413 p = strm->pend_pos;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200414
415 /* note below : we need to grab the queue's lock to check for emptiness
416 * because we don't want a partial _grab_from_px() or _redistribute()
417 * to be called in parallel and show an empty list without having the
418 * time to finish. With this we know that if we see the element
419 * unlinked, these functions were completely done.
420 */
421 pendconn_queue_lock(p);
422 is_unlinked = LIST_ISEMPTY(&p->list);
423 pendconn_queue_unlock(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100424
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200425 if (!is_unlinked)
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100426 return 1;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100427
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200428 /* the pendconn is not queued anymore and will not be so we're safe
429 * to proceed.
430 */
Willy Tarreau88930dd2018-07-26 07:38:54 +0200431 if (p->target)
432 strm->target = &p->target->obj_type;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100433
434 strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
435 strm->flags |= p->strm_flags & (SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
436 strm->pend_pos = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100437 pool_free(pool_head_pendconn, p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100438 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439}
440
Willy Tarreaubaaee002006-06-26 02:48:02 +0200441/*
442 * Local variables:
443 * c-indent-level: 8
444 * c-basic-offset: 8
445 * End:
446 */