Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | f8e8b76 | 2012-01-20 15:57:05 +0100 | [diff] [blame] | 2 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #ifndef _PROTO_QUEUE_H |
| 23 | #define _PROTO_QUEUE_H |
| 24 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 25 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/memory.h> |
| 27 | #include <common/mini-clist.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | |
| 29 | #include <types/proxy.h> |
| 30 | #include <types/queue.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 31 | #include <types/stream.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | #include <types/server.h> |
| 33 | #include <types/task.h> |
| 34 | |
Willy Tarreau | f8e8b76 | 2012-01-20 15:57:05 +0100 | [diff] [blame] | 35 | #include <proto/backend.h> |
| 36 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 37 | extern struct pool_head *pool_head_pendconn; |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 38 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 39 | struct pendconn *pendconn_add(struct stream *strm); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 40 | int pendconn_dequeue(struct stream *strm); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 41 | void process_srv_queue(struct server *s); |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 42 | unsigned int srv_dynamic_maxconn(const struct server *s); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 43 | int pendconn_redistribute(struct server *s); |
| 44 | int pendconn_grab_from_px(struct server *s); |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 45 | void pendconn_unlink(struct pendconn *p); |
| 46 | |
| 47 | /* Removes the pendconn from the server/proxy queue. It supports being called |
| 48 | * with NULL for pendconn and with a pendconn not in the list. It is the |
| 49 | * function to be used by default when unsure. Do not call it with server |
| 50 | * or proxy locks held however. |
| 51 | */ |
| 52 | static inline void pendconn_cond_unlink(struct pendconn *p) |
| 53 | { |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 54 | if (p && p->node.node.leaf_p) |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 55 | pendconn_unlink(p); |
| 56 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | |
Willy Tarreau | d0ad4a8 | 2018-07-25 11:13:53 +0200 | [diff] [blame] | 58 | /* Releases the pendconn associated to stream <s> if it has any, and decreases |
| 59 | * the pending count if needed. The connection might have been queued to a |
| 60 | * specific server as well as to the proxy. The stream also gets marked |
| 61 | * unqueued. |
| 62 | * |
| 63 | * This function must be called by the stream itself, so in the context of |
| 64 | * process_stream, without any lock held among the pendconn, the server's queue |
| 65 | * nor the proxy's queue. |
| 66 | */ |
| 67 | static inline void pendconn_free(struct stream *s) |
| 68 | { |
| 69 | struct pendconn *p = s->pend_pos; |
| 70 | |
| 71 | if (p) { |
| 72 | pendconn_cond_unlink(p); |
| 73 | s->pend_pos = NULL; |
| 74 | pool_free(pool_head_pendconn, p); |
| 75 | } |
| 76 | } |
| 77 | |
Willy Tarreau | c35362a | 2014-04-25 13:58:37 +0200 | [diff] [blame] | 78 | /* Returns 0 if all slots are full on a server, or 1 if there are slots available. */ |
| 79 | static inline int server_has_room(const struct server *s) { |
| 80 | return !s->maxconn || s->cur_sess < srv_dynamic_maxconn(s); |
| 81 | } |
| 82 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 83 | /* returns 0 if nothing has to be done for server <s> regarding queued connections, |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 84 | * and non-zero otherwise. If the server is down, we only check its own queue. Suited |
| 85 | * for and if/else usage. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 86 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 87 | static inline int may_dequeue_tasks(const struct server *s, const struct proxy *p) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 88 | return (s && (s->nbpend || (p->nbpend && srv_currently_usable(s))) && |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 89 | (!s->maxconn || s->cur_sess < srv_dynamic_maxconn(s))); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 92 | static inline int queue_limit_class(int class) |
| 93 | { |
| 94 | if (class < -0x7ff) |
| 95 | return -0x7ff; |
| 96 | if (class > 0x7ff) |
| 97 | return 0x7ff; |
| 98 | return class; |
| 99 | } |
| 100 | |
| 101 | static inline int queue_limit_offset(int offset) |
| 102 | { |
| 103 | if (offset < -0x7ffff) |
| 104 | return -0x7ffff; |
| 105 | if (offset > 0x7ffff) |
| 106 | return 0x7ffff; |
| 107 | return offset; |
| 108 | } |
| 109 | |
| 110 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 111 | #endif /* _PROTO_QUEUE_H */ |
| 112 | |
| 113 | /* |
| 114 | * Local variables: |
| 115 | * c-indent-level: 8 |
| 116 | * c-basic-offset: 8 |
| 117 | * End: |
| 118 | */ |