blob: a4670a80c389cd2193847c047e32daa1f0e41383 [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
36 * expected that 0 < s->minconn <= s->maxconn when this is called.
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 Tarreau86034312006-12-29 00:10:33 +010040 if (s->proxy->beconn >= s->proxy->fullconn)
41 /* no fullconn or proxy is full */
42 return s->maxconn;
43
44 if (s->minconn == s->maxconn)
45 /* static limit */
46 return s->maxconn;
47
48 return MAX(s->minconn,
49 s->proxy->beconn * s->maxconn / s->proxy->fullconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +020050}
51
52
53/*
54 * Manages a server's connection queue. If woken up, will try to dequeue as
55 * many pending sessions as possible, and wake them up. The task has nothing
Willy Tarreaud825eef2007-05-12 22:35:00 +020056 * else to do, so it always returns ETERNITY.
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 */
Willy Tarreaud825eef2007-05-12 22:35:00 +020058void process_srv_queue(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +020059{
60 struct server *s = (struct server*)t->context;
61 struct proxy *p = s->proxy;
62 int xferred;
63
64 /* First, check if we can handle some connections queued at the proxy. We
65 * will take as many as we can handle.
66 */
67 for (xferred = 0; s->cur_sess + xferred < srv_dynamic_maxconn(s); xferred++) {
68 struct session *sess;
69
70 sess = pendconn_get_next_sess(s, p);
71 if (sess == NULL)
72 break;
Willy Tarreau96bcfd72007-04-29 10:41:56 +020073 task_wakeup(sess->task);
Willy Tarreaubaaee002006-06-26 02:48:02 +020074 }
75
Willy Tarreaud825eef2007-05-12 22:35:00 +020076 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +020077}
78
79/* Detaches the next pending connection from either a server or a proxy, and
80 * returns its associated session. If no pending connection is found, NULL is
81 * returned. Note that neither <srv> nor <px> can be NULL.
82 */
83struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px)
84{
85 struct pendconn *p;
86 struct session *sess;
87
88 p = pendconn_from_srv(srv);
89 if (!p) {
90 p = pendconn_from_px(px);
91 if (!p)
92 return NULL;
93 p->sess->srv = srv;
94 }
95 sess = p->sess;
96 pendconn_free(p);
97 return sess;
98}
99
100/* Adds the session <sess> to the pending connection list of server <sess>->srv
101 * or to the one of <sess>->proxy if srv is NULL. All counters and back pointers
102 * are updated accordingly. Returns NULL if no memory is available, otherwise the
103 * pendconn itself.
104 */
105struct pendconn *pendconn_add(struct session *sess)
106{
107 struct pendconn *p;
108
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200109 p = pool_alloc2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110 if (!p)
111 return NULL;
112
113 sess->pend_pos = p;
114 p->sess = sess;
115 p->srv = sess->srv;
116 if (sess->srv) {
117 LIST_ADDQ(&sess->srv->pendconns, &p->list);
118 sess->logs.srv_queue_size += sess->srv->nbpend;
119 sess->srv->nbpend++;
120 if (sess->srv->nbpend > sess->srv->nbpend_max)
121 sess->srv->nbpend_max = sess->srv->nbpend;
122 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200123 LIST_ADDQ(&sess->be->pendconns, &p->list);
124 sess->logs.prx_queue_size += sess->be->nbpend;
125 sess->be->nbpend++;
126 if (sess->be->nbpend > sess->be->nbpend_max)
127 sess->be->nbpend_max = sess->be->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200128 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200129 sess->be->totpend++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200130 return p;
131}
132
133/*
134 * Detaches pending connection <p>, decreases the pending count, and frees
135 * the pending connection. The connection might have been queued to a specific
136 * server as well as to the proxy. The session also gets marked unqueued.
137 */
138void pendconn_free(struct pendconn *p)
139{
140 LIST_DEL(&p->list);
141 p->sess->pend_pos = NULL;
142 if (p->srv)
143 p->srv->nbpend--;
144 else
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200145 p->sess->be->nbpend--;
146 p->sess->be->totpend--;
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200147 pool_free2(pool2_pendconn, p);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200148}
149
150
151/*
152 * Local variables:
153 * c-indent-level: 8
154 * c-basic-offset: 8
155 * End:
156 */