]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/pconn.cc
3 * $Id: pconn.cc,v 1.55 2007/12/27 01:03:13 hno Exp $
5 * DEBUG: section 48 Persistent Connections
6 * AUTHOR: Duane Wessels
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
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.
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.
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.
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.
37 #include "CacheManager.h"
43 #define PCONN_FDS_SZ 8 /* pconn set size, increase for better memcache hit rate */
45 static MemAllocator
*pconn_fds_pool
= NULL
;
46 PconnModule
* PconnModule::instance
= NULL
;
47 CBDATA_CLASS_INIT(IdleConnList
);
49 /* ========== IdleConnList ============================================ */
51 IdleConnList::IdleConnList(const char *key
, PconnPool
*thePool
) : parent(thePool
)
53 hash
.key
= xstrdup(key
);
54 nfds_alloc
= PCONN_FDS_SZ
;
56 fds
= (int *)pconn_fds_pool
->alloc();
59 IdleConnList::~IdleConnList()
62 parent
->unlinkList(this);
64 if (nfds_alloc
== PCONN_FDS_SZ
)
65 pconn_fds_pool
->free(fds
);
73 IdleConnList::findFDIndex (int fd
)
77 for (index
= nfds
- 1; index
>= 0; --index
) {
86 IdleConnList::removeFD(int fd
)
88 int index
= findFDIndex(fd
);
90 debugs(48, 0, "IdleConnList::removeFD: FD " << fd
<< " NOT FOUND!");
93 debugs(48, 3, "IdleConnList::removeFD: found FD " << fd
<< " at index " << index
);
95 for (; index
< nfds
- 1; index
++)
96 fds
[index
] = fds
[index
+ 1];
99 debugs(48, 3, "IdleConnList::removeFD: deleting " << hashKeyStr(&hash
));
105 IdleConnList::clearHandlers(int fd
)
107 comm_read_cancel(fd
, IdleConnList::read
, this);
108 commSetTimeout(fd
, -1, NULL
, NULL
);
112 IdleConnList::push(int fd
)
114 if (nfds
== nfds_alloc
) {
115 debugs(48, 3, "IdleConnList::push: growing FD array");
118 fds
= (int *)xmalloc(nfds_alloc
* sizeof(int));
119 xmemcpy(fds
, old
, nfds
* sizeof(int));
121 if (nfds
== PCONN_FDS_SZ
)
122 pconn_fds_pool
->free(old
);
128 comm_read(fd
, fakeReadBuf
, sizeof(fakeReadBuf
), IdleConnList::read
, this);
129 commSetTimeout(fd
, Config
.Timeout
.pconn
, IdleConnList::timeout
, this);
133 * XXX this routine isn't terribly efficient - if there's a pending
134 * read event (which signifies the fd will close in the next IO loop!)
135 * we ignore the FD and move onto the next one. This means, as an example,
136 * if we have a lot of FDs open to a very popular server and we get a bunch
137 * of requests JUST as they timeout (say, it shuts down) we'll be wasting
138 * quite a bit of CPU. Just keep it in mind.
141 IdleConnList::findUseableFD()
145 for (int i
=nfds
-1; i
>=0; i
--) {
146 if (!comm_has_pending_read_callback(fds
[i
])) {
155 IdleConnList::read(int fd
, char *buf
, size_t len
, comm_err_t flag
, int xerrno
, void *data
)
157 debugs(48, 3, "IdleConnList::read: " << len
<< " bytes from FD " << fd
);
159 if (flag
== COMM_ERR_CLOSING
) {
160 /* Bail out early on COMM_ERR_CLOSING - close handlers will tidy up for us */
164 IdleConnList
*list
= (IdleConnList
*) data
;
165 list
->removeFD(fd
); /* might delete list */
170 IdleConnList::timeout(int fd
, void *data
)
172 debugs(48, 3, "IdleConnList::timeout: FD " << fd
);
173 IdleConnList
*list
= (IdleConnList
*) data
;
174 list
->removeFD(fd
); /* might delete list */
178 /* ========== PconnPool PRIVATE FUNCTIONS ============================================ */
181 PconnPool::key(const char *host
, u_short port
, const char *domain
, IPAddress
&client_address
)
183 LOCAL_ARRAY(char, buf
, SQUIDHOSTNAMELEN
* 2 + 10);
184 char ntoabuf
[MAX_IPSTRLEN
];
186 if (domain
&& !client_address
.IsAnyAddr())
187 snprintf(buf
, SQUIDHOSTNAMELEN
* 2 + 10, "%s:%d-%s/%s", host
, (int) port
, client_address
.NtoA(ntoabuf
,MAX_IPSTRLEN
), domain
);
188 else if (domain
&& client_address
.IsAnyAddr())
189 snprintf(buf
, SQUIDHOSTNAMELEN
* 2 + 10, "%s:%d/%s", host
, (int) port
, domain
);
190 else if ((!domain
) && !client_address
.IsAnyAddr())
191 snprintf(buf
, SQUIDHOSTNAMELEN
* 2 + 10, "%s:%d-%s", host
, (int) port
, client_address
.NtoA(ntoabuf
,MAX_IPSTRLEN
));
193 snprintf(buf
, SQUIDHOSTNAMELEN
* 2 + 10, "%s:%d", host
, (int) port
);
199 PconnPool::dumpHist(StoreEntry
* e
)
203 "%s persistent connection counts:\n"
207 "\t---- ---------\n",
210 for (i
= 0; i
< PCONN_HIST_SZ
; i
++) {
214 storeAppendPrintf(e
, "\t%4d %9d\n", i
, hist
[i
]);
218 /* ========== PconnPool PUBLIC FUNCTIONS ============================================ */
220 PconnPool::PconnPool(const char *aDescr
) : table(NULL
), descr(aDescr
)
223 table
= hash_create((HASHCMP
*) strcmp
, 229, hash_string
);
225 for (i
= 0; i
< PCONN_HIST_SZ
; i
++)
228 PconnModule::GetInstance()->add
233 PconnPool::push(int fd
, const char *host
, u_short port
, const char *domain
, IPAddress
&client_address
)
238 LOCAL_ARRAY(char, desc
, FD_DESC_SZ
);
242 debugs(48, 3, "PconnPool::push: Not many unused FDs");
245 } else if (shutting_down
)
251 aKey
= key(host
, port
, domain
, client_address
);
253 list
= (IdleConnList
*) hash_lookup(table
, aKey
);
257 list
= new IdleConnList(aKey
, this);
258 debugs(48, 3, "pconnNew: adding " << hashKeyStr(&list
->hash
));
259 hash_join(table
, &list
->hash
);
264 assert(!comm_has_incomplete_write(fd
));
265 snprintf(desc
, FD_DESC_SZ
, "%s idle connection", host
);
267 debugs(48, 3, "PconnPool::push: pushed FD " << fd
<< " for " << aKey
);
271 * Return a pconn fd for host:port if available and retriable.
272 * Otherwise, return -1.
274 * We close available persistent connection if the caller transaction is not
275 * retriable to avoid having a growing number of open connections when many
276 * transactions create persistent connections but are not retriable.
280 PconnPool::pop(const char *host
, u_short port
, const char *domain
, IPAddress
&client_address
, bool isRetriable
)
283 const char * aKey
= key(host
, port
, domain
, client_address
);
284 list
= (IdleConnList
*)hash_lookup(table
, aKey
);
289 int fd
= list
->findUseableFD(); // search from the end. skip pending reads.
293 list
->clearHandlers(fd
);
294 list
->removeFD(fd
); /* might delete list */
306 PconnPool::unlinkList(IdleConnList
*list
) const
308 hash_remove_link(table
, &list
->hash
);
312 PconnPool::count(int uses
)
314 if (uses
>= PCONN_HIST_SZ
)
315 uses
= PCONN_HIST_SZ
- 1;
320 /* ========== PconnModule ============================================ */
323 * This simple class exists only for the cache manager
326 PconnModule::PconnModule() : pools(NULL
), poolCount(0)
328 pools
= (PconnPool
**) xcalloc(MAX_NUM_PCONN_POOLS
, sizeof(*pools
));
329 pconn_fds_pool
= memPoolCreate("pconn_fds", PCONN_FDS_SZ
* sizeof(int));
330 debugs(48, 0, "persistent connection module initialized");
331 registerWithCacheManager();
335 PconnModule::GetInstance()
337 if (instance
== NULL
)
338 instance
= new PconnModule
;
344 PconnModule::registerWithCacheManager(void)
346 CacheManager::GetInstance()->
347 registerAction("pconn",
348 "Persistent Connection Utilization Histograms",
357 assert(poolCount
< MAX_NUM_PCONN_POOLS
);
358 *(pools
+poolCount
) = aPool
;
363 PconnModule::dump(StoreEntry
*e
)
367 for (i
= 0; i
< poolCount
; i
++) {
368 (*(pools
+i
))->dumpHist(e
);
373 PconnModule::DumpWrapper(StoreEntry
*e
)
375 PconnModule::GetInstance()->dump(e
);