blob: d70c68faf030c61f46d911b513766ffc22f33b79 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 include/proto/backend.h
3 Functions prototypes for the backend.
4
Willy Tarreau2fcb5002007-05-08 13:35:26 +02005 Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
Willy Tarreaubaaee002006-06-26 02:48:02 +02006
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*/
21
22#ifndef _PROTO_BACKEND_H
23#define _PROTO_BACKEND_H
24
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
27#include <types/backend.h>
28#include <types/session.h>
29
30#include <proto/queue.h>
31
32int assign_server(struct session *s);
33int assign_server_address(struct session *s);
34int assign_server_and_queue(struct session *s);
35int connect_server(struct session *s);
36int srv_count_retry_down(struct session *t, int conn_err);
37int srv_retryable_connect(struct session *t);
38int srv_redispatch_connect(struct session *t);
39
40void recount_servers(struct proxy *px);
41void recalc_server_map(struct proxy *px);
Krzysztof Oledzki85130942007-10-22 16:21:10 +020042int be_downtime(struct proxy *px);
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
44/*
45 * This function tries to find a running server with free connection slots for
46 * the proxy <px> following the round-robin method.
47 * If any server is found, it will be returned and px->srv_rr_idx will be updated
48 * to point to the next server. If no valid server is found, NULL is returned.
49 */
50static inline struct server *get_server_rr_with_conns(struct proxy *px)
51{
52 int newidx;
53 struct server *srv;
54
Willy Tarreau5af3a692007-07-24 23:32:33 +020055 if (px->map_state & PR_MAP_RECALC)
56 recalc_server_map(px);
57
Willy Tarreaubaaee002006-06-26 02:48:02 +020058 if (px->srv_map_sz == 0)
59 return NULL;
60
61 if (px->srv_rr_idx < 0 || px->srv_rr_idx >= px->srv_map_sz)
62 px->srv_rr_idx = 0;
63 newidx = px->srv_rr_idx;
64
65 do {
66 srv = px->srv_map[newidx++];
67 if (!srv->maxconn || srv->cur_sess < srv_dynamic_maxconn(srv)) {
68 px->srv_rr_idx = newidx;
69 return srv;
70 }
71 if (newidx == px->srv_map_sz)
72 newidx = 0;
73 } while (newidx != px->srv_rr_idx);
74
75 return NULL;
76}
77
78
79/*
80 * This function tries to find a running server for the proxy <px> following
81 * the round-robin method.
82 * If any server is found, it will be returned and px->srv_rr_idx will be updated
83 * to point to the next server. If no valid server is found, NULL is returned.
84 */
85static inline struct server *get_server_rr(struct proxy *px)
86{
Willy Tarreau5af3a692007-07-24 23:32:33 +020087 if (px->map_state & PR_MAP_RECALC)
88 recalc_server_map(px);
89
Willy Tarreaubaaee002006-06-26 02:48:02 +020090 if (px->srv_map_sz == 0)
91 return NULL;
92
93 if (px->srv_rr_idx < 0 || px->srv_rr_idx >= px->srv_map_sz)
94 px->srv_rr_idx = 0;
95 return px->srv_map[px->srv_rr_idx++];
96}
97
98
99/*
100 * This function tries to find a running server for the proxy <px> following
101 * the source hash method. Depending on the number of active/backup servers,
102 * it will either look for active servers, or for backup servers.
103 * If any server is found, it will be returned. If no valid server is found,
104 * NULL is returned.
105 */
Willy Tarreau5af3a692007-07-24 23:32:33 +0200106static inline struct server *get_server_sh(struct proxy *px,
Willy Tarreaub17916e2006-10-15 15:17:57 +0200107 const char *addr, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108{
109 unsigned int h, l;
110
Willy Tarreau5af3a692007-07-24 23:32:33 +0200111 if (px->map_state & PR_MAP_RECALC)
112 recalc_server_map(px);
113
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 if (px->srv_map_sz == 0)
115 return NULL;
116
117 l = h = 0;
118 if (px->srv_act > 1 || (px->srv_act == 0 && px->srv_bck > 1)) {
119 while ((l + sizeof (int)) <= len) {
120 h ^= ntohl(*(unsigned int *)(&addr[l]));
121 l += sizeof (int);
122 }
123 h %= px->srv_map_sz;
124 }
125 return px->srv_map[h];
126}
127
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200128/*
129 * This function tries to find a running server for the proxy <px> following
130 * the URI hash method. In order to optimize cache hits, the hash computation
131 * ends at the question mark. Depending on the number of active/backup servers,
132 * it will either look for active servers, or for backup servers.
133 * If any server is found, it will be returned. If no valid server is found,
134 * NULL is returned.
135 *
136 * This code was contributed by Guillaume Dallaire, who also selected this hash
137 * algorithm out of a tens because it gave him the best results.
138 *
139 */
140static inline struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
141{
142 unsigned long hash = 0;
143 int c;
144
Willy Tarreau5af3a692007-07-24 23:32:33 +0200145 if (px->map_state & PR_MAP_RECALC)
146 recalc_server_map(px);
147
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200148 if (px->srv_map_sz == 0)
149 return NULL;
150
151 while (uri_len--) {
152 c = *uri++;
153 if (c == '?')
154 break;
155 hash = c + (hash << 6) + (hash << 16) - hash;
156 }
157
158 return px->srv_map[hash % px->srv_map_sz];
159}
160
Willy Tarreaubaaee002006-06-26 02:48:02 +0200161
162#endif /* _PROTO_BACKEND_H */
163
164/*
165 * Local variables:
166 * c-indent-level: 8
167 * c-basic-offset: 8
168 * End:
169 */