blob: c27ef75790b1f0ba0b265cc1747d0c8e3ccbeb40 [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 Tarreaubaaee002006-06-26 02:48:02 +020050 if (s->req)
Willy Tarreau7341d942007-05-13 19:56:02 +020051 pool_free2(pool2_buffer, s->req);
Willy Tarreaubaaee002006-06-26 02:48:02 +020052 if (s->rep)
Willy Tarreau7341d942007-05-13 19:56:02 +020053 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
Willy Tarreau92fb9832007-10-16 17:34:28 +020055 if (fe) {
56 if (txn->hdr_idx.v != NULL)
57 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau41dff822007-01-01 23:32:30 +010058
Willy Tarreau92fb9832007-10-16 17:34:28 +020059 if (txn->rsp.cap != NULL) {
60 struct cap_hdr *h;
61 for (h = fe->rsp_cap; h; h = h->next) {
62 if (txn->rsp.cap[h->index] != NULL)
63 pool_free2(h->pool, txn->rsp.cap[h->index]);
64 }
65 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020066 }
Willy Tarreau92fb9832007-10-16 17:34:28 +020067 if (txn->req.cap != NULL) {
68 struct cap_hdr *h;
69 for (h = fe->req_cap; h; h = h->next) {
70 if (txn->req.cap[h->index] != NULL)
71 pool_free2(h->pool, txn->req.cap[h->index]);
72 }
73 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020074 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020075 }
Willy Tarreau3bac9ff2007-03-18 17:31:28 +010076 if (txn->uri)
Willy Tarreau332f8bf2007-05-13 21:36:56 +020077 pool_free2(pool2_requri, txn->uri);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +010078 if (txn->cli_cookie)
Willy Tarreau086b3b42007-05-13 21:45:51 +020079 pool_free2(pool2_capture, txn->cli_cookie);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +010080 if (txn->srv_cookie)
Willy Tarreau086b3b42007-05-13 21:45:51 +020081 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaubaaee002006-06-26 02:48:02 +020082
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020083 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +020084
85 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +020086 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau632f5a72007-07-11 10:42:35 +020087 if (pool2_buffer)
88 pool_flush2(pool2_buffer);
89 if (fe->hdr_idx_pool)
90 pool_flush2(fe->hdr_idx_pool);
91 if (pool2_requri)
92 pool_flush2(pool2_requri);
93 if (pool2_capture)
94 pool_flush2(pool2_capture);
95 if (pool2_session)
96 pool_flush2(pool2_session);
97 if (fe->req_cap_pool)
98 pool_flush2(fe->req_cap_pool);
99 if (fe->rsp_cap_pool)
100 pool_flush2(fe->rsp_cap_pool);
101 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200102}
103
104
105/* perform minimal intializations, report 0 in case of error, 1 if OK. */
106int init_session()
107{
108 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
109 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110}
111
Willy Tarreau30e71012007-11-26 20:15:35 +0100112void session_process_counters(struct session *s)
113{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100114 unsigned long long bytes;
115
Willy Tarreau30e71012007-11-26 20:15:35 +0100116 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100117 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100118 s->logs.bytes_in = s->req->total;
119 if (bytes) {
120 s->fe->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100121
Willy Tarreau30e71012007-11-26 20:15:35 +0100122 if (s->be != s->fe)
123 s->be->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100124
Willy Tarreau30e71012007-11-26 20:15:35 +0100125 if (s->srv)
126 s->srv->bytes_in += bytes;
127 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100128 }
129
Willy Tarreau30e71012007-11-26 20:15:35 +0100130 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100131 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100132 s->logs.bytes_out = s->rep->total;
133 if (bytes) {
134 s->fe->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100135
Willy Tarreau30e71012007-11-26 20:15:35 +0100136 if (s->be != s->fe)
137 s->be->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100138
Willy Tarreau30e71012007-11-26 20:15:35 +0100139 if (s->srv)
140 s->srv->bytes_out += bytes;
141 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100142 }
143}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200144
Willy Tarreau7c669d72008-06-20 15:04:11 +0200145/*
146 * This function adjusts sess->srv_conn and maintains the previous and new
147 * server's served session counts. Setting newsrv to NULL is enough to release
148 * current connection slot. This function also notifies any LB algo which might
149 * expect to be informed about any change in the number of active sessions on a
150 * server.
151 */
152void sess_change_server(struct session *sess, struct server *newsrv)
153{
154 if (sess->srv_conn == newsrv)
155 return;
156
157 if (sess->srv_conn) {
158 sess->srv_conn->served--;
159 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
160 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
161 sess->srv_conn = NULL;
162 }
163
164 if (newsrv) {
165 newsrv->served++;
166 if (newsrv->proxy->lbprm.server_take_conn)
167 newsrv->proxy->lbprm.server_take_conn(newsrv);
168 sess->srv_conn = newsrv;
169 }
170}
171
172
Willy Tarreaubaaee002006-06-26 02:48:02 +0200173/*
174 * Local variables:
175 * c-indent-level: 8
176 * c-basic-offset: 8
177 * End:
178 */