]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ResolvedPeers.cc
f7620134e2b704599abc2ab50166d67839c91e1e
2 * Copyright (C) 1996-2019 The Squid Software Foundation and contributors
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.
10 #include "CachePeer.h"
11 #include "comm/Connection.h"
12 #include "comm/ConnOpener.h"
13 #include "ResolvedPeers.h"
14 #include "SquidConfig.h"
16 ResolvedPeers::ResolvedPeers()
18 if (Config
.forward_max_tries
> 0)
19 paths_
.reserve(Config
.forward_max_tries
);
23 ResolvedPeers::retryPath(const Comm::ConnectionPointer
&path
)
27 paths_
.emplace(paths_
.begin(), path
);
31 ResolvedPeers::addPath(const Comm::ConnectionPointer
&path
)
33 paths_
.emplace_back(path
);
36 Comm::ConnectionPointer
37 ResolvedPeers::extractFront()
40 return extractFound("first: ", paths_
.begin());
43 Comm::ConnectionPointer
44 ResolvedPeers::extractPrime(const Comm::Connection
¤tPeer
)
47 const auto peerToMatch
= currentPeer
.getPeer();
48 const auto familyToMatch
= ConnectionFamily(currentPeer
);
49 const auto &conn
= paths_
.front();
50 if (conn
->getPeer() == peerToMatch
&& familyToMatch
== ConnectionFamily(*conn
))
51 return extractFound("same-peer same-family match: ", paths_
.begin());
54 debugs(17, 7, "no same-peer same-family paths");
58 /// If spare paths exist for currentPeer, returns the first spare path iterator.
59 /// Otherwise, if there are paths for other peers, returns one of those.
60 /// Otherwise, returns the end() iterator.
61 Comm::ConnectionList::iterator
62 ResolvedPeers::findSpareOrNextPeer(const Comm::Connection
¤tPeer
)
64 const auto peerToMatch
= currentPeer
.getPeer();
65 const auto familyToAvoid
= ConnectionFamily(currentPeer
);
66 // Optimization: Also stop at the first mismatching peer because all
67 // same-peer paths are grouped together.
68 return std::find_if(paths_
.begin(), paths_
.end(),
69 [peerToMatch
, familyToAvoid
](const Comm::ConnectionPointer
&conn
) {
70 return peerToMatch
!= conn
->getPeer() ||
71 familyToAvoid
!= ConnectionFamily(*conn
);
75 Comm::ConnectionPointer
76 ResolvedPeers::extractSpare(const Comm::Connection
¤tPeer
)
78 auto found
= findSpareOrNextPeer(currentPeer
);
79 if (found
!= paths_
.end() && currentPeer
.getPeer() == (*found
)->getPeer())
80 return extractFound("same-peer different-family match: ", found
);
82 debugs(17, 7, "no same-peer different-family paths");
86 /// convenience method to finish a successful extract*() call
87 Comm::ConnectionPointer
88 ResolvedPeers::extractFound(const char *description
, const Comm::ConnectionList::iterator
&found
)
90 const auto path
= *found
;
92 debugs(17, 7, description
<< path
);
97 ResolvedPeers::haveSpare(const Comm::Connection
¤tPeer
)
99 const auto found
= findSpareOrNextPeer(currentPeer
);
100 return found
!= paths_
.end() &&
101 currentPeer
.getPeer() == (*found
)->getPeer();
105 ResolvedPeers::doneWithSpares(const Comm::Connection
¤tPeer
)
107 const auto found
= findSpareOrNextPeer(currentPeer
);
108 if (found
== paths_
.end())
109 return destinationsFinalized
;
110 return currentPeer
.getPeer() != (*found
)->getPeer();
114 ResolvedPeers::doneWithPrimes(const Comm::Connection
¤tPeer
) const
116 const auto first
= paths_
.begin();
117 if (first
== paths_
.end())
118 return destinationsFinalized
;
119 return currentPeer
.getPeer() != (*first
)->getPeer() ||
120 ConnectionFamily(currentPeer
) != ConnectionFamily(**first
);
124 ResolvedPeers::doneWithPeer(const Comm::Connection
¤tPeer
) const
126 const auto first
= paths_
.begin();
127 if (first
== paths_
.end())
128 return destinationsFinalized
;
129 return currentPeer
.getPeer() != (*first
)->getPeer();
133 ResolvedPeers::ConnectionFamily(const Comm::Connection
&conn
)
135 return conn
.remote
.isIPv4() ? AF_INET
: AF_INET6
;
139 operator <<(std::ostream
&os
, const ResolvedPeers
&peers
)
142 return os
<< "[no paths]";
143 return os
<< peers
.size() << (peers
.destinationsFinalized
? "" : "+") << " paths";