blob: 8f0474cb801bd93d48230d4745841f0a5c7175fb [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
28 * and the proxy's usage relative to its saturation.
29 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020030unsigned int srv_dynamic_maxconn(const struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020031{
Willy Tarreauf1221aa2006-12-17 22:14:12 +010032 return (s->proxy->beconn >= s->proxy->maxconn) ? s->maxconn :
33 (s->minconn ?
34 MAX(s->maxconn * s->proxy->beconn / s->proxy->maxconn, s->minconn)
35 : s->maxconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +020036}
37
38
39/*
40 * Manages a server's connection queue. If woken up, will try to dequeue as
41 * many pending sessions as possible, and wake them up. The task has nothing
42 * else to do, so it always returns TIME_ETERNITY.
43 */
44int process_srv_queue(struct task *t)
45{
46 struct server *s = (struct server*)t->context;
47 struct proxy *p = s->proxy;
48 int xferred;
49
50 /* First, check if we can handle some connections queued at the proxy. We
51 * will take as many as we can handle.
52 */
53 for (xferred = 0; s->cur_sess + xferred < srv_dynamic_maxconn(s); xferred++) {
54 struct session *sess;
55
56 sess = pendconn_get_next_sess(s, p);
57 if (sess == NULL)
58 break;
59 task_wakeup(&rq, sess->task);
60 }
61
62 return TIME_ETERNITY;
63}
64
65/* Detaches the next pending connection from either a server or a proxy, and
66 * returns its associated session. If no pending connection is found, NULL is
67 * returned. Note that neither <srv> nor <px> can be NULL.
68 */
69struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px)
70{
71 struct pendconn *p;
72 struct session *sess;
73
74 p = pendconn_from_srv(srv);
75 if (!p) {
76 p = pendconn_from_px(px);
77 if (!p)
78 return NULL;
79 p->sess->srv = srv;
80 }
81 sess = p->sess;
82 pendconn_free(p);
83 return sess;
84}
85
86/* Adds the session <sess> to the pending connection list of server <sess>->srv
87 * or to the one of <sess>->proxy if srv is NULL. All counters and back pointers
88 * are updated accordingly. Returns NULL if no memory is available, otherwise the
89 * pendconn itself.
90 */
91struct pendconn *pendconn_add(struct session *sess)
92{
93 struct pendconn *p;
94
95 p = pool_alloc(pendconn);
96 if (!p)
97 return NULL;
98
99 sess->pend_pos = p;
100 p->sess = sess;
101 p->srv = sess->srv;
102 if (sess->srv) {
103 LIST_ADDQ(&sess->srv->pendconns, &p->list);
104 sess->logs.srv_queue_size += sess->srv->nbpend;
105 sess->srv->nbpend++;
106 if (sess->srv->nbpend > sess->srv->nbpend_max)
107 sess->srv->nbpend_max = sess->srv->nbpend;
108 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +0100109 LIST_ADDQ(&sess->be->beprm->pendconns, &p->list);
110 sess->logs.prx_queue_size += sess->be->beprm->nbpend;
111 sess->be->beprm->nbpend++;
112 if (sess->be->beprm->nbpend > sess->be->beprm->nbpend_max)
113 sess->be->beprm->nbpend_max = sess->be->beprm->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 }
Willy Tarreau830ff452006-12-17 19:31:23 +0100115 sess->be->beprm->totpend++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 return p;
117}
118
119/*
120 * Detaches pending connection <p>, decreases the pending count, and frees
121 * the pending connection. The connection might have been queued to a specific
122 * server as well as to the proxy. The session also gets marked unqueued.
123 */
124void pendconn_free(struct pendconn *p)
125{
126 LIST_DEL(&p->list);
127 p->sess->pend_pos = NULL;
128 if (p->srv)
129 p->srv->nbpend--;
130 else
Willy Tarreau830ff452006-12-17 19:31:23 +0100131 p->sess->be->beprm->nbpend--;
132 p->sess->be->beprm->totpend--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133 pool_free(pendconn, p);
134}
135
136
137/*
138 * Local variables:
139 * c-indent-level: 8
140 * c-basic-offset: 8
141 * End:
142 */