blob: 4397d601fc8e603a1595e3c178370397564ffbd7 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreauf8e8b762012-01-20 15:57:05 +01002 * include/proto/queue.h
3 * This file defines everything related to queues.
4 *
5 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#ifndef _PROTO_QUEUE_H
23#define _PROTO_QUEUE_H
24
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020025#include <haproxy/api.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020026#include <haproxy/pool.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
28#include <types/proxy.h>
29#include <types/queue.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020030#include <types/stream.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032
Willy Tarreauf8e8b762012-01-20 15:57:05 +010033#include <proto/backend.h>
34
Willy Tarreaubafbe012017-11-24 17:34:44 +010035extern struct pool_head *pool_head_pendconn;
Willy Tarreaue4d7e552007-05-13 20:19:55 +020036
Willy Tarreau87b09662015-04-03 00:22:06 +020037struct pendconn *pendconn_add(struct stream *strm);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +010038int pendconn_dequeue(struct stream *strm);
Willy Tarreau7c669d72008-06-20 15:04:11 +020039void process_srv_queue(struct server *s);
Willy Tarreaub17916e2006-10-15 15:17:57 +020040unsigned int srv_dynamic_maxconn(const struct server *s);
Willy Tarreau4aac7db2014-05-16 11:48:10 +020041int pendconn_redistribute(struct server *s);
42int pendconn_grab_from_px(struct server *s);
Willy Tarreau9624fae2018-07-25 08:04:20 +020043void pendconn_unlink(struct pendconn *p);
44
45/* Removes the pendconn from the server/proxy queue. It supports being called
46 * with NULL for pendconn and with a pendconn not in the list. It is the
47 * function to be used by default when unsure. Do not call it with server
48 * or proxy locks held however.
49 */
50static inline void pendconn_cond_unlink(struct pendconn *p)
51{
Patrick Hemmer0355dab2018-05-11 12:52:31 -040052 if (p && p->node.node.leaf_p)
Willy Tarreau9624fae2018-07-25 08:04:20 +020053 pendconn_unlink(p);
54}
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
Willy Tarreaud0ad4a82018-07-25 11:13:53 +020056/* Releases the pendconn associated to stream <s> if it has any, and decreases
57 * the pending count if needed. The connection might have been queued to a
58 * specific server as well as to the proxy. The stream also gets marked
59 * unqueued.
60 *
61 * This function must be called by the stream itself, so in the context of
62 * process_stream, without any lock held among the pendconn, the server's queue
63 * nor the proxy's queue.
64 */
65static inline void pendconn_free(struct stream *s)
66{
67 struct pendconn *p = s->pend_pos;
68
69 if (p) {
70 pendconn_cond_unlink(p);
71 s->pend_pos = NULL;
72 pool_free(pool_head_pendconn, p);
73 }
74}
75
Willy Tarreauc35362a2014-04-25 13:58:37 +020076/* Returns 0 if all slots are full on a server, or 1 if there are slots available. */
77static inline int server_has_room(const struct server *s) {
78 return !s->maxconn || s->cur_sess < srv_dynamic_maxconn(s);
79}
80
Willy Tarreaubaaee002006-06-26 02:48:02 +020081/* returns 0 if nothing has to be done for server <s> regarding queued connections,
Willy Tarreau922a8062008-12-04 09:33:58 +010082 * and non-zero otherwise. If the server is down, we only check its own queue. Suited
83 * for and if/else usage.
Willy Tarreaubaaee002006-06-26 02:48:02 +020084 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020085static inline int may_dequeue_tasks(const struct server *s, const struct proxy *p) {
Emeric Brun52a91d32017-08-31 14:41:55 +020086 return (s && (s->nbpend || (p->nbpend && srv_currently_usable(s))) &&
Willy Tarreau7c669d72008-06-20 15:04:11 +020087 (!s->maxconn || s->cur_sess < srv_dynamic_maxconn(s)));
Willy Tarreaubaaee002006-06-26 02:48:02 +020088}
89
Patrick Hemmer268a7072018-05-11 12:52:31 -040090static inline int queue_limit_class(int class)
91{
92 if (class < -0x7ff)
93 return -0x7ff;
94 if (class > 0x7ff)
95 return 0x7ff;
96 return class;
97}
98
99static inline int queue_limit_offset(int offset)
100{
101 if (offset < -0x7ffff)
102 return -0x7ffff;
103 if (offset > 0x7ffff)
104 return 0x7ffff;
105 return offset;
106}
107
108
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109#endif /* _PROTO_QUEUE_H */
110
111/*
112 * Local variables:
113 * c-indent-level: 8
114 * c-basic-offset: 8
115 * End:
116 */