blob: 178f1877c23b18a3c3084332d5701f2789a59be0 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Queue management functions.
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020013#include <common/config.h>
Willy Tarreaue4d7e552007-05-13 20:19:55 +020014#include <common/memory.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020015#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016
17#include <types/proxy.h>
18#include <types/session.h>
19
20#include <proto/queue.h>
21#include <proto/server.h>
22#include <proto/task.h>
23
24
Willy Tarreaue4d7e552007-05-13 20:19:55 +020025struct pool_head *pool2_pendconn;
26
27/* perform minimal intializations, report 0 in case of error, 1 if OK. */
28int init_pendconn()
29{
30 pool2_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED);
31 return pool2_pendconn != NULL;
32}
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
34/* returns the effective dynamic maxconn for a server, considering the minconn
Willy Tarreau86034312006-12-29 00:10:33 +010035 * and the proxy's usage relative to its dynamic connections limit. It is
Willy Tarreau9909fc12007-11-30 17:42:05 +010036 * expected that 0 < s->minconn <= s->maxconn when this is called. If the
37 * server is currently warming up, the slowstart is also applied to the
38 * resulting value, which can be lower than minconn in this case, but never
39 * less than 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +020040 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020041unsigned int srv_dynamic_maxconn(const struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020042{
Willy Tarreau9909fc12007-11-30 17:42:05 +010043 unsigned int max;
44
Willy Tarreau86034312006-12-29 00:10:33 +010045 if (s->proxy->beconn >= s->proxy->fullconn)
46 /* no fullconn or proxy is full */
Willy Tarreau9909fc12007-11-30 17:42:05 +010047 max = s->maxconn;
48 else if (s->minconn == s->maxconn)
Willy Tarreau86034312006-12-29 00:10:33 +010049 /* static limit */
Willy Tarreau9909fc12007-11-30 17:42:05 +010050 max = s->maxconn;
51 else max = MAX(s->minconn,
52 s->proxy->beconn * s->maxconn / s->proxy->fullconn);
Willy Tarreau86034312006-12-29 00:10:33 +010053
Willy Tarreau9909fc12007-11-30 17:42:05 +010054 if ((s->state & SRV_WARMINGUP) &&
55 now.tv_sec < s->last_change + s->slowstart &&
56 now.tv_sec >= s->last_change) {
57 unsigned int ratio;
58 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
59 max = max * ratio / 100;
60 }
61 return max;
Willy Tarreaubaaee002006-06-26 02:48:02 +020062}
63
64
65/*
Willy Tarreau7c669d72008-06-20 15:04:11 +020066 * Manages a server's connection queue. This function will try to dequeue as
67 * many pending sessions as possible, and wake them up.
Willy Tarreaubaaee002006-06-26 02:48:02 +020068 */
Willy Tarreau7c669d72008-06-20 15:04:11 +020069void process_srv_queue(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020070{
Willy Tarreaubaaee002006-06-26 02:48:02 +020071 struct proxy *p = s->proxy;
Willy Tarreau7c669d72008-06-20 15:04:11 +020072 int maxconn;
Willy Tarreaubaaee002006-06-26 02:48:02 +020073
74 /* First, check if we can handle some connections queued at the proxy. We
75 * will take as many as we can handle.
76 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
Willy Tarreau7c669d72008-06-20 15:04:11 +020078 maxconn = srv_dynamic_maxconn(s);
79 while (s->served < maxconn) {
80 struct session *sess = pendconn_get_next_sess(s, p);
Willy Tarreaubaaee002006-06-26 02:48:02 +020081 if (sess == NULL)
82 break;
Willy Tarreau96bcfd72007-04-29 10:41:56 +020083 task_wakeup(sess->task);
Willy Tarreaubaaee002006-06-26 02:48:02 +020084 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020085}
86
87/* Detaches the next pending connection from either a server or a proxy, and
88 * returns its associated session. If no pending connection is found, NULL is
Willy Tarreau7c669d72008-06-20 15:04:11 +020089 * returned. Note that neither <srv> nor <px> may be NULL.
Willy Tarreau70089872008-06-13 21:12:51 +020090 * Priority is given to the oldest request in the queue if both <srv> and <px>
91 * have pending requests. This ensures that no request will be left unserved.
Willy Tarreau7c669d72008-06-20 15:04:11 +020092 * The session is immediately marked as "assigned", and both its <srv> and
93 * <srv_conn> are set to <srv>,
Willy Tarreaubaaee002006-06-26 02:48:02 +020094 */
95struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px)
96{
Willy Tarreau70089872008-06-13 21:12:51 +020097 struct pendconn *ps, *pp;
Willy Tarreaubaaee002006-06-26 02:48:02 +020098 struct session *sess;
99
Willy Tarreau70089872008-06-13 21:12:51 +0200100 ps = pendconn_from_srv(srv);
101 pp = pendconn_from_px(px);
102 /* we want to get the definitive pendconn in <ps> */
103 if (!pp) {
104 if (!ps)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105 return NULL;
Willy Tarreau70089872008-06-13 21:12:51 +0200106 } else {
107 /* pendconn exists in the proxy queue */
108 if (!ps || tv_islt(&pp->sess->logs.tv_request, &ps->sess->logs.tv_request)) {
109 ps = pp;
110 ps->sess->srv = srv;
111 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112 }
Willy Tarreau70089872008-06-13 21:12:51 +0200113 sess = ps->sess;
114 pendconn_free(ps);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200115
116 /* we want to note that the session has now been assigned a server */
117 sess->flags |= SN_ASSIGNED;
118 sess->srv = srv;
119 sess->srv_conn = srv;
120 srv->served++;
121 if (px->lbprm.server_take_conn)
122 px->lbprm.server_take_conn(srv);
123
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124 return sess;
125}
126
127/* Adds the session <sess> to the pending connection list of server <sess>->srv
128 * or to the one of <sess>->proxy if srv is NULL. All counters and back pointers
129 * are updated accordingly. Returns NULL if no memory is available, otherwise the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200130 * pendconn itself. If the session was already marked as served, its flag is
131 * cleared. It is illegal to call this function with a non-NULL sess->srv_conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200132 */
133struct pendconn *pendconn_add(struct session *sess)
134{
135 struct pendconn *p;
136
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200137 p = pool_alloc2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200138 if (!p)
139 return NULL;
140
141 sess->pend_pos = p;
142 p->sess = sess;
143 p->srv = sess->srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200144
145 if (sess->flags & SN_ASSIGNED && sess->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200146 LIST_ADDQ(&sess->srv->pendconns, &p->list);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200147 sess->srv->nbpend++;
Willy Tarreau7a63abd2008-06-13 21:48:18 +0200148 sess->logs.srv_queue_size += sess->srv->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149 if (sess->srv->nbpend > sess->srv->nbpend_max)
150 sess->srv->nbpend_max = sess->srv->nbpend;
151 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200152 LIST_ADDQ(&sess->be->pendconns, &p->list);
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200153 sess->be->nbpend++;
Willy Tarreau7a63abd2008-06-13 21:48:18 +0200154 sess->logs.prx_queue_size += sess->be->nbpend;
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200155 if (sess->be->nbpend > sess->be->nbpend_max)
156 sess->be->nbpend_max = sess->be->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200157 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200158 sess->be->totpend++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159 return p;
160}
161
162/*
163 * Detaches pending connection <p>, decreases the pending count, and frees
164 * the pending connection. The connection might have been queued to a specific
165 * server as well as to the proxy. The session also gets marked unqueued.
166 */
167void pendconn_free(struct pendconn *p)
168{
169 LIST_DEL(&p->list);
170 p->sess->pend_pos = NULL;
171 if (p->srv)
172 p->srv->nbpend--;
173 else
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200174 p->sess->be->nbpend--;
175 p->sess->be->totpend--;
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200176 pool_free2(pool2_pendconn, p);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200177}
178
179
180/*
181 * Local variables:
182 * c-indent-level: 8
183 * c-basic-offset: 8
184 * End:
185 */