blob: 8a4696297465f0f4a527f0f09ab6f11190eef397 [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 Tarreauf54f8bd2008-11-23 19:53:55 +010029struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
31/*
32 * frees the context associated to a session. It must have been removed first.
33 */
34void session_free(struct session *s)
35{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +010036 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +020037 struct proxy *fe = s->fe;
Willy Tarreau0f7562b2007-01-07 15:46:13 +010038
Willy Tarreaubaaee002006-06-26 02:48:02 +020039 if (s->pend_pos)
40 pendconn_free(s->pend_pos);
Willy Tarreau1e62de62008-11-11 20:20:02 +010041 if (s->srv) { /* there may be requests left pending in queue */
42 if (s->flags & SN_CURR_SESS) {
43 s->flags &= ~SN_CURR_SESS;
44 s->srv->cur_sess--;
45 }
Willy Tarreau7c669d72008-06-20 15:04:11 +020046 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +010047 }
Willy Tarreau7c669d72008-06-20 15:04:11 +020048 if (unlikely(s->srv_conn)) {
49 /* the session still has a reserved slot on a server, but
50 * it should normally be only the same as the one above,
51 * so this should not happen in fact.
52 */
53 sess_change_server(s, NULL);
54 }
55
Willy Tarreau48d63db2008-08-03 17:41:33 +020056 pool_free2(pool2_buffer, s->req);
57 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau92fb9832007-10-16 17:34:28 +020059 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020060 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau41dff822007-01-01 23:32:30 +010061
Willy Tarreau92fb9832007-10-16 17:34:28 +020062 if (txn->rsp.cap != NULL) {
63 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020064 for (h = fe->rsp_cap; h; h = h->next)
65 pool_free2(h->pool, txn->rsp.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020066 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020067 }
Willy Tarreau92fb9832007-10-16 17:34:28 +020068 if (txn->req.cap != NULL) {
69 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020070 for (h = fe->req_cap; h; h = h->next)
71 pool_free2(h->pool, txn->req.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020072 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020073 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020074 }
Willy Tarreau48d63db2008-08-03 17:41:33 +020075 pool_free2(pool2_requri, txn->uri);
76 pool_free2(pool2_capture, txn->cli_cookie);
77 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010078 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020079 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +020080
81 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +020082 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020083 pool_flush2(pool2_buffer);
84 pool_flush2(fe->hdr_idx_pool);
85 pool_flush2(pool2_requri);
86 pool_flush2(pool2_capture);
87 pool_flush2(pool2_session);
88 pool_flush2(fe->req_cap_pool);
89 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +020090 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020091}
92
93
94/* perform minimal intializations, report 0 in case of error, 1 if OK. */
95int init_session()
96{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010097 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020098 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
99 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100}
101
Willy Tarreau30e71012007-11-26 20:15:35 +0100102void session_process_counters(struct session *s)
103{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100104 unsigned long long bytes;
105
Willy Tarreau30e71012007-11-26 20:15:35 +0100106 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100107 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100108 s->logs.bytes_in = s->req->total;
109 if (bytes) {
110 s->fe->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100111
Willy Tarreau30e71012007-11-26 20:15:35 +0100112 if (s->be != s->fe)
113 s->be->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100114
Willy Tarreau30e71012007-11-26 20:15:35 +0100115 if (s->srv)
116 s->srv->bytes_in += bytes;
117 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100118 }
119
Willy Tarreau30e71012007-11-26 20:15:35 +0100120 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100121 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100122 s->logs.bytes_out = s->rep->total;
123 if (bytes) {
124 s->fe->bytes_out += 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_out += 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_out += bytes;
131 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100132 }
133}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134
Willy Tarreau7c669d72008-06-20 15:04:11 +0200135/*
136 * This function adjusts sess->srv_conn and maintains the previous and new
137 * server's served session counts. Setting newsrv to NULL is enough to release
138 * current connection slot. This function also notifies any LB algo which might
139 * expect to be informed about any change in the number of active sessions on a
140 * server.
141 */
142void sess_change_server(struct session *sess, struct server *newsrv)
143{
144 if (sess->srv_conn == newsrv)
145 return;
146
147 if (sess->srv_conn) {
148 sess->srv_conn->served--;
149 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
150 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
151 sess->srv_conn = NULL;
152 }
153
154 if (newsrv) {
155 newsrv->served++;
156 if (newsrv->proxy->lbprm.server_take_conn)
157 newsrv->proxy->lbprm.server_take_conn(newsrv);
158 sess->srv_conn = newsrv;
159 }
160}
161
162
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163/*
164 * Local variables:
165 * c-indent-level: 8
166 * c-basic-offset: 8
167 * End:
168 */