blob: d282fecb1e3406239cef64eda46738ea2e9bc3f0 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Queue management functions.
3 *
Willy Tarreaua8cff1d2007-04-09 16:10:57 +02004 * Copyright 2000-2007 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/*
66 * Manages a server's connection queue. If woken up, will try to dequeue as
67 * many pending sessions as possible, and wake them up. The task has nothing
Willy Tarreaud825eef2007-05-12 22:35:00 +020068 * else to do, so it always returns ETERNITY.
Willy Tarreaubaaee002006-06-26 02:48:02 +020069 */
Willy Tarreaud825eef2007-05-12 22:35:00 +020070void process_srv_queue(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +020071{
72 struct server *s = (struct server*)t->context;
73 struct proxy *p = s->proxy;
74 int xferred;
75
76 /* First, check if we can handle some connections queued at the proxy. We
77 * will take as many as we can handle.
78 */
79 for (xferred = 0; s->cur_sess + xferred < srv_dynamic_maxconn(s); xferred++) {
80 struct session *sess;
81
82 sess = pendconn_get_next_sess(s, p);
83 if (sess == NULL)
84 break;
Willy Tarreau96bcfd72007-04-29 10:41:56 +020085 task_wakeup(sess->task);
Willy Tarreaubaaee002006-06-26 02:48:02 +020086 }
87
Willy Tarreaud825eef2007-05-12 22:35:00 +020088 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +020089}
90
91/* Detaches the next pending connection from either a server or a proxy, and
92 * returns its associated session. If no pending connection is found, NULL is
93 * returned. Note that neither <srv> nor <px> can be NULL.
94 */
95struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px)
96{
97 struct pendconn *p;
98 struct session *sess;
99
100 p = pendconn_from_srv(srv);
101 if (!p) {
102 p = pendconn_from_px(px);
103 if (!p)
104 return NULL;
105 p->sess->srv = srv;
106 }
107 sess = p->sess;
108 pendconn_free(p);
109 return sess;
110}
111
112/* Adds the session <sess> to the pending connection list of server <sess>->srv
113 * or to the one of <sess>->proxy if srv is NULL. All counters and back pointers
114 * are updated accordingly. Returns NULL if no memory is available, otherwise the
115 * pendconn itself.
116 */
117struct pendconn *pendconn_add(struct session *sess)
118{
119 struct pendconn *p;
120
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200121 p = pool_alloc2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 if (!p)
123 return NULL;
124
125 sess->pend_pos = p;
126 p->sess = sess;
127 p->srv = sess->srv;
128 if (sess->srv) {
129 LIST_ADDQ(&sess->srv->pendconns, &p->list);
130 sess->logs.srv_queue_size += sess->srv->nbpend;
131 sess->srv->nbpend++;
132 if (sess->srv->nbpend > sess->srv->nbpend_max)
133 sess->srv->nbpend_max = sess->srv->nbpend;
134 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200135 LIST_ADDQ(&sess->be->pendconns, &p->list);
136 sess->logs.prx_queue_size += sess->be->nbpend;
137 sess->be->nbpend++;
138 if (sess->be->nbpend > sess->be->nbpend_max)
139 sess->be->nbpend_max = sess->be->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200140 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200141 sess->be->totpend++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200142 return p;
143}
144
145/*
146 * Detaches pending connection <p>, decreases the pending count, and frees
147 * the pending connection. The connection might have been queued to a specific
148 * server as well as to the proxy. The session also gets marked unqueued.
149 */
150void pendconn_free(struct pendconn *p)
151{
152 LIST_DEL(&p->list);
153 p->sess->pend_pos = NULL;
154 if (p->srv)
155 p->srv->nbpend--;
156 else
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200157 p->sess->be->nbpend--;
158 p->sess->be->totpend--;
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200159 pool_free2(pool2_pendconn, p);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200160}
161
162
163/*
164 * Local variables:
165 * c-indent-level: 8
166 * c-basic-offset: 8
167 * End:
168 */