blob: 173ef76f64d0d61a386bf355148671e4b05e20b5 [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
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100212 if (srv_currently_usable(rsrv) && px->nbpend) {
213 struct pendconn *pp;
214
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200215 pp = LIST_ELEM(px->pendconns.n, struct pendconn *, list);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100216
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200217 /* If the server pendconn is older than the proxy one,
218 * we process the server one.
219 */
220 if (p && !tv_islt(&pp->strm->logs.tv_request, &p->strm->logs.tv_request))
221 goto pendconn_found;
222
223 /* Let's switch from the server pendconn to the proxy pendconn */
224 p = pp;
225 goto pendconn_found;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100226 }
227
228 if (!p)
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100229 return 0;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100230
231 pendconn_found:
Willy Tarreau9624fae2018-07-25 08:04:20 +0200232 __pendconn_unlink(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100233 p->strm_flags |= SF_ASSIGNED;
Willy Tarreau88930dd2018-07-26 07:38:54 +0200234 p->target = srv;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100235
Christopher Faulet29f77e82017-06-08 14:04:45 +0200236 HA_ATOMIC_ADD(&srv->served, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200237 HA_ATOMIC_ADD(&srv->proxy->served, 1);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200238 if (px->lbprm.server_take_conn)
239 px->lbprm.server_take_conn(srv);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100240 __stream_add_srv_conn(p->strm, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200241
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100242 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100243
Olivier Houchardecfe6732018-07-26 18:47:27 +0200244 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245}
246
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100247/* Manages a server's connection queue. This function will try to dequeue as
Christopher Faulet87566c92017-06-06 10:34:51 +0200248 * many pending streams as possible, and wake them up.
249 */
250void process_srv_queue(struct server *s)
251{
252 struct proxy *p = s->proxy;
Olivier Houchardecfe6732018-07-26 18:47:27 +0200253 int maxconn;
Christopher Faulet87566c92017-06-06 10:34:51 +0200254
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100255 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
256 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet87566c92017-06-06 10:34:51 +0200257 maxconn = srv_dynamic_maxconn(s);
258 while (s->served < maxconn) {
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100259 int ret = pendconn_process_next_strm(s, p);
260 if (!ret)
Christopher Faulet87566c92017-06-06 10:34:51 +0200261 break;
Christopher Faulet87566c92017-06-06 10:34:51 +0200262 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100263 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
264 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Christopher Faulet87566c92017-06-06 10:34:51 +0200265}
266
Willy Tarreau87b09662015-04-03 00:22:06 +0200267/* Adds the stream <strm> to the pending connection list of server <strm>->srv
268 * or to the one of <strm>->proxy if srv is NULL. All counters and back pointers
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269 * are updated accordingly. Returns NULL if no memory is available, otherwise the
Willy Tarreau87b09662015-04-03 00:22:06 +0200270 * pendconn itself. If the stream was already marked as served, its flag is
271 * cleared. It is illegal to call this function with a non-NULL strm->srv_conn.
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100272 *
273 * This function must be called by the stream itself, so in the context of
274 * process_stream.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200275 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200276struct pendconn *pendconn_add(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200277{
278 struct pendconn *p;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100279 struct proxy *px;
280 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200281
Willy Tarreaubafbe012017-11-24 17:34:44 +0100282 p = pool_alloc(pool_head_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200283 if (!p)
284 return NULL;
285
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200286 if (strm->flags & SF_ASSIGNED)
287 srv = objt_server(strm->target);
288 else
289 srv = NULL;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100290
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200291 px = strm->be;
Willy Tarreau88930dd2018-07-26 07:38:54 +0200292 p->target = NULL;
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200293 p->srv = srv;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100294 p->px = px;
295 p->strm = strm;
296 p->strm_flags = strm->flags;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200297
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200298 pendconn_queue_lock(p);
299
300 if (srv) {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100301 srv->nbpend++;
302 strm->logs.srv_queue_size += srv->nbpend;
303 if (srv->nbpend > srv->counters.nbpend_max)
304 srv->counters.nbpend_max = srv->nbpend;
Willy Tarreau827aee92011-03-10 16:55:02 +0100305 LIST_ADDQ(&srv->pendconns, &p->list);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100306 }
307 else {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100308 px->nbpend++;
309 strm->logs.prx_queue_size += px->nbpend;
310 if (px->nbpend > px->be_counters.nbpend_max)
311 px->be_counters.nbpend_max = px->nbpend;
312 LIST_ADDQ(&px->pendconns, &p->list);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200313 }
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200314 strm->pend_pos = p;
315
316 pendconn_queue_unlock(p);
317
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100318 HA_ATOMIC_ADD(&px->totpend, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319 return p;
320}
321
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200322/* Redistribute pending connections when a server goes down. The number of
323 * connections redistributed is returned.
324 */
325int pendconn_redistribute(struct server *s)
326{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100327 struct pendconn *p, *pback;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200328 int xferred = 0;
329
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100330 /* The REDISP option was specified. We will ignore cookie and force to
331 * balance or use the dispatcher. */
332 if ((s->proxy->options & (PR_O_REDISP|PR_O_PERSIST)) != PR_O_REDISP)
333 return 0;
334
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100335 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100336 list_for_each_entry_safe(p, pback, &s->pendconns, list) {
337 if (p->strm_flags & SF_FORCE_PRST)
338 continue;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200339
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100340 /* it's left to the dispatcher to choose a server */
Willy Tarreau9624fae2018-07-25 08:04:20 +0200341 __pendconn_unlink(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100342 p->strm_flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200343
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100344 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200345 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100346 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200347 return xferred;
348}
349
350/* Check for pending connections at the backend, and assign some of them to
351 * the server coming up. The server's weight is checked before being assigned
352 * connections it may not be able to handle. The total number of transferred
353 * connections is returned.
354 */
355int pendconn_grab_from_px(struct server *s)
356{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100357 struct pendconn *p, *pback;
358 int maxconn, xferred = 0;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200359
Emeric Brun52a91d32017-08-31 14:41:55 +0200360 if (!srv_currently_usable(s))
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200361 return 0;
362
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100363 HA_SPIN_LOCK(PROXY_LOCK, &s->proxy->lock);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100364 maxconn = srv_dynamic_maxconn(s);
365 list_for_each_entry_safe(p, pback, &s->proxy->pendconns, list) {
366 if (s->maxconn && s->served + xferred >= maxconn)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200367 break;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100368
Willy Tarreau9624fae2018-07-25 08:04:20 +0200369 __pendconn_unlink(p);
Willy Tarreau88930dd2018-07-26 07:38:54 +0200370 p->target = s;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100371
372 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100373 xferred++;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200374 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100375 HA_SPIN_UNLOCK(PROXY_LOCK, &s->proxy->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200376 return xferred;
377}
378
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100379/* Try to dequeue pending connection attached to the stream <strm>. It must
380 * always exists here. If the pendconn is still linked to the server or the
381 * proxy queue, nothing is done and the function returns 1. Otherwise,
382 * <strm>->flags and <strm>->target are updated, the pendconn is released and 0
383 * is returned.
384 *
385 * This function must be called by the stream itself, so in the context of
386 * process_stream.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200387 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100388int pendconn_dequeue(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100390 struct pendconn *p;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200391 int is_unlinked;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100392
393 if (unlikely(!strm->pend_pos)) {
394 /* unexpected case because it is called by the stream itself and
395 * only the stream can release a pendconn. So it is only
396 * possible if a pendconn is released by someone else or if the
397 * stream is supposed to be queued but without its associated
398 * pendconn. In both cases it is a bug! */
399 abort();
Christopher Faulet8ba59142017-06-27 15:43:53 +0200400 }
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100401 p = strm->pend_pos;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200402
403 /* note below : we need to grab the queue's lock to check for emptiness
404 * because we don't want a partial _grab_from_px() or _redistribute()
405 * to be called in parallel and show an empty list without having the
406 * time to finish. With this we know that if we see the element
407 * unlinked, these functions were completely done.
408 */
409 pendconn_queue_lock(p);
410 is_unlinked = LIST_ISEMPTY(&p->list);
411 pendconn_queue_unlock(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100412
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200413 if (!is_unlinked)
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100414 return 1;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100415
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200416 /* the pendconn is not queued anymore and will not be so we're safe
417 * to proceed.
418 */
Willy Tarreau88930dd2018-07-26 07:38:54 +0200419 if (p->target)
420 strm->target = &p->target->obj_type;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100421
422 strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
423 strm->flags |= p->strm_flags & (SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
424 strm->pend_pos = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100425 pool_free(pool_head_pendconn, p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100426 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200427}
428
Willy Tarreaubaaee002006-06-26 02:48:02 +0200429/*
430 * Local variables:
431 * c-indent-level: 8
432 * c-basic-offset: 8
433 * End:
434 */