]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/AcceptLimiter.cc
Source Format Enforcement (#963)
[thirdparty/squid.git] / src / comm / AcceptLimiter.cc
1 /*
2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10 #include "comm/AcceptLimiter.h"
11 #include "comm/Connection.h"
12 #include "comm/TcpAcceptor.h"
13 #include "fde.h"
14 #include "globals.h"
15
16 Comm::AcceptLimiter Comm::AcceptLimiter::Instance_;
17
18 Comm::AcceptLimiter &
19 Comm::AcceptLimiter::Instance()
20 {
21 return Instance_;
22 }
23
24 void
25 Comm::AcceptLimiter::defer(const Comm::TcpAcceptor::Pointer &afd)
26 {
27 debugs(5, 5, afd->conn << "; already queued: " << deferred_.size());
28 deferred_.push_back(afd);
29 }
30
31 void
32 Comm::AcceptLimiter::removeDead(const Comm::TcpAcceptor::Pointer &afd)
33 {
34 for (auto it = deferred_.begin(); it != deferred_.end(); ++it) {
35 if (*it == afd) {
36 *it = nullptr; // fast. kick() will skip empty entries later.
37 debugs(5,4, "Abandoned client TCP SYN by closing socket: " << afd->conn);
38 return;
39 }
40 }
41 debugs(5,4, "Not found " << afd->conn << " in queue, size: " << deferred_.size());
42 }
43
44 void
45 Comm::AcceptLimiter::kick()
46 {
47 debugs(5, 5, "size=" << deferred_.size());
48 while (deferred_.size() > 0 && Comm::TcpAcceptor::okToAccept()) {
49 /* NP: shift() is equivalent to pop_front(). Giving us a FIFO queue. */
50 TcpAcceptor::Pointer temp = deferred_.front();
51 deferred_.erase(deferred_.begin());
52 if (temp.valid()) {
53 debugs(5, 5, "doing one.");
54 temp->acceptNext();
55 break;
56 }
57 }
58 }
59