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 | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 50 | pool_free2(pool2_buffer, s->req); |
| 51 | pool_free2(pool2_buffer, s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 53 | if (fe) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 54 | pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v); |
Willy Tarreau | 41dff82 | 2007-01-01 23:32:30 +0100 | [diff] [blame] | 55 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 56 | if (txn->rsp.cap != NULL) { |
| 57 | struct cap_hdr *h; |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 58 | for (h = fe->rsp_cap; h; h = h->next) |
| 59 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 60 | pool_free2(fe->rsp_cap_pool, txn->rsp.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 62 | if (txn->req.cap != NULL) { |
| 63 | struct cap_hdr *h; |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 64 | for (h = fe->req_cap; h; h = h->next) |
| 65 | pool_free2(h->pool, txn->req.cap[h->index]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 66 | pool_free2(fe->req_cap_pool, txn->req.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | } |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 69 | pool_free2(pool2_requri, txn->uri); |
| 70 | pool_free2(pool2_capture, txn->cli_cookie); |
| 71 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 72 | pool_free2(pool2_session, s); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 73 | |
| 74 | /* 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] | 75 | if (fe && unlikely(fe->state == PR_STSTOPPED)) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 76 | 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 Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 83 | } |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
| 87 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 88 | int init_session() |
| 89 | { |
| 90 | pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED); |
| 91 | return pool2_session != NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 94 | void session_process_counters(struct session *s) |
| 95 | { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 96 | unsigned long long bytes; |
| 97 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 98 | if (s->req) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 99 | bytes = s->req->total - s->logs.bytes_in; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 100 | s->logs.bytes_in = s->req->total; |
| 101 | if (bytes) { |
| 102 | s->fe->bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 103 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 104 | if (s->be != s->fe) |
| 105 | s->be->bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 106 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 107 | if (s->srv) |
| 108 | s->srv->bytes_in += bytes; |
| 109 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 112 | if (s->rep) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 113 | bytes = s->rep->total - s->logs.bytes_out; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 114 | s->logs.bytes_out = s->rep->total; |
| 115 | if (bytes) { |
| 116 | s->fe->bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 117 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 118 | if (s->be != s->fe) |
| 119 | s->be->bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 120 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 121 | if (s->srv) |
| 122 | s->srv->bytes_out += bytes; |
| 123 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 124 | } |
| 125 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 126 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 127 | /* |
| 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 | */ |
| 134 | void 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 155 | /* |
| 156 | * Local variables: |
| 157 | * c-indent-level: 8 |
| 158 | * c-basic-offset: 8 |
| 159 | * End: |
| 160 | */ |