]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/pconn.cc
4 * DEBUG: section 48 Persistent Connections
5 * AUTHOR: Duane Wessels
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 #include "CacheManager.h"
42 #define PCONN_FDS_SZ 8 /* pconn set size, increase for better memcache hit rate */
44 static MemAllocator
*pconn_fds_pool
= NULL
;
45 PconnModule
* PconnModule::instance
= NULL
;
46 CBDATA_CLASS_INIT(IdleConnList
);
48 /* ========== IdleConnList ============================================ */
50 IdleConnList::IdleConnList(const char *key
, PconnPool
*thePool
) : parent(thePool
)
52 hash
.key
= xstrdup(key
);
53 nfds_alloc
= PCONN_FDS_SZ
;
55 fds
= (int *)pconn_fds_pool
->alloc();
58 IdleConnList::~IdleConnList()
61 parent
->unlinkList(this);
63 if (nfds_alloc
== PCONN_FDS_SZ
)
64 pconn_fds_pool
->free(fds
);
72 IdleConnList::findFDIndex (int fd
)
76 for (index
= nfds
- 1; index
>= 0; --index
) {
85 IdleConnList::removeFD(int fd
)
87 int index
= findFDIndex(fd
);
89 debugs(48, 0, "IdleConnList::removeFD: FD " << fd
<< " NOT FOUND!");
92 debugs(48, 3, "IdleConnList::removeFD: found FD " << fd
<< " at index " << index
);
94 for (; index
< nfds
- 1; index
++)
95 fds
[index
] = fds
[index
+ 1];
98 debugs(48, 3, "IdleConnList::removeFD: deleting " << hashKeyStr(&hash
));
104 IdleConnList::clearHandlers(int fd
)
106 comm_read_cancel(fd
, IdleConnList::read
, this);
107 commSetTimeout(fd
, -1, NULL
, NULL
);
111 IdleConnList::push(int fd
)
113 if (nfds
== nfds_alloc
) {
114 debugs(48, 3, "IdleConnList::push: growing FD array");
117 fds
= (int *)xmalloc(nfds_alloc
* sizeof(int));
118 xmemcpy(fds
, old
, nfds
* sizeof(int));
120 if (nfds
== PCONN_FDS_SZ
)
121 pconn_fds_pool
->free(old
);
127 comm_read(fd
, fakeReadBuf
, sizeof(fakeReadBuf
), IdleConnList::read
, this);
128 commSetTimeout(fd
, Config
.Timeout
.pconn
, IdleConnList::timeout
, this);
132 * XXX this routine isn't terribly efficient - if there's a pending
133 * read event (which signifies the fd will close in the next IO loop!)
134 * we ignore the FD and move onto the next one. This means, as an example,
135 * if we have a lot of FDs open to a very popular server and we get a bunch
136 * of requests JUST as they timeout (say, it shuts down) we'll be wasting
137 * quite a bit of CPU. Just keep it in mind.
140 IdleConnList::findUseableFD()
144 for (int i
=nfds
-1; i
>=0; i
--) {
145 if (!comm_has_pending_read_callback(fds
[i
])) {
154 IdleConnList::read(int fd
, char *buf
, size_t len
, comm_err_t flag
, int xerrno
, void *data
)
156 debugs(48, 3, "IdleConnList::read: " << len
<< " bytes from FD " << fd
);
158 if (flag
== COMM_ERR_CLOSING
) {
159 /* Bail out early on COMM_ERR_CLOSING - close handlers will tidy up for us */
163 IdleConnList
*list
= (IdleConnList
*) data
;
164 list
->removeFD(fd
); /* might delete list */
169 IdleConnList::timeout(int fd
, void *data
)
171 debugs(48, 3, "IdleConnList::timeout: FD " << fd
);
172 IdleConnList
*list
= (IdleConnList
*) data
;
173 list
->removeFD(fd
); /* might delete list */
177 /* ========== PconnPool PRIVATE FUNCTIONS ============================================ */
180 PconnPool::key(const char *host
, u_short port
, const char *domain
, IpAddress
&client_address
)
182 LOCAL_ARRAY(char, buf
, SQUIDHOSTNAMELEN
* 3 + 10);
183 char ntoabuf
[MAX_IPSTRLEN
];
185 if (domain
&& !client_address
.IsAnyAddr())
186 snprintf(buf
, SQUIDHOSTNAMELEN
* 3 + 10, "%s:%d-%s/%s", host
, (int) port
, client_address
.NtoA(ntoabuf
,MAX_IPSTRLEN
), domain
);
187 else if (domain
&& client_address
.IsAnyAddr())
188 snprintf(buf
, SQUIDHOSTNAMELEN
* 3 + 10, "%s:%d/%s", host
, (int) port
, domain
);
189 else if ((!domain
) && !client_address
.IsAnyAddr())
190 snprintf(buf
, SQUIDHOSTNAMELEN
* 3 + 10, "%s:%d-%s", host
, (int) port
, client_address
.NtoA(ntoabuf
,MAX_IPSTRLEN
));
192 snprintf(buf
, SQUIDHOSTNAMELEN
* 3 + 10, "%s:%d", host
, (int) port
);
194 debugs(48,6,"PconnPool::key(" << host
<< "," << port
<< "," << domain
<< "," << client_address
<< "is {" << buf
<< "}" );
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
]);
219 PconnPool::dumpHash(StoreEntry
*e
)
222 hash_link
*walker
= NULL
;
223 hash_table
*hid
= table
;
226 for (i
= 0, walker
= hid
->next
; walker
; walker
= hash_next(hid
)) {
227 storeAppendPrintf(e
, "\t item %5d: %s\n", i
++, (char *)(walker
->key
));
231 /* ========== PconnPool PUBLIC FUNCTIONS ============================================ */
233 PconnPool::PconnPool(const char *aDescr
) : table(NULL
), descr(aDescr
)
236 table
= hash_create((HASHCMP
*) strcmp
, 229, hash_string
);
238 for (i
= 0; i
< PCONN_HIST_SZ
; i
++)
241 PconnModule::GetInstance()->add(this);
244 PconnPool::~PconnPool()
247 hashFreeMemory(table
);
251 PconnPool::push(int fd
, const char *host
, u_short port
, const char *domain
, IpAddress
&client_address
)
255 LOCAL_ARRAY(char, desc
, FD_DESC_SZ
);
258 debugs(48, 3, "PconnPool::push: Not many unused FDs");
261 } else if (shutting_down
) {
263 debugs(48, 3, "PconnPool::push: Squid is shutting down. Refusing to do anything");
267 aKey
= key(host
, port
, domain
, client_address
);
269 list
= (IdleConnList
*) hash_lookup(table
, aKey
);
272 list
= new IdleConnList(aKey
, this);
273 debugs(48, 3, "PconnPool::push: new IdleConnList for {" << hashKeyStr(&list
->hash
) << "}" );
274 hash_join(table
, &list
->hash
);
276 debugs(48, 3, "PconnPool::push: found IdleConnList for {" << hashKeyStr(&list
->hash
) << "}" );
281 assert(!comm_has_incomplete_write(fd
));
282 snprintf(desc
, FD_DESC_SZ
, "%s idle connection", host
);
284 debugs(48, 3, "PconnPool::push: pushed FD " << fd
<< " for " << aKey
);
288 * Return a pconn fd for host:port if available and retriable.
289 * Otherwise, return -1.
291 * We close available persistent connection if the caller transaction is not
292 * retriable to avoid having a growing number of open connections when many
293 * transactions create persistent connections but are not retriable.
296 PconnPool::pop(const char *host
, u_short port
, const char *domain
, IpAddress
&client_address
, bool isRetriable
)
298 const char * aKey
= key(host
, port
, domain
, client_address
);
300 IdleConnList
*list
= (IdleConnList
*)hash_lookup(table
, aKey
);
302 debugs(48, 3, "PconnPool::pop: lookup for key {" << aKey
<< "} failed.");
305 debugs(48, 3, "PconnPool::pop: found " << hashKeyStr(&list
->hash
) << (isRetriable
?"(to use)":"(to kill)") );
308 int fd
= list
->findUseableFD(); // search from the end. skip pending reads.
311 list
->clearHandlers(fd
);
312 list
->removeFD(fd
); /* might delete list */
324 PconnPool::unlinkList(IdleConnList
*list
) const
326 hash_remove_link(table
, &list
->hash
);
330 PconnPool::count(int uses
)
332 if (uses
>= PCONN_HIST_SZ
)
333 uses
= PCONN_HIST_SZ
- 1;
338 /* ========== PconnModule ============================================ */
341 * This simple class exists only for the cache manager
344 PconnModule::PconnModule() : pools(NULL
), poolCount(0)
346 pools
= (PconnPool
**) xcalloc(MAX_NUM_PCONN_POOLS
, sizeof(*pools
));
347 pconn_fds_pool
= memPoolCreate("pconn_fds", PCONN_FDS_SZ
* sizeof(int));
348 debugs(48, 0, "persistent connection module initialized");
349 registerWithCacheManager();
353 PconnModule::GetInstance()
355 if (instance
== NULL
)
356 instance
= new PconnModule
;
362 PconnModule::registerWithCacheManager(void)
364 CacheManager::GetInstance()->
365 registerAction("pconn",
366 "Persistent Connection Utilization Histograms",
375 assert(poolCount
< MAX_NUM_PCONN_POOLS
);
376 *(pools
+poolCount
) = aPool
;
381 PconnModule::dump(StoreEntry
*e
)
385 for (i
= 0; i
< poolCount
; i
++) {
386 storeAppendPrintf(e
, "\n Pool %d Stats\n", i
);
387 (*(pools
+i
))->dumpHist(e
);
388 storeAppendPrintf(e
, "\n Pool %d Hash Table\n",i
);
389 (*(pools
+i
))->dumpHash(e
);
394 PconnModule::DumpWrapper(StoreEntry
*e
)
396 PconnModule::GetInstance()->dump(e
);