blob: 5f24be6d884573957ebd63ce4207d0adde1adde6 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 include/proto/backend.h
3 Functions prototypes for the backend.
4
5 Copyright (C) 2000-2006 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*/
21
22#ifndef _PROTO_BACKEND_H
23#define _PROTO_BACKEND_H
24
25
26#include <types/backend.h>
27#include <types/session.h>
28
29#include <proto/queue.h>
30
31int assign_server(struct session *s);
32int assign_server_address(struct session *s);
33int assign_server_and_queue(struct session *s);
34int connect_server(struct session *s);
35int srv_count_retry_down(struct session *t, int conn_err);
36int srv_retryable_connect(struct session *t);
37int srv_redispatch_connect(struct session *t);
38
39void recount_servers(struct proxy *px);
40void recalc_server_map(struct proxy *px);
41
42
43/*
44 * This function tries to find a running server with free connection slots for
45 * the proxy <px> following the round-robin method.
46 * If any server is found, it will be returned and px->srv_rr_idx will be updated
47 * to point to the next server. If no valid server is found, NULL is returned.
48 */
49static inline struct server *get_server_rr_with_conns(struct proxy *px)
50{
51 int newidx;
52 struct server *srv;
53
54 if (px->srv_map_sz == 0)
55 return NULL;
56
57 if (px->srv_rr_idx < 0 || px->srv_rr_idx >= px->srv_map_sz)
58 px->srv_rr_idx = 0;
59 newidx = px->srv_rr_idx;
60
61 do {
62 srv = px->srv_map[newidx++];
63 if (!srv->maxconn || srv->cur_sess < srv_dynamic_maxconn(srv)) {
64 px->srv_rr_idx = newidx;
65 return srv;
66 }
67 if (newidx == px->srv_map_sz)
68 newidx = 0;
69 } while (newidx != px->srv_rr_idx);
70
71 return NULL;
72}
73
74
75/*
76 * This function tries to find a running server for the proxy <px> following
77 * the round-robin method.
78 * If any server is found, it will be returned and px->srv_rr_idx will be updated
79 * to point to the next server. If no valid server is found, NULL is returned.
80 */
81static inline struct server *get_server_rr(struct proxy *px)
82{
83 if (px->srv_map_sz == 0)
84 return NULL;
85
86 if (px->srv_rr_idx < 0 || px->srv_rr_idx >= px->srv_map_sz)
87 px->srv_rr_idx = 0;
88 return px->srv_map[px->srv_rr_idx++];
89}
90
91
92/*
93 * This function tries to find a running server for the proxy <px> following
94 * the source hash method. Depending on the number of active/backup servers,
95 * it will either look for active servers, or for backup servers.
96 * If any server is found, it will be returned. If no valid server is found,
97 * NULL is returned.
98 */
99static inline struct server *get_server_sh(struct proxy *px, char *addr, int len)
100{
101 unsigned int h, l;
102
103 if (px->srv_map_sz == 0)
104 return NULL;
105
106 l = h = 0;
107 if (px->srv_act > 1 || (px->srv_act == 0 && px->srv_bck > 1)) {
108 while ((l + sizeof (int)) <= len) {
109 h ^= ntohl(*(unsigned int *)(&addr[l]));
110 l += sizeof (int);
111 }
112 h %= px->srv_map_sz;
113 }
114 return px->srv_map[h];
115}
116
117
118#endif /* _PROTO_BACKEND_H */
119
120/*
121 * Local variables:
122 * c-indent-level: 8
123 * c-basic-offset: 8
124 * End:
125 */