blob: ceef0b7dfe7edc99c2afc6b0c161f43f0a6a2a7d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 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
13#include <stdlib.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020014
15#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020016#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020017#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020018
Willy Tarreaubaaee002006-06-26 02:48:02 +020019#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreau7341d942007-05-13 19:56:02 +020021#include <proto/buffers.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010022#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020023#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <proto/session.h>
25#include <proto/queue.h>
26
27
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020028struct pool_head *pool2_session;
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
30/*
31 * frees the context associated to a session. It must have been removed first.
32 */
33void session_free(struct session *s)
34{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +010035 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +020036 struct proxy *fe = s->fe;
Willy Tarreau0f7562b2007-01-07 15:46:13 +010037
Willy Tarreaubaaee002006-06-26 02:48:02 +020038 if (s->pend_pos)
39 pendconn_free(s->pend_pos);
Willy Tarreau7c669d72008-06-20 15:04:11 +020040 if (s->srv) /* there may be requests left pending in queue */
41 process_srv_queue(s->srv);
42 if (unlikely(s->srv_conn)) {
43 /* the session still has a reserved slot on a server, but
44 * it should normally be only the same as the one above,
45 * so this should not happen in fact.
46 */
47 sess_change_server(s, NULL);
48 }
49
Willy Tarreau48d63db2008-08-03 17:41:33 +020050 pool_free2(pool2_buffer, s->req);
51 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +020052
Willy Tarreau92fb9832007-10-16 17:34:28 +020053 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020054 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau41dff822007-01-01 23:32:30 +010055
Willy Tarreau92fb9832007-10-16 17:34:28 +020056 if (txn->rsp.cap != NULL) {
57 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020058 for (h = fe->rsp_cap; h; h = h->next)
59 pool_free2(h->pool, txn->rsp.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020060 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020061 }
Willy Tarreau92fb9832007-10-16 17:34:28 +020062 if (txn->req.cap != NULL) {
63 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020064 for (h = fe->req_cap; h; h = h->next)
65 pool_free2(h->pool, txn->req.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020066 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020067 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020068 }
Willy Tarreau48d63db2008-08-03 17:41:33 +020069 pool_free2(pool2_requri, txn->uri);
70 pool_free2(pool2_capture, txn->cli_cookie);
71 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020072 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +020073
74 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +020075 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020076 pool_flush2(pool2_buffer);
77 pool_flush2(fe->hdr_idx_pool);
78 pool_flush2(pool2_requri);
79 pool_flush2(pool2_capture);
80 pool_flush2(pool2_session);
81 pool_flush2(fe->req_cap_pool);
82 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +020083 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020084}
85
86
87/* perform minimal intializations, report 0 in case of error, 1 if OK. */
88int init_session()
89{
90 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
91 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020092}
93
Willy Tarreau30e71012007-11-26 20:15:35 +010094void session_process_counters(struct session *s)
95{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +010096 unsigned long long bytes;
97
Willy Tarreau30e71012007-11-26 20:15:35 +010098 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +010099 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100100 s->logs.bytes_in = s->req->total;
101 if (bytes) {
102 s->fe->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100103
Willy Tarreau30e71012007-11-26 20:15:35 +0100104 if (s->be != s->fe)
105 s->be->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100106
Willy Tarreau30e71012007-11-26 20:15:35 +0100107 if (s->srv)
108 s->srv->bytes_in += bytes;
109 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100110 }
111
Willy Tarreau30e71012007-11-26 20:15:35 +0100112 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100113 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100114 s->logs.bytes_out = s->rep->total;
115 if (bytes) {
116 s->fe->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100117
Willy Tarreau30e71012007-11-26 20:15:35 +0100118 if (s->be != s->fe)
119 s->be->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100120
Willy Tarreau30e71012007-11-26 20:15:35 +0100121 if (s->srv)
122 s->srv->bytes_out += bytes;
123 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100124 }
125}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126
Willy Tarreau7c669d72008-06-20 15:04:11 +0200127/*
128 * This function adjusts sess->srv_conn and maintains the previous and new
129 * server's served session counts. Setting newsrv to NULL is enough to release
130 * current connection slot. This function also notifies any LB algo which might
131 * expect to be informed about any change in the number of active sessions on a
132 * server.
133 */
134void sess_change_server(struct session *sess, struct server *newsrv)
135{
136 if (sess->srv_conn == newsrv)
137 return;
138
139 if (sess->srv_conn) {
140 sess->srv_conn->served--;
141 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
142 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
143 sess->srv_conn = NULL;
144 }
145
146 if (newsrv) {
147 newsrv->served++;
148 if (newsrv->proxy->lbprm.server_take_conn)
149 newsrv->proxy->lbprm.server_take_conn(newsrv);
150 sess->srv_conn = newsrv;
151 }
152}
153
154
Willy Tarreaubaaee002006-06-26 02:48:02 +0200155/*
156 * Local variables:
157 * c-indent-level: 8
158 * c-basic-offset: 8
159 * End:
160 */