blob: 54fd6a3b289a1af182d51e87278eee45a9a678e3 [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 Tarreau2dd0d472006-06-29 17:53:05 +020014#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015
16#include <types/proxy.h>
17#include <types/session.h>
18
19#include <proto/queue.h>
20#include <proto/server.h>
21#include <proto/task.h>
22
23
24void **pool_pendconn = NULL;
25
26/* returns the effective dynamic maxconn for a server, considering the minconn
Willy Tarreau86034312006-12-29 00:10:33 +010027 * and the proxy's usage relative to its dynamic connections limit. It is
28 * expected that 0 < s->minconn <= s->maxconn when this is called.
Willy Tarreaubaaee002006-06-26 02:48:02 +020029 */
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 Tarreau86034312006-12-29 00:10:33 +010032 if (s->proxy->beconn >= s->proxy->fullconn)
33 /* no fullconn or proxy is full */
34 return s->maxconn;
35
36 if (s->minconn == s->maxconn)
37 /* static limit */
38 return s->maxconn;
39
40 return MAX(s->minconn,
41 s->proxy->beconn * s->maxconn / s->proxy->fullconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +020042}
43
44
45/*
46 * Manages a server's connection queue. If woken up, will try to dequeue as
47 * many pending sessions as possible, and wake them up. The task has nothing
48 * else to do, so it always returns TIME_ETERNITY.
49 */
50int process_srv_queue(struct task *t)
51{
52 struct server *s = (struct server*)t->context;
53 struct proxy *p = s->proxy;
54 int xferred;
55
56 /* First, check if we can handle some connections queued at the proxy. We
57 * will take as many as we can handle.
58 */
59 for (xferred = 0; s->cur_sess + xferred < srv_dynamic_maxconn(s); xferred++) {
60 struct session *sess;
61
62 sess = pendconn_get_next_sess(s, p);
63 if (sess == NULL)
64 break;
65 task_wakeup(&rq, sess->task);
66 }
67
68 return TIME_ETERNITY;
69}
70
71/* Detaches the next pending connection from either a server or a proxy, and
72 * returns its associated session. If no pending connection is found, NULL is
73 * returned. Note that neither <srv> nor <px> can be NULL.
74 */
75struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px)
76{
77 struct pendconn *p;
78 struct session *sess;
79
80 p = pendconn_from_srv(srv);
81 if (!p) {
82 p = pendconn_from_px(px);
83 if (!p)
84 return NULL;
85 p->sess->srv = srv;
86 }
87 sess = p->sess;
88 pendconn_free(p);
89 return sess;
90}
91
92/* Adds the session <sess> to the pending connection list of server <sess>->srv
93 * or to the one of <sess>->proxy if srv is NULL. All counters and back pointers
94 * are updated accordingly. Returns NULL if no memory is available, otherwise the
95 * pendconn itself.
96 */
97struct pendconn *pendconn_add(struct session *sess)
98{
99 struct pendconn *p;
100
101 p = pool_alloc(pendconn);
102 if (!p)
103 return NULL;
104
105 sess->pend_pos = p;
106 p->sess = sess;
107 p->srv = sess->srv;
108 if (sess->srv) {
109 LIST_ADDQ(&sess->srv->pendconns, &p->list);
110 sess->logs.srv_queue_size += sess->srv->nbpend;
111 sess->srv->nbpend++;
112 if (sess->srv->nbpend > sess->srv->nbpend_max)
113 sess->srv->nbpend_max = sess->srv->nbpend;
114 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200115 LIST_ADDQ(&sess->be->pendconns, &p->list);
116 sess->logs.prx_queue_size += sess->be->nbpend;
117 sess->be->nbpend++;
118 if (sess->be->nbpend > sess->be->nbpend_max)
119 sess->be->nbpend_max = sess->be->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200121 sess->be->totpend++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 return p;
123}
124
125/*
126 * Detaches pending connection <p>, decreases the pending count, and frees
127 * the pending connection. The connection might have been queued to a specific
128 * server as well as to the proxy. The session also gets marked unqueued.
129 */
130void pendconn_free(struct pendconn *p)
131{
132 LIST_DEL(&p->list);
133 p->sess->pend_pos = NULL;
134 if (p->srv)
135 p->srv->nbpend--;
136 else
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200137 p->sess->be->nbpend--;
138 p->sess->be->totpend--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139 pool_free(pendconn, p);
140}
141
142
143/*
144 * Local variables:
145 * c-indent-level: 8
146 * c-basic-offset: 8
147 * End:
148 */