]> git.ipfire.org Git - thirdparty/squid.git/blame - src/CachePeer.cc
Do not use invasive lists to store CachePeers (#1424)
[thirdparty/squid.git] / src / CachePeer.cc
CommitLineData
719815a0 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
719815a0
AJ
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 "acl/Gadgets.h"
11#include "CachePeer.h"
719815a0 12#include "defines.h"
022dbabd 13#include "neighbors.h"
719815a0
AJ
14#include "NeighborTypeDomainList.h"
15#include "pconn.h"
97269a72 16#include "PeerDigest.h"
719815a0 17#include "PeerPoolMgr.h"
5f5d319e 18#include "SquidConfig.h"
a555a85b 19#include "util.h"
719815a0
AJ
20
21CBDATA_CLASS_INIT(CachePeer);
22
a555a85b
AR
23CachePeer::CachePeer(const char * const hostname):
24 name(xstrdup(hostname)),
25 host(xstrdup(hostname))
26{
27 Tolower(host); // but .name preserves original spelling
28}
29
719815a0
AJ
30CachePeer::~CachePeer()
31{
32 xfree(name);
33 xfree(host);
34
719815a0
AJ
35 while (NeighborTypeDomainList *l = typelist) {
36 typelist = l->next;
37 xfree(l->domain);
38 xfree(l);
39 }
40
41 aclDestroyAccessList(&access);
42
43#if USE_CACHE_DIGESTS
97269a72
EB
44 void *digestTmp = nullptr;
45 if (cbdataReferenceValidDone(digest, &digestTmp))
46 peerDigestNotePeerGone(static_cast<PeerDigest *>(digestTmp));
719815a0
AJ
47 xfree(digest_url);
48#endif
49
719815a0
AJ
50 xfree(login);
51
52 delete standby.pool;
53
54 // the mgr job will notice that its owner is gone and stop
55 PeerPoolMgr::Checkpoint(standby.mgr, "peer gone");
56
57 xfree(domain);
719815a0 58}
7dfc5092 59
022dbabd
EB
60void
61CachePeer::noteSuccess()
62{
63 if (!tcp_up) {
64 debugs(15, 2, "connection to " << *this << " succeeded");
65 tcp_up = connect_fail_limit; // NP: so peerAlive() works properly.
66 peerAlive(this);
67 } else {
68 tcp_up = connect_fail_limit;
69 }
70}
71
72void
73CachePeer::noteFailure(const Http::StatusCode code)
74{
75 if (Http::Is4xx(code))
76 return; // this failure is not our fault
77
78 countFailure();
79}
80
81// TODO: Require callers to detail failures instead of using one (and often
82// misleading!) "connection failed" phrase for all of them.
83/// noteFailure() helper for handling failures attributed to this peer
84void
85CachePeer::countFailure()
86{
87 stats.last_connect_failure = squid_curtime;
88 if (tcp_up > 0)
89 --tcp_up;
90
91 const auto consideredAliveByAdmin = (stats.logged_state == PEER_ALIVE);
92 const auto level = consideredAliveByAdmin ? DBG_IMPORTANT : 2;
93 debugs(15, level, "ERROR: Connection to " << *this << " failed");
94
95 if (consideredAliveByAdmin) {
96 if (!tcp_up) {
97 debugs(15, DBG_IMPORTANT, "Detected DEAD " << neighborTypeStr(this) << ": " << name);
98 stats.logged_state = PEER_DEAD;
99 } else {
100 debugs(15, 2, "additional failures needed to mark this cache_peer DEAD: " << tcp_up);
101 }
102 } else {
103 assert(!tcp_up);
104 debugs(15, 2, "cache_peer " << *this << " is still DEAD");
105 }
106}
107
a555a85b
AR
108void
109CachePeer::rename(const char * const newName)
110{
111 if (!newName || !*newName)
112 throw TextException("cache_peer name=value cannot be empty", Here());
113
114 xfree(name);
115 name = xstrdup(newName);
116}
117
5f5d319e
FC
118time_t
119CachePeer::connectTimeout() const
120{
121 if (connect_timeout_raw > 0)
122 return connect_timeout_raw;
123 return Config.Timeout.peer_connect;
124}
125
a555a85b
AR
126std::ostream &
127operator <<(std::ostream &os, const CachePeer &p)
128{
129 return os << p.name;
130}
131