]> git.ipfire.org Git - thirdparty/squid.git/blob - src/pconn.cc
SSL->HTTP gatewaying support by Benno Rice
[thirdparty/squid.git] / src / pconn.cc
1
2 /*
3 * $Id: pconn.cc,v 1.31 2001/04/14 00:03:23 hno Exp $
4 *
5 * DEBUG: section 48 Persistent Connections
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid.h"
37
38 struct _pconn {
39 hash_link hash; /* must be first */
40 int *fds;
41 int nfds_alloc;
42 int nfds;
43 };
44
45 #define PCONN_FDS_SZ 8 /* pconn set size, increase for better memcache hit rate */
46 #define PCONN_HIST_SZ (1<<16)
47 int client_pconn_hist[PCONN_HIST_SZ];
48 int server_pconn_hist[PCONN_HIST_SZ];
49
50 static PF pconnRead;
51 static PF pconnTimeout;
52 static const char *pconnKey(const char *host, u_short port);
53 static hash_table *table = NULL;
54 static struct _pconn *pconnNew(const char *key);
55 static void pconnDelete(struct _pconn *p);
56 static void pconnRemoveFD(struct _pconn *p, int fd);
57 static OBJH pconnHistDump;
58 static MemPool *pconn_data_pool = NULL;
59 static MemPool *pconn_fds_pool = NULL;
60
61 static const char *
62 pconnKey(const char *host, u_short port)
63 {
64 LOCAL_ARRAY(char, buf, SQUIDHOSTNAMELEN + 10);
65 snprintf(buf, SQUIDHOSTNAMELEN + 10, "%s.%d", host, (int) port);
66 return buf;
67 }
68
69 static struct _pconn *
70 pconnNew(const char *key)
71 {
72 struct _pconn *p = memPoolAlloc(pconn_data_pool);
73 p->hash.key = xstrdup(key);
74 p->nfds_alloc = PCONN_FDS_SZ;
75 p->fds = memPoolAlloc(pconn_fds_pool);
76 debug(48, 3) ("pconnNew: adding %s\n", hashKeyStr(&p->hash));
77 hash_join(table, &p->hash);
78 return p;
79 }
80
81 static void
82 pconnDelete(struct _pconn *p)
83 {
84 debug(48, 3) ("pconnDelete: deleting %s\n", hashKeyStr(&p->hash));
85 hash_remove_link(table, (hash_link *) p);
86 if (p->nfds_alloc == PCONN_FDS_SZ)
87 memPoolFree(pconn_fds_pool, p->fds);
88 else
89 xfree(p->fds);
90 xfree(p->hash.key);
91 memPoolFree(pconn_data_pool, p);
92 }
93
94 static void
95 pconnRemoveFD(struct _pconn *p, int fd)
96 {
97 int i;
98 for (i = 0; i < p->nfds; i++) {
99 if (p->fds[i] == fd)
100 break;
101 }
102 assert(i < p->nfds);
103 debug(48, 3) ("pconnRemoveFD: found FD %d at index %d\n", fd, i);
104 for (; i < p->nfds - 1; i++)
105 p->fds[i] = p->fds[i + 1];
106 if (--p->nfds == 0)
107 pconnDelete(p);
108 }
109
110 static void
111 pconnTimeout(int fd, void *data)
112 {
113 struct _pconn *p = data;
114 assert(table != NULL);
115 debug(48, 3) ("pconnTimeout: FD %d %s\n", fd, hashKeyStr(&p->hash));
116 pconnRemoveFD(p, fd);
117 comm_close(fd);
118 }
119
120 static void
121 pconnRead(int fd, void *data)
122 {
123 LOCAL_ARRAY(char, buf, 256);
124 struct _pconn *p = data;
125 int n;
126 assert(table != NULL);
127 statCounter.syscalls.sock.reads++;
128 n = FD_READ_METHOD(fd, buf, 256);
129 debug(48, 3) ("pconnRead: %d bytes from FD %d, %s\n", n, fd,
130 hashKeyStr(&p->hash));
131 pconnRemoveFD(p, fd);
132 comm_close(fd);
133 }
134
135 static void
136 pconnHistDump(StoreEntry * e)
137 {
138 int i;
139 storeAppendPrintf(e,
140 "Client-side persistent connection counts:\n"
141 "\n"
142 "\treq/\n"
143 "\tconn count\n"
144 "\t---- ---------\n");
145 for (i = 0; i < PCONN_HIST_SZ; i++) {
146 if (client_pconn_hist[i] == 0)
147 continue;
148 storeAppendPrintf(e, "\t%4d %9d\n", i, client_pconn_hist[i]);
149 }
150 storeAppendPrintf(e,
151 "\n"
152 "Server-side persistent connection counts:\n"
153 "\n"
154 "\treq/\n"
155 "\tconn count\n"
156 "\t---- ---------\n");
157 for (i = 0; i < PCONN_HIST_SZ; i++) {
158 if (server_pconn_hist[i] == 0)
159 continue;
160 storeAppendPrintf(e, "\t%4d %9d\n", i, server_pconn_hist[i]);
161 }
162 }
163
164 /* ========== PUBLIC FUNCTIONS ============================================ */
165
166
167 void
168 pconnInit(void)
169 {
170 int i;
171 assert(table == NULL);
172 table = hash_create((HASHCMP *) strcmp, 229, hash_string);
173 for (i = 0; i < PCONN_HIST_SZ; i++) {
174 client_pconn_hist[i] = 0;
175 server_pconn_hist[i] = 0;
176 }
177 pconn_data_pool = memPoolCreate("pconn_data", sizeof(struct _pconn));
178 pconn_fds_pool = memPoolCreate("pconn_fds", PCONN_FDS_SZ * sizeof(int));
179
180 cachemgrRegister("pconn",
181 "Persistent Connection Utilization Histograms",
182 pconnHistDump, 0, 1);
183 debug(48, 3) ("persistent connection module initialized\n");
184 }
185
186 void
187 pconnPush(int fd, const char *host, u_short port)
188 {
189 struct _pconn *p;
190 int *old;
191 LOCAL_ARRAY(char, key, SQUIDHOSTNAMELEN + 10);
192 LOCAL_ARRAY(char, desc, FD_DESC_SZ);
193 if (fdNFree() < (RESERVED_FD << 1)) {
194 debug(48, 3) ("pconnPush: Not many unused FDs\n");
195 comm_close(fd);
196 return;
197 } else if (shutting_down) {
198 comm_close(fd);
199 return;
200 }
201 assert(table != NULL);
202 strcpy(key, pconnKey(host, port));
203 p = (struct _pconn *) hash_lookup(table, key);
204 if (p == NULL)
205 p = pconnNew(key);
206 if (p->nfds == p->nfds_alloc) {
207 debug(48, 3) ("pconnPush: growing FD array\n");
208 p->nfds_alloc <<= 1;
209 old = p->fds;
210 p->fds = xmalloc(p->nfds_alloc * sizeof(int));
211 xmemcpy(p->fds, old, p->nfds * sizeof(int));
212 if (p->nfds == PCONN_FDS_SZ)
213 memPoolFree(pconn_fds_pool, old);
214 else
215 xfree(old);
216 }
217 p->fds[p->nfds++] = fd;
218 commSetSelect(fd, COMM_SELECT_READ, pconnRead, p, 0);
219 commSetTimeout(fd, Config.Timeout.pconn, pconnTimeout, p);
220 snprintf(desc, FD_DESC_SZ, "%s idle connection", host);
221 fd_note(fd, desc);
222 debug(48, 3) ("pconnPush: pushed FD %d for %s\n", fd, key);
223 }
224
225 int
226 pconnPop(const char *host, u_short port)
227 {
228 struct _pconn *p;
229 hash_link *hptr;
230 int fd = -1;
231 LOCAL_ARRAY(char, key, SQUIDHOSTNAMELEN + 10);
232 assert(table != NULL);
233 strcpy(key, pconnKey(host, port));
234 hptr = hash_lookup(table, key);
235 if (hptr != NULL) {
236 p = (struct _pconn *) hptr;
237 assert(p->nfds > 0);
238 fd = p->fds[0];
239 pconnRemoveFD(p, fd);
240 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
241 commSetTimeout(fd, -1, NULL, NULL);
242 }
243 return fd;
244 }
245
246 void
247 pconnHistCount(int what, int i)
248 {
249 if (i >= PCONN_HIST_SZ)
250 i = PCONN_HIST_SZ - 1;
251 /* what == 0 for client, 1 for server */
252 if (what == 0)
253 client_pconn_hist[i]++;
254 else if (what == 1)
255 server_pconn_hist[i]++;
256 else
257 assert(0);
258 }