blob: f0295b0f5ecb61328b94aaf8be33e78622be3238 [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);
42
43
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
55 if (px->srv_map_sz == 0)
56 return NULL;
57
58 if (px->srv_rr_idx < 0 || px->srv_rr_idx >= px->srv_map_sz)
59 px->srv_rr_idx = 0;
60 newidx = px->srv_rr_idx;
61
62 do {
63 srv = px->srv_map[newidx++];
64 if (!srv->maxconn || srv->cur_sess < srv_dynamic_maxconn(srv)) {
65 px->srv_rr_idx = newidx;
66 return srv;
67 }
68 if (newidx == px->srv_map_sz)
69 newidx = 0;
70 } while (newidx != px->srv_rr_idx);
71
72 return NULL;
73}
74
75
76/*
77 * This function tries to find a running server for the proxy <px> following
78 * the round-robin method.
79 * If any server is found, it will be returned and px->srv_rr_idx will be updated
80 * to point to the next server. If no valid server is found, NULL is returned.
81 */
82static inline struct server *get_server_rr(struct proxy *px)
83{
84 if (px->srv_map_sz == 0)
85 return NULL;
86
87 if (px->srv_rr_idx < 0 || px->srv_rr_idx >= px->srv_map_sz)
88 px->srv_rr_idx = 0;
89 return px->srv_map[px->srv_rr_idx++];
90}
91
92
93/*
94 * This function tries to find a running server for the proxy <px> following
95 * the source hash method. Depending on the number of active/backup servers,
96 * it will either look for active servers, or for backup servers.
97 * If any server is found, it will be returned. If no valid server is found,
98 * NULL is returned.
99 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200100static inline struct server *get_server_sh(const struct proxy *px,
101 const char *addr, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102{
103 unsigned int h, l;
104
105 if (px->srv_map_sz == 0)
106 return NULL;
107
108 l = h = 0;
109 if (px->srv_act > 1 || (px->srv_act == 0 && px->srv_bck > 1)) {
110 while ((l + sizeof (int)) <= len) {
111 h ^= ntohl(*(unsigned int *)(&addr[l]));
112 l += sizeof (int);
113 }
114 h %= px->srv_map_sz;
115 }
116 return px->srv_map[h];
117}
118
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200119/*
120 * This function tries to find a running server for the proxy <px> following
121 * the URI hash method. In order to optimize cache hits, the hash computation
122 * ends at the question mark. Depending on the number of active/backup servers,
123 * it will either look for active servers, or for backup servers.
124 * If any server is found, it will be returned. If no valid server is found,
125 * NULL is returned.
126 *
127 * This code was contributed by Guillaume Dallaire, who also selected this hash
128 * algorithm out of a tens because it gave him the best results.
129 *
130 */
131static inline struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
132{
133 unsigned long hash = 0;
134 int c;
135
136 if (px->srv_map_sz == 0)
137 return NULL;
138
139 while (uri_len--) {
140 c = *uri++;
141 if (c == '?')
142 break;
143 hash = c + (hash << 6) + (hash << 16) - hash;
144 }
145
146 return px->srv_map[hash % px->srv_map_sz];
147}
148
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149
150#endif /* _PROTO_BACKEND_H */
151
152/*
153 * Local variables:
154 * c-indent-level: 8
155 * c-basic-offset: 8
156 * End:
157 */