blob: 172e0560d05f79da35a284772a0a1f0342132604 [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
Willy Tarreaubaaee002006-06-26 02:48:02 +020017#include <proto/queue.h>
18#include <proto/server.h>
19#include <proto/task.h>
20
21
Willy Tarreaue4d7e552007-05-13 20:19:55 +020022struct pool_head *pool2_pendconn;
23
24/* perform minimal intializations, report 0 in case of error, 1 if OK. */
25int init_pendconn()
26{
27 pool2_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED);
28 return pool2_pendconn != NULL;
29}
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
31/* returns the effective dynamic maxconn for a server, considering the minconn
Willy Tarreau86034312006-12-29 00:10:33 +010032 * and the proxy's usage relative to its dynamic connections limit. It is
Willy Tarreau9909fc12007-11-30 17:42:05 +010033 * expected that 0 < s->minconn <= s->maxconn when this is called. If the
34 * server is currently warming up, the slowstart is also applied to the
35 * resulting value, which can be lower than minconn in this case, but never
36 * less than 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +020037 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020038unsigned int srv_dynamic_maxconn(const struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020039{
Willy Tarreau9909fc12007-11-30 17:42:05 +010040 unsigned int max;
41
Willy Tarreau86034312006-12-29 00:10:33 +010042 if (s->proxy->beconn >= s->proxy->fullconn)
43 /* no fullconn or proxy is full */
Willy Tarreau9909fc12007-11-30 17:42:05 +010044 max = s->maxconn;
45 else if (s->minconn == s->maxconn)
Willy Tarreau86034312006-12-29 00:10:33 +010046 /* static limit */
Willy Tarreau9909fc12007-11-30 17:42:05 +010047 max = s->maxconn;
48 else max = MAX(s->minconn,
49 s->proxy->beconn * s->maxconn / s->proxy->fullconn);
Willy Tarreau86034312006-12-29 00:10:33 +010050
Willy Tarreau9909fc12007-11-30 17:42:05 +010051 if ((s->state & SRV_WARMINGUP) &&
52 now.tv_sec < s->last_change + s->slowstart &&
53 now.tv_sec >= s->last_change) {
54 unsigned int ratio;
Willy Tarreau28a9e522008-09-14 17:43:27 +020055 ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart;
56 max = MAX(1, max * ratio / 100);
Willy Tarreau9909fc12007-11-30 17:42:05 +010057 }
58 return max;
Willy Tarreaubaaee002006-06-26 02:48:02 +020059}
60
61
62/*
Willy Tarreau7c669d72008-06-20 15:04:11 +020063 * Manages a server's connection queue. This function will try to dequeue as
64 * many pending sessions as possible, and wake them up.
Willy Tarreaubaaee002006-06-26 02:48:02 +020065 */
Willy Tarreau7c669d72008-06-20 15:04:11 +020066void process_srv_queue(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020067{
Willy Tarreaubaaee002006-06-26 02:48:02 +020068 struct proxy *p = s->proxy;
Willy Tarreau7c669d72008-06-20 15:04:11 +020069 int maxconn;
Willy Tarreaubaaee002006-06-26 02:48:02 +020070
71 /* First, check if we can handle some connections queued at the proxy. We
72 * will take as many as we can handle.
73 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020074
Willy Tarreau7c669d72008-06-20 15:04:11 +020075 maxconn = srv_dynamic_maxconn(s);
76 while (s->served < maxconn) {
77 struct session *sess = pendconn_get_next_sess(s, p);
Willy Tarreaubaaee002006-06-26 02:48:02 +020078 if (sess == NULL)
79 break;
Willy Tarreaufdccded2008-08-29 18:19:04 +020080 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubaaee002006-06-26 02:48:02 +020081 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020082}
83
84/* Detaches the next pending connection from either a server or a proxy, and
85 * returns its associated session. If no pending connection is found, NULL is
Willy Tarreau7c669d72008-06-20 15:04:11 +020086 * returned. Note that neither <srv> nor <px> may be NULL.
Willy Tarreau70089872008-06-13 21:12:51 +020087 * Priority is given to the oldest request in the queue if both <srv> and <px>
88 * have pending requests. This ensures that no request will be left unserved.
Willy Tarreau7c669d72008-06-20 15:04:11 +020089 * The session is immediately marked as "assigned", and both its <srv> and
90 * <srv_conn> are set to <srv>,
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 */
92struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px)
93{
Willy Tarreau70089872008-06-13 21:12:51 +020094 struct pendconn *ps, *pp;
Willy Tarreaubaaee002006-06-26 02:48:02 +020095 struct session *sess;
96
Willy Tarreau70089872008-06-13 21:12:51 +020097 ps = pendconn_from_srv(srv);
98 pp = pendconn_from_px(px);
99 /* we want to get the definitive pendconn in <ps> */
100 if (!pp) {
101 if (!ps)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102 return NULL;
Willy Tarreau70089872008-06-13 21:12:51 +0200103 } else {
104 /* pendconn exists in the proxy queue */
105 if (!ps || tv_islt(&pp->sess->logs.tv_request, &ps->sess->logs.tv_request)) {
106 ps = pp;
107 ps->sess->srv = srv;
108 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109 }
Willy Tarreau70089872008-06-13 21:12:51 +0200110 sess = ps->sess;
111 pendconn_free(ps);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200112
113 /* we want to note that the session has now been assigned a server */
114 sess->flags |= SN_ASSIGNED;
115 sess->srv = srv;
116 sess->srv_conn = srv;
117 srv->served++;
118 if (px->lbprm.server_take_conn)
119 px->lbprm.server_take_conn(srv);
120
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121 return sess;
122}
123
124/* Adds the session <sess> to the pending connection list of server <sess>->srv
125 * or to the one of <sess>->proxy if srv is NULL. All counters and back pointers
126 * are updated accordingly. Returns NULL if no memory is available, otherwise the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200127 * pendconn itself. If the session was already marked as served, its flag is
128 * cleared. It is illegal to call this function with a non-NULL sess->srv_conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129 */
130struct pendconn *pendconn_add(struct session *sess)
131{
132 struct pendconn *p;
133
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200134 p = pool_alloc2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200135 if (!p)
136 return NULL;
137
138 sess->pend_pos = p;
139 p->sess = sess;
140 p->srv = sess->srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200141
142 if (sess->flags & SN_ASSIGNED && sess->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143 LIST_ADDQ(&sess->srv->pendconns, &p->list);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200144 sess->srv->nbpend++;
Willy Tarreau7a63abd2008-06-13 21:48:18 +0200145 sess->logs.srv_queue_size += sess->srv->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200146 if (sess->srv->nbpend > sess->srv->nbpend_max)
147 sess->srv->nbpend_max = sess->srv->nbpend;
148 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200149 LIST_ADDQ(&sess->be->pendconns, &p->list);
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200150 sess->be->nbpend++;
Willy Tarreau7a63abd2008-06-13 21:48:18 +0200151 sess->logs.prx_queue_size += sess->be->nbpend;
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200152 if (sess->be->nbpend > sess->be->nbpend_max)
153 sess->be->nbpend_max = sess->be->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200155 sess->be->totpend++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200156 return p;
157}
158
159/*
160 * Detaches pending connection <p>, decreases the pending count, and frees
161 * the pending connection. The connection might have been queued to a specific
162 * server as well as to the proxy. The session also gets marked unqueued.
163 */
164void pendconn_free(struct pendconn *p)
165{
166 LIST_DEL(&p->list);
167 p->sess->pend_pos = NULL;
168 if (p->srv)
169 p->srv->nbpend--;
170 else
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200171 p->sess->be->nbpend--;
172 p->sess->be->totpend--;
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200173 pool_free2(pool2_pendconn, p);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200174}
175
176
177/*
178 * Local variables:
179 * c-indent-level: 8
180 * c-basic-offset: 8
181 * End:
182 */