Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Server management functions. |
| 3 | * |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 14 | |
| 15 | #include <common/config.h> |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 16 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 17 | #include <common/memory.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 21 | #include <proto/buffers.h> |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 22 | #include <proto/hdr_idx.h> |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 23 | #include <proto/log.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | #include <proto/session.h> |
| 25 | #include <proto/queue.h> |
| 26 | |
| 27 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 28 | struct pool_head *pool2_session; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * frees the context associated to a session. It must have been removed first. |
| 32 | */ |
| 33 | void session_free(struct session *s) |
| 34 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 35 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 36 | struct proxy *fe = s->fe; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 37 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | if (s->pend_pos) |
| 39 | pendconn_free(s->pend_pos); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 40 | 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | if (s->req) |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 51 | pool_free2(pool2_buffer, s->req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | if (s->rep) |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 53 | pool_free2(pool2_buffer, s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 55 | if (fe) { |
| 56 | if (txn->hdr_idx.v != NULL) |
| 57 | pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v); |
Willy Tarreau | 41dff82 | 2007-01-01 23:32:30 +0100 | [diff] [blame] | 58 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 59 | 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 67 | 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 75 | } |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 76 | if (txn->uri) |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 77 | pool_free2(pool2_requri, txn->uri); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 78 | if (txn->cli_cookie) |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 79 | pool_free2(pool2_capture, txn->cli_cookie); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 80 | if (txn->srv_cookie) |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 81 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 83 | pool_free2(pool2_session, s); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 84 | |
| 85 | /* We may want to free the maximum amount of pools if the proxy is stopping */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 86 | if (fe && unlikely(fe->state == PR_STSTOPPED)) { |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 87 | 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 Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
| 105 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 106 | int init_session() |
| 107 | { |
| 108 | pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED); |
| 109 | return pool2_session != NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 112 | void session_process_counters(struct session *s) |
| 113 | { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 114 | unsigned long long bytes; |
| 115 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 116 | if (s->req) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 117 | bytes = s->req->total - s->logs.bytes_in; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 118 | s->logs.bytes_in = s->req->total; |
| 119 | if (bytes) { |
| 120 | s->fe->bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 121 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 122 | if (s->be != s->fe) |
| 123 | s->be->bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 124 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 125 | if (s->srv) |
| 126 | s->srv->bytes_in += bytes; |
| 127 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 130 | if (s->rep) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 131 | bytes = s->rep->total - s->logs.bytes_out; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 132 | s->logs.bytes_out = s->rep->total; |
| 133 | if (bytes) { |
| 134 | s->fe->bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 135 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 136 | if (s->be != s->fe) |
| 137 | s->be->bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 138 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 139 | if (s->srv) |
| 140 | s->srv->bytes_out += bytes; |
| 141 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 142 | } |
| 143 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 144 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 145 | /* |
| 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 | */ |
| 152 | void 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 173 | /* |
| 174 | * Local variables: |
| 175 | * c-indent-level: 8 |
| 176 | * c-basic-offset: 8 |
| 177 | * End: |
| 178 | */ |