]>
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 "comm/Connection.h"
37 #include "mgr/Registration.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 capacity_(PCONN_FDS_SZ
),
56 hash
.key
= xstrdup(key
);
57 theList_
= new Comm::ConnectionPointer
[capacity_
];
58 // TODO: re-attach to MemPools. WAS: theList = (?? *)pconn_fds_pool->alloc();
61 IdleConnList::~IdleConnList()
64 parent_
->unlinkList(this);
71 /** Search the list. Matches by FD socket number.
72 * Performed from the end of list where newest entries are.
74 * \retval <0 The connection is not listed
75 * \retval >=0 The connection array index
78 IdleConnList::findIndexOf(const Comm::ConnectionPointer
&conn
) const
80 for (int index
= size_
- 1; index
>= 0; --index
) {
81 if (conn
->fd
== theList_
[index
]->fd
) {
82 debugs(48, 3, HERE
<< "found " << conn
<< " at index " << index
);
87 debugs(48, 2, HERE
<< conn
<< " NOT FOUND!");
91 /** Remove the entry at specified index.
92 * May perform a shuffle of list entries to fill the gap.
93 * \retval false The index is not an in-use entry.
96 IdleConnList::removeAt(int index
)
98 if (index
< 0 || index
>= size_
)
101 // shuffle the remaining entries to fill the new gap.
102 for (; index
< size_
- 1; index
++)
103 theList_
[index
] = theList_
[index
+ 1];
104 theList_
[--size_
] = NULL
;
107 parent_
->noteConnectionRemoved();
109 debugs(48, 3, HERE
<< "deleting " << hashKeyStr(&hash
));
117 // almost a duplicate of removeFD. But drops multiple entries.
119 IdleConnList::closeN(size_t n
)
122 debugs(48, 2, HERE
<< "Nothing to do.");
124 } else if (n
>= (size_t)size_
) {
125 debugs(48, 2, HERE
<< "Closing all entries.");
127 const Comm::ConnectionPointer conn
= theList_
[--size_
];
128 theList_
[size_
] = NULL
;
132 parent_
->noteConnectionRemoved();
134 } else { //if (n < size_)
135 debugs(48, 2, HERE
<< "Closing " << n
<< " of " << size_
<< " entries.");
138 // ensure the first N entries are closed
139 for (index
= 0; index
< n
; index
++) {
140 const Comm::ConnectionPointer conn
= theList_
[index
];
141 theList_
[index
] = NULL
;
145 parent_
->noteConnectionRemoved();
147 // shuffle the list N down.
148 for (index
= 0; index
< (size_t)size_
- n
; index
++) {
149 theList_
[index
] = theList_
[index
+ n
];
151 // ensure the last N entries are unset
152 while (index
< ((size_t)size_
)) {
153 theList_
[index
++] = NULL
;
158 if (parent_
&& size_
== 0) {
159 debugs(48, 3, HERE
<< "deleting " << hashKeyStr(&hash
));
165 IdleConnList::clearHandlers(const Comm::ConnectionPointer
&conn
)
167 debugs(48, 3, HERE
<< "removing close handler for " << conn
);
168 comm_read_cancel(conn
->fd
, IdleConnList::Read
, this);
169 commUnsetConnTimeout(conn
);
173 IdleConnList::push(const Comm::ConnectionPointer
&conn
)
175 if (size_
== capacity_
) {
176 debugs(48, 3, HERE
<< "growing idle Connection array");
178 const Comm::ConnectionPointer
*oldList
= theList_
;
179 theList_
= new Comm::ConnectionPointer
[capacity_
];
180 for (int index
= 0; index
< size_
; index
++)
181 theList_
[index
] = oldList
[index
];
187 parent_
->noteConnectionAdded();
189 theList_
[size_
++] = conn
;
190 AsyncCall::Pointer readCall
= commCbCall(5,4, "IdleConnList::Read",
191 CommIoCbPtrFun(IdleConnList::Read
, this));
192 comm_read(conn
, fakeReadBuf_
, sizeof(fakeReadBuf_
), readCall
);
193 AsyncCall::Pointer timeoutCall
= commCbCall(5,4, "IdleConnList::Timeout",
194 CommTimeoutCbPtrFun(IdleConnList::Timeout
, this));
195 commSetConnTimeout(conn
, Config
.Timeout
.serverIdlePconn
, timeoutCall
);
198 /// Determine whether an entry in the idle list is available for use.
199 /// Returns false if the entry is unset, closed or closing.
201 IdleConnList::isAvailable(int i
) const
203 const Comm::ConnectionPointer
&conn
= theList_
[i
];
205 // connection already closed. useless.
206 if (!Comm::IsConnOpen(conn
))
209 // our connection early-read/close handler is scheduled to run already. unsafe
210 if (!COMMIO_FD_READCB(conn
->fd
)->active())
216 Comm::ConnectionPointer
219 for (int i
=size_
-1; i
>=0; i
--) {
224 // our connection timeout handler is scheduled to run already. unsafe for now.
225 // TODO: cancel the pending timeout callback and allow re-use of the conn.
226 if (fd_table
[theList_
[i
]->fd
].timeoutHandler
== NULL
)
229 // finally, a match. pop and return it.
230 Comm::ConnectionPointer result
= theList_
[i
];
231 /* may delete this */
233 clearHandlers(result
);
237 return Comm::ConnectionPointer();
241 * XXX this routine isn't terribly efficient - if there's a pending
242 * read event (which signifies the fd will close in the next IO loop!)
243 * we ignore the FD and move onto the next one. This means, as an example,
244 * if we have a lot of FDs open to a very popular server and we get a bunch
245 * of requests JUST as they timeout (say, it shuts down) we'll be wasting
246 * quite a bit of CPU. Just keep it in mind.
248 Comm::ConnectionPointer
249 IdleConnList::findUseable(const Comm::ConnectionPointer
&key
)
253 // small optimization: do the constant bool tests only once.
254 const bool keyCheckAddr
= !key
->local
.IsAnyAddr();
255 const bool keyCheckPort
= key
->local
.GetPort() > 0;
257 for (int i
=size_
-1; i
>=0; i
--) {
262 // local end port is required, but dont match.
263 if (keyCheckPort
&& key
->local
.GetPort() != theList_
[i
]->local
.GetPort())
266 // local address is required, but does not match.
267 if (keyCheckAddr
&& key
->local
.matchIPAddr(theList_
[i
]->local
) != 0)
270 // our connection timeout handler is scheduled to run already. unsafe for now.
271 // TODO: cancel the pending timeout callback and allow re-use of the conn.
272 if (fd_table
[theList_
[i
]->fd
].timeoutHandler
== NULL
)
275 // finally, a match. pop and return it.
276 Comm::ConnectionPointer result
= theList_
[i
];
277 /* may delete this */
279 clearHandlers(result
);
283 return Comm::ConnectionPointer();
286 /* might delete list */
288 IdleConnList::findAndClose(const Comm::ConnectionPointer
&conn
)
290 const int index
= findIndexOf(conn
);
292 /* might delete this */
300 IdleConnList::Read(const Comm::ConnectionPointer
&conn
, char *buf
, size_t len
, comm_err_t flag
, int xerrno
, void *data
)
302 debugs(48, 3, HERE
<< len
<< " bytes from " << conn
);
304 if (flag
== COMM_ERR_CLOSING
) {
305 debugs(48, 3, HERE
<< "COMM_ERR_CLOSING from " << conn
);
306 /* Bail out on COMM_ERR_CLOSING - may happen when shutdown aborts our idle FD */
310 IdleConnList
*list
= (IdleConnList
*) data
;
311 /* may delete list/data */
312 list
->findAndClose(conn
);
316 IdleConnList::Timeout(const CommTimeoutCbParams
&io
)
318 debugs(48, 3, HERE
<< io
.conn
);
319 IdleConnList
*list
= static_cast<IdleConnList
*>(io
.data
);
320 /* may delete list/data */
321 list
->findAndClose(io
.conn
);
324 /* ========== PconnPool PRIVATE FUNCTIONS ============================================ */
327 PconnPool::key(const Comm::ConnectionPointer
&destLink
, const char *domain
)
329 LOCAL_ARRAY(char, buf
, SQUIDHOSTNAMELEN
* 3 + 10);
331 destLink
->remote
.ToURL(buf
, SQUIDHOSTNAMELEN
* 3 + 10);
333 const int used
= strlen(buf
);
334 snprintf(buf
+used
, SQUIDHOSTNAMELEN
* 3 + 10-used
, "/%s", domain
);
337 debugs(48,6,"PconnPool::key(" << destLink
<< ", " << (domain
?domain
:"[no domain]") << ") is {" << buf
<< "}" );
342 PconnPool::dumpHist(StoreEntry
* e
) const
345 "%s persistent connection counts:\n"
349 "\t---- ---------\n",
352 for (int i
= 0; i
< PCONN_HIST_SZ
; i
++) {
356 storeAppendPrintf(e
, "\t%4d %9d\n", i
, hist
[i
]);
361 PconnPool::dumpHash(StoreEntry
*e
) const
363 hash_table
*hid
= table
;
367 for (hash_link
*walker
= hid
->next
; walker
; walker
= hash_next(hid
)) {
368 storeAppendPrintf(e
, "\t item %5d: %s\n", i
++, (char *)(walker
->key
));
372 /* ========== PconnPool PUBLIC FUNCTIONS ============================================ */
374 PconnPool::PconnPool(const char *aDescr
) : table(NULL
), descr(aDescr
),
378 table
= hash_create((HASHCMP
*) strcmp
, 229, hash_string
);
380 for (i
= 0; i
< PCONN_HIST_SZ
; i
++)
383 PconnModule::GetInstance()->add(this);
386 PconnPool::~PconnPool()
389 hashFreeMemory(table
);
393 PconnPool::push(const Comm::ConnectionPointer
&conn
, const char *domain
)
396 debugs(48, 3, HERE
<< "Not many unused FDs");
399 } else if (shutting_down
) {
401 debugs(48, 3, HERE
<< "Squid is shutting down. Refusing to do anything");
405 const char *aKey
= key(conn
, domain
);
406 IdleConnList
*list
= (IdleConnList
*) hash_lookup(table
, aKey
);
409 list
= new IdleConnList(aKey
, this);
410 debugs(48, 3, HERE
<< "new IdleConnList for {" << hashKeyStr(&list
->hash
) << "}" );
411 hash_join(table
, &list
->hash
);
413 debugs(48, 3, HERE
<< "found IdleConnList for {" << hashKeyStr(&list
->hash
) << "}" );
417 assert(!comm_has_incomplete_write(conn
->fd
));
419 LOCAL_ARRAY(char, desc
, FD_DESC_SZ
);
420 snprintf(desc
, FD_DESC_SZ
, "Idle server: %s", aKey
);
421 fd_note(conn
->fd
, desc
);
422 debugs(48, 3, HERE
<< "pushed " << conn
<< " for " << aKey
);
425 Comm::ConnectionPointer
426 PconnPool::pop(const Comm::ConnectionPointer
&destLink
, const char *domain
, bool isRetriable
)
428 const char * aKey
= key(destLink
, domain
);
430 IdleConnList
*list
= (IdleConnList
*)hash_lookup(table
, aKey
);
432 debugs(48, 3, HERE
<< "lookup for key {" << aKey
<< "} failed.");
433 return Comm::ConnectionPointer();
435 debugs(48, 3, HERE
<< "found " << hashKeyStr(&list
->hash
) << (isRetriable
?"(to use)":"(to kill)") );
438 /* may delete list */
439 Comm::ConnectionPointer temp
= list
->findUseable(destLink
);
440 if (!isRetriable
&& Comm::IsConnOpen(temp
))
447 PconnPool::closeN(int n
, const Comm::ConnectionPointer
&destLink
, const char *domain
)
449 // TODO: optimize: we can probably do hash_lookup just once
450 for (int i
= 0; i
< n
; ++i
)
451 pop(destLink
, domain
, false); // may fail!
455 PconnPool::unlinkList(IdleConnList
*list
)
457 theCount
-= list
->count();
458 assert(theCount
>= 0);
459 hash_remove_link(table
, &list
->hash
);
463 PconnPool::noteUses(int uses
)
465 if (uses
>= PCONN_HIST_SZ
)
466 uses
= PCONN_HIST_SZ
- 1;
471 /* ========== PconnModule ============================================ */
474 * This simple class exists only for the cache manager
477 PconnModule::PconnModule() : pools(NULL
), poolCount(0)
479 pools
= (PconnPool
**) xcalloc(MAX_NUM_PCONN_POOLS
, sizeof(*pools
));
480 //TODO: re-link to MemPools. WAS: pconn_fds_pool = memPoolCreate("pconn_fds", PCONN_FDS_SZ * sizeof(int));
481 debugs(48, 0, "persistent connection module initialized");
482 registerWithCacheManager();
486 PconnModule::GetInstance()
488 if (instance
== NULL
)
489 instance
= new PconnModule
;
495 PconnModule::registerWithCacheManager(void)
497 Mgr::RegisterAction("pconn",
498 "Persistent Connection Utilization Histograms",
503 PconnModule::add(PconnPool
*aPool
)
505 assert(poolCount
< MAX_NUM_PCONN_POOLS
);
506 *(pools
+poolCount
) = aPool
;
511 PconnModule::dump(StoreEntry
*e
)
515 for (i
= 0; i
< poolCount
; i
++) {
516 storeAppendPrintf(e
, "\n Pool %d Stats\n", i
);
517 (*(pools
+i
))->dumpHist(e
);
518 storeAppendPrintf(e
, "\n Pool %d Hash Table\n",i
);
519 (*(pools
+i
))->dumpHash(e
);
524 PconnModule::DumpWrapper(StoreEntry
*e
)
526 PconnModule::GetInstance()->dump(e
);