blob: 611cfe95c9513efa2e435a1820df8bea13fcfc0b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Queue management functions.
3 *
4 * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
5 *
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 Tarreau2dd0d472006-06-29 17:53:05 +020014#include <common/time.h>
Willy Tarreauf1221aa2006-12-17 22:14:12 +010015#include <common/tools.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
25void **pool_pendconn = NULL;
26
27/* returns the effective dynamic maxconn for a server, considering the minconn
Willy Tarreau86034312006-12-29 00:10:33 +010028 * and the proxy's usage relative to its dynamic connections limit. It is
29 * expected that 0 < s->minconn <= s->maxconn when this is called.
Willy Tarreaubaaee002006-06-26 02:48:02 +020030 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020031unsigned int srv_dynamic_maxconn(const struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020032{
Willy Tarreau86034312006-12-29 00:10:33 +010033 if (s->proxy->beconn >= s->proxy->fullconn)
34 /* no fullconn or proxy is full */
35 return s->maxconn;
36
37 if (s->minconn == s->maxconn)
38 /* static limit */
39 return s->maxconn;
40
41 return MAX(s->minconn,
42 s->proxy->beconn * s->maxconn / s->proxy->fullconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +020043}
44
45
46/*
47 * Manages a server's connection queue. If woken up, will try to dequeue as
48 * many pending sessions as possible, and wake them up. The task has nothing
49 * else to do, so it always returns TIME_ETERNITY.
50 */
51int process_srv_queue(struct task *t)
52{
53 struct server *s = (struct server*)t->context;
54 struct proxy *p = s->proxy;
55 int xferred;
56
57 /* First, check if we can handle some connections queued at the proxy. We
58 * will take as many as we can handle.
59 */
60 for (xferred = 0; s->cur_sess + xferred < srv_dynamic_maxconn(s); xferred++) {
61 struct session *sess;
62
63 sess = pendconn_get_next_sess(s, p);
64 if (sess == NULL)
65 break;
66 task_wakeup(&rq, sess->task);
67 }
68
69 return TIME_ETERNITY;
70}
71
72/* Detaches the next pending connection from either a server or a proxy, and
73 * returns its associated session. If no pending connection is found, NULL is
74 * returned. Note that neither <srv> nor <px> can be NULL.
75 */
76struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px)
77{
78 struct pendconn *p;
79 struct session *sess;
80
81 p = pendconn_from_srv(srv);
82 if (!p) {
83 p = pendconn_from_px(px);
84 if (!p)
85 return NULL;
86 p->sess->srv = srv;
87 }
88 sess = p->sess;
89 pendconn_free(p);
90 return sess;
91}
92
93/* Adds the session <sess> to the pending connection list of server <sess>->srv
94 * or to the one of <sess>->proxy if srv is NULL. All counters and back pointers
95 * are updated accordingly. Returns NULL if no memory is available, otherwise the
96 * pendconn itself.
97 */
98struct pendconn *pendconn_add(struct session *sess)
99{
100 struct pendconn *p;
101
102 p = pool_alloc(pendconn);
103 if (!p)
104 return NULL;
105
106 sess->pend_pos = p;
107 p->sess = sess;
108 p->srv = sess->srv;
109 if (sess->srv) {
110 LIST_ADDQ(&sess->srv->pendconns, &p->list);
111 sess->logs.srv_queue_size += sess->srv->nbpend;
112 sess->srv->nbpend++;
113 if (sess->srv->nbpend > sess->srv->nbpend_max)
114 sess->srv->nbpend_max = sess->srv->nbpend;
115 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +0100116 LIST_ADDQ(&sess->be->beprm->pendconns, &p->list);
117 sess->logs.prx_queue_size += sess->be->beprm->nbpend;
118 sess->be->beprm->nbpend++;
119 if (sess->be->beprm->nbpend > sess->be->beprm->nbpend_max)
120 sess->be->beprm->nbpend_max = sess->be->beprm->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121 }
Willy Tarreau830ff452006-12-17 19:31:23 +0100122 sess->be->beprm->totpend++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200123 return p;
124}
125
126/*
127 * Detaches pending connection <p>, decreases the pending count, and frees
128 * the pending connection. The connection might have been queued to a specific
129 * server as well as to the proxy. The session also gets marked unqueued.
130 */
131void pendconn_free(struct pendconn *p)
132{
133 LIST_DEL(&p->list);
134 p->sess->pend_pos = NULL;
135 if (p->srv)
136 p->srv->nbpend--;
137 else
Willy Tarreau830ff452006-12-17 19:31:23 +0100138 p->sess->be->beprm->nbpend--;
139 p->sess->be->beprm->totpend--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200140 pool_free(pendconn, p);
141}
142
143
144/*
145 * Local variables:
146 * c-indent-level: 8
147 * c-basic-offset: 8
148 * End:
149 */