blob: e3a736d2bcca7cd4a95ab564807b6952ac9fec54 [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
19#include <types/backend.h>
20#include <types/capture.h>
21#include <types/log.h>
22#include <types/proxy.h>
23#include <types/server.h>
24
Willy Tarreau7341d942007-05-13 19:56:02 +020025#include <proto/buffers.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010026#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020027#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <proto/session.h>
29#include <proto/queue.h>
30
31
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020032struct pool_head *pool2_session;
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
34/*
35 * frees the context associated to a session. It must have been removed first.
36 */
37void session_free(struct session *s)
38{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +010039 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +020040 struct proxy *fe = s->fe;
Willy Tarreau0f7562b2007-01-07 15:46:13 +010041
Willy Tarreaubaaee002006-06-26 02:48:02 +020042 if (s->pend_pos)
43 pendconn_free(s->pend_pos);
Willy Tarreau7c669d72008-06-20 15:04:11 +020044 if (s->srv) /* there may be requests left pending in queue */
45 process_srv_queue(s->srv);
46 if (unlikely(s->srv_conn)) {
47 /* the session still has a reserved slot on a server, but
48 * it should normally be only the same as the one above,
49 * so this should not happen in fact.
50 */
51 sess_change_server(s, NULL);
52 }
53
Willy Tarreaubaaee002006-06-26 02:48:02 +020054 if (s->req)
Willy Tarreau7341d942007-05-13 19:56:02 +020055 pool_free2(pool2_buffer, s->req);
Willy Tarreaubaaee002006-06-26 02:48:02 +020056 if (s->rep)
Willy Tarreau7341d942007-05-13 19:56:02 +020057 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau92fb9832007-10-16 17:34:28 +020059 if (fe) {
60 if (txn->hdr_idx.v != NULL)
61 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau41dff822007-01-01 23:32:30 +010062
Willy Tarreau92fb9832007-10-16 17:34:28 +020063 if (txn->rsp.cap != NULL) {
64 struct cap_hdr *h;
65 for (h = fe->rsp_cap; h; h = h->next) {
66 if (txn->rsp.cap[h->index] != NULL)
67 pool_free2(h->pool, txn->rsp.cap[h->index]);
68 }
69 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020070 }
Willy Tarreau92fb9832007-10-16 17:34:28 +020071 if (txn->req.cap != NULL) {
72 struct cap_hdr *h;
73 for (h = fe->req_cap; h; h = h->next) {
74 if (txn->req.cap[h->index] != NULL)
75 pool_free2(h->pool, txn->req.cap[h->index]);
76 }
77 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020078 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020079 }
Willy Tarreau3bac9ff2007-03-18 17:31:28 +010080 if (txn->uri)
Willy Tarreau332f8bf2007-05-13 21:36:56 +020081 pool_free2(pool2_requri, txn->uri);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +010082 if (txn->cli_cookie)
Willy Tarreau086b3b42007-05-13 21:45:51 +020083 pool_free2(pool2_capture, txn->cli_cookie);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +010084 if (txn->srv_cookie)
Willy Tarreau086b3b42007-05-13 21:45:51 +020085 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaubaaee002006-06-26 02:48:02 +020086
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020087 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +020088
89 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +020090 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau632f5a72007-07-11 10:42:35 +020091 if (pool2_buffer)
92 pool_flush2(pool2_buffer);
93 if (fe->hdr_idx_pool)
94 pool_flush2(fe->hdr_idx_pool);
95 if (pool2_requri)
96 pool_flush2(pool2_requri);
97 if (pool2_capture)
98 pool_flush2(pool2_capture);
99 if (pool2_session)
100 pool_flush2(pool2_session);
101 if (fe->req_cap_pool)
102 pool_flush2(fe->req_cap_pool);
103 if (fe->rsp_cap_pool)
104 pool_flush2(fe->rsp_cap_pool);
105 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200106}
107
108
109/* perform minimal intializations, report 0 in case of error, 1 if OK. */
110int init_session()
111{
112 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
113 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114}
115
Willy Tarreau30e71012007-11-26 20:15:35 +0100116void session_process_counters(struct session *s)
117{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100118 unsigned long long bytes;
119
Willy Tarreau30e71012007-11-26 20:15:35 +0100120 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100121 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100122 s->logs.bytes_in = s->req->total;
123 if (bytes) {
124 s->fe->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100125
Willy Tarreau30e71012007-11-26 20:15:35 +0100126 if (s->be != s->fe)
127 s->be->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100128
Willy Tarreau30e71012007-11-26 20:15:35 +0100129 if (s->srv)
130 s->srv->bytes_in += bytes;
131 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100132 }
133
Willy Tarreau30e71012007-11-26 20:15:35 +0100134 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100135 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100136 s->logs.bytes_out = s->rep->total;
137 if (bytes) {
138 s->fe->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100139
Willy Tarreau30e71012007-11-26 20:15:35 +0100140 if (s->be != s->fe)
141 s->be->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100142
Willy Tarreau30e71012007-11-26 20:15:35 +0100143 if (s->srv)
144 s->srv->bytes_out += bytes;
145 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100146 }
147}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200148
Willy Tarreau7c669d72008-06-20 15:04:11 +0200149/*
150 * This function adjusts sess->srv_conn and maintains the previous and new
151 * server's served session counts. Setting newsrv to NULL is enough to release
152 * current connection slot. This function also notifies any LB algo which might
153 * expect to be informed about any change in the number of active sessions on a
154 * server.
155 */
156void sess_change_server(struct session *sess, struct server *newsrv)
157{
158 if (sess->srv_conn == newsrv)
159 return;
160
161 if (sess->srv_conn) {
162 sess->srv_conn->served--;
163 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
164 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
165 sess->srv_conn = NULL;
166 }
167
168 if (newsrv) {
169 newsrv->served++;
170 if (newsrv->proxy->lbprm.server_take_conn)
171 newsrv->proxy->lbprm.server_take_conn(newsrv);
172 sess->srv_conn = newsrv;
173 }
174}
175
176
Willy Tarreaubaaee002006-06-26 02:48:02 +0200177/*
178 * Local variables:
179 * c-indent-level: 8
180 * c-basic-offset: 8
181 * End:
182 */