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"
38 #include "comm/Connection.h"
43 #define PCONN_FDS_SZ 8 /* pconn set size, increase for better memcache hit rate */
45 //TODO: re-attach to MemPools. WAS: 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
) :
52 nfds_alloc(PCONN_FDS_SZ
),
56 hash
.key
= xstrdup(key
);
57 theList
= new Comm::ConnectionPointer
[nfds_alloc
];
58 // TODO: re-attach to MemPools. WAS: fds = (int *)pconn_fds_pool->alloc();
61 IdleConnList::~IdleConnList()
63 parent
->unlinkList(this);
65 /* TODO: re-attach to MemPools.
66 if (nfds_alloc == PCONN_FDS_SZ)
67 pconn_fds_pool->freeOne(theList);
76 IdleConnList::findIndex(const Comm::ConnectionPointer
&conn
)
78 for (int index
= nfds
- 1; index
>= 0; --index
) {
79 if (theList
[index
]->fd
== conn
->fd
)
87 IdleConnList::remove(const Comm::ConnectionPointer
&conn
)
89 int index
= findIndex(conn
);
91 debugs(48, 2, HERE
<< conn
<< " NOT FOUND!");
94 debugs(48, 3, HERE
<< "found " << conn
<< " at index " << index
);
96 for (; index
< nfds
- 1; index
++)
97 theList
[index
] = theList
[index
+ 1];
100 debugs(48, 3, "IdleConnList::removeFD: deleting " << hashKeyStr(&hash
));
107 IdleConnList::clearHandlers(const Comm::ConnectionPointer
&conn
)
109 comm_read_cancel(conn
->fd
, IdleConnList::read
, this);
110 commSetTimeout(conn
->fd
, -1, NULL
, NULL
);
114 IdleConnList::push(const Comm::ConnectionPointer
&conn
)
116 if (nfds
== nfds_alloc
) {
117 debugs(48, 3, "IdleConnList::push: growing FD array");
119 const Comm::ConnectionPointer
*oldList
= theList
;
120 theList
= new Comm::ConnectionPointer
[nfds_alloc
];
121 for (int index
= 0; index
< nfds
; index
++)
122 theList
[index
] = oldList
[index
];
124 /* TODO: re-attach to MemPools.
125 if (nfds == PCONN_FDS_SZ)
126 pconn_fds_pool->freeOne(oldList);
132 theList
[nfds
++] = conn
;
133 comm_read(conn
, fakeReadBuf
, sizeof(fakeReadBuf
), IdleConnList::read
, this);
134 commSetTimeout(conn
->fd
, Config
.Timeout
.pconn
, IdleConnList::timeout
, this);
138 * XXX this routine isn't terribly efficient - if there's a pending
139 * read event (which signifies the fd will close in the next IO loop!)
140 * we ignore the FD and move onto the next one. This means, as an example,
141 * if we have a lot of FDs open to a very popular server and we get a bunch
142 * of requests JUST as they timeout (say, it shuts down) we'll be wasting
143 * quite a bit of CPU. Just keep it in mind.
145 Comm::ConnectionPointer
146 IdleConnList::findUseable()
150 for (int i
=nfds
-1; i
>=0; i
--) {
151 if (!comm_has_pending_read_callback(theList
[i
]->fd
)) {
156 return Comm::ConnectionPointer();
160 IdleConnList::read(const Comm::ConnectionPointer
&conn
, char *buf
, size_t len
, comm_err_t flag
, int xerrno
, void *data
)
162 debugs(48, 3, HERE
<< len
<< " bytes from " << conn
);
164 if (flag
== COMM_ERR_CLOSING
) {
165 /* Bail out early on COMM_ERR_CLOSING - close handlers will tidy up for us */
169 IdleConnList
*list
= (IdleConnList
*) data
;
170 /* might delete list */
171 if (list
&& list
->remove(conn
)) {
172 Comm::ConnectionPointer nonConst
= conn
;
178 IdleConnList::timeout(int fd
, void *data
)
180 debugs(48, 3, "IdleConnList::timeout: FD " << fd
);
181 IdleConnList
*list
= (IdleConnList
*) data
;
182 Comm::ConnectionPointer temp
= new Comm::Connection
; // XXX: transition. make timeouts pass conn in
184 if (list
->remove(temp
)) {
187 temp
->fd
= -1; // XXX: transition. prevent temp erasure double-closing FD until timeout CB passess conn in.
190 /* ========== PconnPool PRIVATE FUNCTIONS ============================================ */
193 PconnPool::key(const Comm::ConnectionPointer
&destLink
, const char *domain
)
195 LOCAL_ARRAY(char, buf
, SQUIDHOSTNAMELEN
* 3 + 10);
197 destLink
->remote
.ToURL(buf
, SQUIDHOSTNAMELEN
* 3 + 10);
199 int used
= strlen(buf
);
200 snprintf(buf
+used
, SQUIDHOSTNAMELEN
* 3 + 10-used
, "/%s", domain
);
203 debugs(48,6,"PconnPool::key(" << destLink
<< ", " << (domain
?domain
:"[no domain]") << ") is {" << buf
<< "}" );
208 PconnPool::dumpHist(StoreEntry
* e
) const
211 "%s persistent connection counts:\n"
215 "\t---- ---------\n",
218 for (int i
= 0; i
< PCONN_HIST_SZ
; i
++) {
222 storeAppendPrintf(e
, "\t%4d %9d\n", i
, hist
[i
]);
227 PconnPool::dumpHash(StoreEntry
*e
) const
229 hash_table
*hid
= table
;
233 for (hash_link
*walker
= hid
->next
; walker
; walker
= hash_next(hid
)) {
234 storeAppendPrintf(e
, "\t item %5d: %s\n", i
++, (char *)(walker
->key
));
238 /* ========== PconnPool PUBLIC FUNCTIONS ============================================ */
240 PconnPool::PconnPool(const char *aDescr
) : table(NULL
), descr(aDescr
)
242 table
= hash_create((HASHCMP
*) strcmp
, 229, hash_string
);
244 for (int i
= 0; i
< PCONN_HIST_SZ
; i
++)
247 PconnModule::GetInstance()->add(this);
250 PconnPool::~PconnPool()
253 hashFreeMemory(table
);
257 PconnPool::push(const Comm::ConnectionPointer
&conn
, const char *domain
)
260 debugs(48, 3, "PconnPool::push: Not many unused FDs");
261 Comm::ConnectionPointer nonConst
= conn
;
264 } else if (shutting_down
) {
265 Comm::ConnectionPointer nonConst
= conn
;
267 debugs(48, 3, "PconnPool::push: Squid is shutting down. Refusing to do anything");
271 const char *aKey
= key(conn
, domain
);
272 IdleConnList
*list
= (IdleConnList
*) hash_lookup(table
, aKey
);
275 list
= new IdleConnList(aKey
, this);
276 debugs(48, 3, "PconnPool::push: new IdleConnList for {" << hashKeyStr(&list
->hash
) << "}" );
277 hash_join(table
, &list
->hash
);
279 debugs(48, 3, "PconnPool::push: found IdleConnList for {" << hashKeyStr(&list
->hash
) << "}" );
283 assert(!comm_has_incomplete_write(conn
->fd
));
285 LOCAL_ARRAY(char, desc
, FD_DESC_SZ
);
286 snprintf(desc
, FD_DESC_SZ
, "Idle: %s", aKey
);
287 fd_note(conn
->fd
, desc
);
288 debugs(48, 3, HERE
<< "pushed " << conn
<< " for " << aKey
);
292 PconnPool::pop(Comm::ConnectionPointer
&destLink
, const char *domain
, bool isRetriable
)
294 const char * aKey
= key(destLink
, domain
);
296 IdleConnList
*list
= (IdleConnList
*)hash_lookup(table
, aKey
);
298 debugs(48, 3, "PconnPool::pop: lookup for key {" << aKey
<< "} failed.");
301 debugs(48, 3, "PconnPool::pop: found " << hashKeyStr(&list
->hash
) << (isRetriable
?"(to use)":"(to kill)") );
304 Comm::ConnectionPointer temp
= list
->findUseable(); // search from the end. skip pending reads.
306 if (Comm::IsConnOpen(temp
)) {
307 list
->clearHandlers(temp
);
309 /* might delete list */
310 if (list
->remove(temp
) && !isRetriable
)
320 PconnPool::unlinkList(IdleConnList
*list
) const
322 hash_remove_link(table
, &list
->hash
);
326 PconnPool::count(int uses
)
328 if (uses
>= PCONN_HIST_SZ
)
329 uses
= PCONN_HIST_SZ
- 1;
334 /* ========== PconnModule ============================================ */
337 * This simple class exists only for the cache manager
340 PconnModule::PconnModule() : pools(NULL
), poolCount(0)
342 pools
= (PconnPool
**) xcalloc(MAX_NUM_PCONN_POOLS
, sizeof(*pools
));
343 //TODO: re-link to MemPools. WAS: pconn_fds_pool = memPoolCreate("pconn_fds", PCONN_FDS_SZ * sizeof(int));
344 debugs(48, 0, "persistent connection module initialized");
345 registerWithCacheManager();
349 PconnModule::GetInstance()
351 if (instance
== NULL
)
352 instance
= new PconnModule
;
358 PconnModule::registerWithCacheManager(void)
360 CacheManager::GetInstance()->
361 registerAction("pconn",
362 "Persistent Connection Utilization Histograms",
368 PconnModule::add(PconnPool
*aPool
)
370 assert(poolCount
< MAX_NUM_PCONN_POOLS
);
371 *(pools
+poolCount
) = aPool
;
376 PconnModule::dump(StoreEntry
*e
)
378 for (int i
= 0; i
< poolCount
; i
++) {
379 storeAppendPrintf(e
, "\n Pool %d Stats\n", i
);
380 (*(pools
+i
))->dumpHist(e
);
381 storeAppendPrintf(e
, "\n Pool %d Hash Table\n",i
);
382 (*(pools
+i
))->dumpHash(e
);
387 PconnModule::DumpWrapper(StoreEntry
*e
)
389 PconnModule::GetInstance()->dump(e
);