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.
9 /* DEBUG: section 44 Peer Selection Algorithm */
12 #include "acl/FilledChecklist.h"
13 #include "base/InstanceId.h"
14 #include "CachePeer.h"
16 #include "client_side.h"
17 #include "dns/LookupDetails.h"
18 #include "errorpage.h"
22 #include "hier_code.h"
24 #include "http/Stream.h"
25 #include "HttpRequest.h"
26 #include "icmp/net_db.h"
30 #include "neighbors.h"
31 #include "peer_sourcehash.h"
32 #include "peer_userhash.h"
33 #include "PeerSelectState.h"
34 #include "SquidConfig.h"
35 #include "SquidTime.h"
39 * A CachePeer which has been selected as a possible destination.
40 * Listed as pointers here so as to prevent duplicates being added but will
41 * be converted to a set of IP address path options before handing back out
44 * Certain connection flags and outgoing settings will also be looked up and
45 * set based on the received request and CachePeer settings before handing back.
49 MEMPROXY_CLASS(FwdServer
);
52 FwdServer(CachePeer
*p
, hier_code c
) :
58 CbcPointer
<CachePeer
> _peer
; /* NULL --> origin server */
67 static const char *DirectStr
[] = {
74 /// a helper class to report a selected destination (for debugging)
75 class PeerSelectionDumper
78 PeerSelectionDumper(const PeerSelector
* const aSelector
, const CachePeer
* const aPeer
, const hier_code aCode
):
79 selector(aSelector
), peer(aPeer
), code(aCode
) {}
81 const PeerSelector
* const selector
; ///< selection parameters
82 const CachePeer
* const peer
; ///< successful selection info
83 const hier_code code
; ///< selection algorithm
86 CBDATA_CLASS_INIT(PeerSelector
);
88 /// prints PeerSelectionDumper (for debugging)
90 operator <<(std::ostream
&os
, const PeerSelectionDumper
&fsd
)
92 os
<< hier_code_str
[fsd
.code
];
95 os
<< '/' << fsd
.peer
->host
;
96 else if (fsd
.selector
) // useful for DIRECT and gone PINNED destinations
97 os
<< '#' << fsd
.selector
->request
->url
.host();
102 PeerSelector::~PeerSelector()
105 FwdServer
*next
= servers
->next
;
111 debugs(44, 3, entry
->url());
113 if (entry
->ping_status
== PING_WAITING
)
114 eventDelete(HandlePingTimeout
, this);
116 entry
->ping_status
= PING_DONE
;
120 debugs(44, DBG_IMPORTANT
, "BUG: peer selector gone while waiting for a slow ACL");
121 delete acl_checklist
;
124 HTTPMSGUNLOCK(request
);
127 assert(entry
->ping_status
!= PING_WAITING
);
128 entry
->unlock("peerSelect");
136 peerSelectIcpPing(PeerSelector
*ps
, int direct
, StoreEntry
* entry
)
139 HttpRequest
*request
= ps
->request
;
143 assert(entry
->ping_status
== PING_NONE
);
144 assert(direct
!= DIRECT_YES
);
145 debugs(44, 3, entry
->url());
147 if (!request
->flags
.hierarchical
&& direct
!= DIRECT_NO
)
150 if (EBIT_TEST(entry
->flags
, KEY_PRIVATE
) && !neighbors_do_private_keys
)
151 if (direct
!= DIRECT_NO
)
154 n
= neighborsCount(ps
);
156 debugs(44, 3, "counted " << n
<< " neighbors");
162 peerSelect(PeerSelectionInitiator
*initiator
,
163 HttpRequest
* request
,
164 AccessLogEntry::Pointer
const &al
,
168 debugs(44, 3, *entry
<< ' ' << entry
->url());
170 debugs(44, 3, request
->method
);
172 const auto selector
= new PeerSelector(initiator
);
174 selector
->request
= request
;
175 HTTPMSGLOCK(selector
->request
);
178 selector
->entry
= entry
;
180 #if USE_CACHE_DIGESTS
182 request
->hier
.peer_select_start
= current_time
;
187 selector
->entry
->lock("peerSelect");
189 selector
->selectMore();
193 PeerSelectionInitiator::startSelectingDestinations(HttpRequest
*request
, const AccessLogEntry::Pointer
&ale
, StoreEntry
*entry
)
196 peerSelect(this, request
, ale
, entry
);
197 // and wait for noteDestination() and/or noteDestinationsEnd() calls
201 PeerSelector::checkNeverDirectDone(const allow_t answer
)
203 acl_checklist
= nullptr;
204 debugs(44, 3, answer
);
205 never_direct
= answer
;
208 /** if never_direct says YES, do that. */
210 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (never_direct allow)");
212 case ACCESS_DENIED
: // not relevant.
213 case ACCESS_DUNNO
: // not relevant.
215 case ACCESS_AUTH_REQUIRED
:
216 debugs(44, DBG_IMPORTANT
, "WARNING: never_direct resulted in " << answer
<< ". Username ACLs are not reliable here.");
223 PeerSelector::CheckNeverDirectDone(allow_t answer
, void *data
)
225 static_cast<PeerSelector
*>(data
)->checkNeverDirectDone(answer
);
229 PeerSelector::checkAlwaysDirectDone(const allow_t answer
)
231 acl_checklist
= nullptr;
232 debugs(44, 3, answer
);
233 always_direct
= answer
;
236 /** if always_direct says YES, do that. */
238 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (always_direct allow)");
240 case ACCESS_DENIED
: // not relevant.
241 case ACCESS_DUNNO
: // not relevant.
243 case ACCESS_AUTH_REQUIRED
:
244 debugs(44, DBG_IMPORTANT
, "WARNING: always_direct resulted in " << answer
<< ". Username ACLs are not reliable here.");
251 PeerSelector::CheckAlwaysDirectDone(allow_t answer
, void *data
)
253 static_cast<PeerSelector
*>(data
)->checkAlwaysDirectDone(answer
);
256 /// \returns true (after destroying "this") if the peer initiator is gone
257 /// \returns false (without side effects) otherwise
259 PeerSelector::selectionAborted()
261 if (interestedInitiator())
264 debugs(44, 3, "Aborting peer selection: Initiator gone or lost interest.");
269 /// A single DNS resolution loop iteration: Converts selected FwdServer to IPs.
271 PeerSelector::resolveSelected()
273 if (selectionAborted())
276 FwdServer
*fs
= servers
;
278 // Bug 3243: CVE 2009-0801
279 // Bypass of browser same-origin access control in intercepted communication
280 // To resolve this we must use only the original client destination when going DIRECT
281 // on intercepted traffic which failed Host verification
282 const HttpRequest
*req
= request
;
283 const bool isIntercepted
= !req
->flags
.redirected
&&
284 (req
->flags
.intercepted
|| req
->flags
.interceptTproxy
);
285 const bool useOriginalDst
= Config
.onoff
.client_dst_passthru
|| !req
->flags
.hostVerified
;
286 const bool choseDirect
= fs
&& fs
->code
== HIER_DIRECT
;
287 if (isIntercepted
&& useOriginalDst
&& choseDirect
) {
288 // check the client is still around before using any of its details
289 if (req
->clientConnectionManager
.valid()) {
290 // construct a "result" adding the ORIGINAL_DST to the set instead of DIRECT
291 Comm::ConnectionPointer p
= new Comm::Connection();
292 p
->remote
= req
->clientConnectionManager
->clientConnection
->local
;
293 fs
->code
= ORIGINAL_DST
; // fs->code is DIRECT. This fixes the display.
297 // clear the used fs and continue
304 if (fs
&& fs
->code
== PINNED
) {
305 // Nil path signals a PINNED destination selection. Our initiator should
306 // borrow and use clientConnectionManager's pinned connection object
307 // (regardless of that connection destination).
308 handlePath(nullptr, *fs
);
315 // convert the list of FwdServer destinations into destinations IP addresses
316 if (fs
&& wantsMoreDestinations()) {
317 // send the next one off for DNS lookup.
318 const char *host
= fs
->_peer
.valid() ? fs
->_peer
->host
: request
->url
.host();
319 debugs(44, 2, "Find IP destination for: " << url() << "' via " << host
);
320 Dns::nbgethostbyname(host
, this);
324 // Bug 3605: clear any extra listed FwdServer destinations, when the options exceeds max_foward_tries.
325 // due to the allocation method of fs, we must deallocate each manually.
326 // TODO: use a std::list so we can get the size and abort adding whenever the selection loops reach Config.forward_max_tries
328 assert(fs
== servers
);
336 // done with DNS lookups. pass back to caller
338 debugs(44, 2, id
<< " found all " << foundPaths
<< " destinations for " << url());
339 debugs(44, 2, " always_direct = " << always_direct
);
340 debugs(44, 2, " never_direct = " << never_direct
);
341 debugs(44, 2, " timedout = " << ping
.timedout
);
343 ping
.stop
= current_time
;
344 request
->hier
.ping
= ping
; // final result
346 if (lastError
&& foundPaths
) {
347 // nobody cares about errors if we found destinations despite them
348 debugs(44, 3, "forgetting the last error");
353 if (const auto initiator
= interestedInitiator())
354 initiator
->noteDestinationsEnd(lastError
);
355 lastError
= nullptr; // initiator owns the ErrorState object now
360 PeerSelector::noteLookup(const Dns::LookupDetails
&details
)
362 /* ignore lookup delays that occurred after the initiator moved on */
364 if (selectionAborted())
367 if (!wantsMoreDestinations())
370 request
->recordLookup(details
);
374 PeerSelector::noteIp(const Ip::Address
&ip
)
376 if (selectionAborted())
379 if (!wantsMoreDestinations())
382 const auto peer
= servers
->_peer
.valid();
384 // for TPROXY spoofing, we must skip unusable addresses
385 if (request
->flags
.spoofClientIp
&& !(peer
&& peer
->options
.no_tproxy
) ) {
386 if (ip
.isIPv4() != request
->client_addr
.isIPv4())
387 return; // cannot spoof the client address on this link
390 Comm::ConnectionPointer p
= new Comm::Connection();
392 p
->remote
.port(peer
? peer
->http_port
: request
->url
.port());
393 handlePath(p
, *servers
);
397 PeerSelector::noteIps(const Dns::CachedIps
*ia
, const Dns::LookupDetails
&details
)
399 if (selectionAborted())
402 FwdServer
*fs
= servers
;
404 debugs(44, 3, "Unknown host: " << (fs
->_peer
.valid() ? fs
->_peer
->host
: request
->url
.host()));
405 // discard any previous error.
408 if (fs
->code
== HIER_DIRECT
) {
409 lastError
= new ErrorState(ERR_DNS_FAIL
, Http::scServiceUnavailable
, request
, al
);
410 lastError
->dnsError
= details
.error
;
413 // else noteIp() calls have already processed all IPs in *ia
418 // continue resolving selected peers
423 PeerSelector::checkNetdbDirect()
430 if (direct
== DIRECT_NO
)
433 /* base lookup on RTT and Hops if ICMP NetDB is enabled. */
435 myrtt
= netdbHostRtt(request
->url
.host());
436 debugs(44, 3, "MY RTT = " << myrtt
<< " msec");
437 debugs(44, 3, "minimum_direct_rtt = " << Config
.minDirectRtt
<< " msec");
439 if (myrtt
&& myrtt
<= Config
.minDirectRtt
)
442 myhops
= netdbHostHops(request
->url
.host());
444 debugs(44, 3, "MY hops = " << myhops
);
445 debugs(44, 3, "minimum_direct_hops = " << Config
.minDirectHops
);
447 if (myhops
&& myhops
<= Config
.minDirectHops
)
450 p
= whichPeer(closest_parent_miss
);
455 debugs(44, 3, "closest_parent_miss RTT = " << ping
.p_rtt
<< " msec");
457 if (myrtt
&& myrtt
<= ping
.p_rtt
)
460 #endif /* USE_ICMP */
466 PeerSelector::selectMore()
468 if (selectionAborted())
471 debugs(44, 3, request
->method
<< ' ' << request
->url
.host());
473 /** If we don't know whether DIRECT is permitted ... */
474 if (direct
== DIRECT_UNKNOWN
) {
475 if (always_direct
== ACCESS_DUNNO
) {
476 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (always_direct to be checked)");
477 /** check always_direct; */
478 ACLFilledChecklist
*ch
= new ACLFilledChecklist(Config
.accessList
.AlwaysDirect
, request
, NULL
);
481 acl_checklist
->syncAle(request
, nullptr);
482 acl_checklist
->nonBlockingCheck(CheckAlwaysDirectDone
, this);
484 } else if (never_direct
== ACCESS_DUNNO
) {
485 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (never_direct to be checked)");
486 /** check never_direct; */
487 ACLFilledChecklist
*ch
= new ACLFilledChecklist(Config
.accessList
.NeverDirect
, request
, NULL
);
490 acl_checklist
->syncAle(request
, nullptr);
491 acl_checklist
->nonBlockingCheck(CheckNeverDirectDone
, this);
493 } else if (request
->flags
.noDirect
) {
494 /** if we are accelerating, direct is not an option. */
496 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (forced non-direct)");
497 } else if (request
->flags
.loopDetected
) {
498 /** if we are in a forwarding-loop, direct is not an option. */
500 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (forwarding loop detected)");
501 } else if (checkNetdbDirect()) {
503 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (checkNetdbDirect)");
505 direct
= DIRECT_MAYBE
;
506 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (default)");
509 debugs(44, 3, "direct = " << DirectStr
[direct
]);
512 if (!entry
|| entry
->ping_status
== PING_NONE
)
516 } else if (entry
->ping_status
== PING_NONE
) {
517 selectSomeNeighbor();
519 if (entry
->ping_status
== PING_WAITING
)
521 } else if (entry
->ping_status
== PING_WAITING
) {
522 selectSomeNeighborReplies();
523 entry
->ping_status
= PING_DONE
;
539 if (Config
.onoff
.prefer_direct
)
542 if (request
->flags
.hierarchical
|| !Config
.onoff
.nonhierarchical_direct
) {
547 if (!Config
.onoff
.prefer_direct
)
553 // end peer selection; start resolving selected peers
557 bool peerAllowedToUse(const CachePeer
*, PeerSelector
*);
559 /// Selects a pinned connection if it exists, is valid, and is allowed.
561 PeerSelector::selectPinned()
563 // TODO: Avoid all repeated calls. Relying on PING_DONE is not enough.
564 if (!request
->pinnedConnection())
567 if (Comm::IsConnOpen(request
->pinnedConnection()->validatePinnedConnection(request
))) {
568 const auto pear
= request
->pinnedConnection()->pinnedPeer();
569 const bool usePinned
= pear
? peerAllowedToUse(pear
, this) : (direct
!= DIRECT_NO
);
571 addSelection(pear
, PINNED
);
573 entry
->ping_status
= PING_DONE
; // skip ICP
576 // If the pinned connection is prohibited (for this request) or gone, then
577 // the initiator must decide whether it is OK to open a new one instead.
581 * Selects a neighbor (parent or sibling) based on one of the
585 * ICMP Netdb RTT estimates
589 PeerSelector::selectSomeNeighbor()
592 hier_code code
= HIER_NONE
;
593 assert(entry
->ping_status
== PING_NONE
);
595 if (direct
== DIRECT_YES
) {
596 entry
->ping_status
= PING_DONE
;
600 #if USE_CACHE_DIGESTS
601 if ((p
= neighborsDigestSelect(this))) {
602 if (neighborType(p
, request
->url
) == PEER_PARENT
)
603 code
= CD_PARENT_HIT
;
605 code
= CD_SIBLING_HIT
;
608 if ((p
= netdbClosestParent(this))) {
609 code
= CLOSEST_PARENT
;
610 } else if (peerSelectIcpPing(this, direct
, entry
)) {
611 debugs(44, 3, "Doing ICP pings");
612 ping
.start
= current_time
;
613 ping
.n_sent
= neighborsUdpPing(request
,
617 &ping
.n_replies_expected
,
620 if (ping
.n_sent
== 0)
621 debugs(44, DBG_CRITICAL
, "WARNING: neighborsUdpPing returned 0");
622 debugs(44, 3, ping
.n_replies_expected
<<
623 " ICP replies expected, RTT " << ping
.timeout
<<
626 if (ping
.n_replies_expected
> 0) {
627 entry
->ping_status
= PING_WAITING
;
628 eventAdd("PeerSelector::HandlePingTimeout",
631 0.001 * ping
.timeout
,
637 if (code
!= HIER_NONE
) {
639 addSelection(p
, code
);
642 entry
->ping_status
= PING_DONE
;
645 /// Selects a neighbor (parent or sibling) based on ICP/HTCP replies.
647 PeerSelector::selectSomeNeighborReplies()
650 hier_code code
= HIER_NONE
;
651 assert(entry
->ping_status
== PING_WAITING
);
652 assert(direct
!= DIRECT_YES
);
654 if (checkNetdbDirect()) {
655 code
= CLOSEST_DIRECT
;
656 addSelection(nullptr, code
);
661 code
= hit_type
== PEER_PARENT
? PARENT_HIT
: SIBLING_HIT
;
663 if (!closest_parent_miss
.isAnyAddr()) {
664 p
= whichPeer(closest_parent_miss
);
665 code
= CLOSEST_PARENT_MISS
;
666 } else if (!first_parent_miss
.isAnyAddr()) {
667 p
= whichPeer(first_parent_miss
);
668 code
= FIRST_PARENT_MISS
;
671 if (p
&& code
!= HIER_NONE
) {
672 addSelection(p
, code
);
676 /// Adds a "direct" entry if the request can be forwarded to the origin server.
678 PeerSelector::selectSomeDirect()
680 if (direct
== DIRECT_NO
)
683 /* WAIS is not implemented natively */
684 if (request
->url
.getScheme() == AnyP::PROTO_WAIS
)
687 addSelection(nullptr, HIER_DIRECT
);
691 PeerSelector::selectSomeParent()
694 hier_code code
= HIER_NONE
;
695 debugs(44, 3, request
->method
<< ' ' << request
->url
.host());
697 if (direct
== DIRECT_YES
)
700 if ((p
= peerSourceHashSelectParent(this))) {
701 code
= SOURCEHASH_PARENT
;
703 } else if ((p
= peerUserHashSelectParent(this))) {
704 code
= USERHASH_PARENT
;
706 } else if ((p
= carpSelectParent(this))) {
708 } else if ((p
= getRoundRobinParent(this))) {
709 code
= ROUNDROBIN_PARENT
;
710 } else if ((p
= getWeightedRoundRobinParent(this))) {
711 code
= ROUNDROBIN_PARENT
;
712 } else if ((p
= getFirstUpParent(this))) {
713 code
= FIRSTUP_PARENT
;
714 } else if ((p
= getDefaultParent(this))) {
715 code
= DEFAULT_PARENT
;
718 if (code
!= HIER_NONE
) {
719 addSelection(p
, code
);
723 /// Adds alive parents. Used as a last resort for never_direct.
725 PeerSelector::selectAllParents()
728 /* Add all alive parents */
730 for (p
= Config
.peers
; p
; p
= p
->next
) {
731 /* XXX: neighbors.c lacks a public interface for enumerating
732 * parents to a request so we have to dig some here..
735 if (neighborType(p
, request
->url
) != PEER_PARENT
)
738 if (!peerHTTPOkay(p
, this))
741 addSelection(p
, ANY_OLD_PARENT
);
744 /* XXX: should add dead parents here, but it is currently
745 * not possible to find out which parents are dead or which
746 * simply are not configured to handle the request.
748 /* Add default parent as a last resort */
749 if ((p
= getDefaultParent(this))) {
750 addSelection(p
, DEFAULT_PARENT
);
755 PeerSelector::handlePingTimeout()
757 debugs(44, 3, url());
760 entry
->ping_status
= PING_DONE
;
762 if (selectionAborted())
765 ++PeerStats
.timeouts
;
771 PeerSelector::HandlePingTimeout(void *data
)
773 static_cast<PeerSelector
*>(data
)->handlePingTimeout();
779 memset(&PeerStats
, '\0', sizeof(PeerStats
));
783 PeerSelector::handleIcpParentMiss(CachePeer
*p
, icp_common_t
*header
)
788 if (Config
.onoff
.query_icmp
) {
789 if (header
->flags
& ICP_FLAG_SRC_RTT
) {
790 rtt
= header
->pad
& 0xFFFF;
791 int hops
= (header
->pad
>> 16) & 0xFFFF;
793 if (rtt
> 0 && rtt
< 0xFFFF)
794 netdbUpdatePeer(request
->url
, p
, rtt
, hops
);
796 if (rtt
&& (ping
.p_rtt
== 0 || rtt
< ping
.p_rtt
)) {
797 closest_parent_miss
= p
->in_addr
;
802 #endif /* USE_ICMP */
804 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
805 if (p
->options
.closest_only
)
808 /* set FIRST_MISS if there is no CLOSEST parent */
809 if (!closest_parent_miss
.isAnyAddr())
812 rtt
= (tvSubMsec(ping
.start
, current_time
) - p
->basetime
) / p
->weight
;
817 if (first_parent_miss
.isAnyAddr() || rtt
< ping
.w_rtt
) {
818 first_parent_miss
= p
->in_addr
;
824 PeerSelector::handleIcpReply(CachePeer
*p
, const peer_t type
, icp_common_t
*header
)
826 icp_opcode op
= header
->getOpCode();
827 debugs(44, 3, icp_opcode_str
[op
] << ' ' << url());
828 #if USE_CACHE_DIGESTS && 0
829 /* do cd lookup to count false misses */
832 peerNoteDigestLookup(request
, p
,
833 peerDigestLookup(p
, this));
839 if (op
== ICP_MISS
|| op
== ICP_DECHO
) {
840 if (type
== PEER_PARENT
)
841 handleIcpParentMiss(p
, header
);
842 } else if (op
== ICP_HIT
) {
849 if (ping
.n_recv
< ping
.n_replies_expected
)
857 PeerSelector::handleHtcpReply(CachePeer
*p
, const peer_t type
, HtcpReplyData
*htcp
)
859 debugs(44, 3, (htcp
->hit
? "HIT" : "MISS") << ' ' << url());
869 if (type
== PEER_PARENT
)
870 handleHtcpParentMiss(p
, htcp
);
872 if (ping
.n_recv
< ping
.n_replies_expected
)
879 PeerSelector::handleHtcpParentMiss(CachePeer
*p
, HtcpReplyData
*htcp
)
884 if (Config
.onoff
.query_icmp
) {
885 if (htcp
->cto
.rtt
> 0) {
886 rtt
= (int) htcp
->cto
.rtt
* 1000;
887 int hops
= (int) htcp
->cto
.hops
* 1000;
888 netdbUpdatePeer(request
->url
, p
, rtt
, hops
);
890 if (rtt
&& (ping
.p_rtt
== 0 || rtt
< ping
.p_rtt
)) {
891 closest_parent_miss
= p
->in_addr
;
896 #endif /* USE_ICMP */
898 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
899 if (p
->options
.closest_only
)
902 /* set FIRST_MISS if there is no CLOSEST parent */
903 if (!closest_parent_miss
.isAnyAddr())
906 rtt
= (tvSubMsec(ping
.start
, current_time
) - p
->basetime
) / p
->weight
;
911 if (first_parent_miss
.isAnyAddr() || rtt
< ping
.w_rtt
) {
912 first_parent_miss
= p
->in_addr
;
920 PeerSelector::HandlePingReply(CachePeer
* p
, peer_t type
, AnyP::ProtocolType proto
, void *pingdata
, void *data
)
922 if (proto
== AnyP::PROTO_ICP
)
923 static_cast<PeerSelector
*>(data
)->handleIcpReply(p
, type
, static_cast<icp_common_t
*>(pingdata
));
927 else if (proto
== AnyP::PROTO_HTCP
)
928 static_cast<PeerSelector
*>(data
)->handleHtcpReply(p
, type
, static_cast<HtcpReplyData
*>(pingdata
));
933 debugs(44, DBG_IMPORTANT
, "ERROR: ignoring an ICP reply with unknown protocol " << proto
);
937 PeerSelector::addSelection(CachePeer
*peer
, const hier_code code
)
939 // Find the end of the servers list. Bail on a duplicate destination.
940 auto **serversTail
= &servers
;
941 while (const auto server
= *serversTail
) {
942 // There can be at most one PINNED destination.
943 // Non-PINNED destinations are uniquely identified by their CachePeer
944 // (even though a DIRECT destination might match a cache_peer address).
945 const bool duplicate
= (server
->code
== PINNED
) ?
946 (code
== PINNED
) : (server
->_peer
== peer
);
948 debugs(44, 3, "skipping " << PeerSelectionDumper(this, peer
, code
) <<
949 "; have " << PeerSelectionDumper(this, server
->_peer
.get(), server
->code
));
952 serversTail
= &server
->next
;
955 debugs(44, 3, "adding " << PeerSelectionDumper(this, peer
, code
));
956 *serversTail
= new FwdServer(peer
, code
);
959 PeerSelector::PeerSelector(PeerSelectionInitiator
*initiator
):
962 always_direct(Config
.accessList
.AlwaysDirect
?ACCESS_DUNNO
:ACCESS_DENIED
),
963 never_direct(Config
.accessList
.NeverDirect
?ACCESS_DUNNO
:ACCESS_DENIED
),
964 direct(DIRECT_UNKNOWN
),
968 closest_parent_miss(),
971 acl_checklist (NULL
),
972 initiator_(initiator
)
974 ; // no local defaults.
978 PeerSelector::url() const
981 return SBuf(entry
->url());
984 return request
->effectiveRequestUri();
986 static const SBuf
noUrl("[no URL]");
990 /// \returns valid/interested peer initiator or nil
991 PeerSelectionInitiator
*
992 PeerSelector::interestedInitiator()
994 const auto initiator
= initiator_
.valid();
997 debugs(44, 3, id
<< " initiator gone");
1001 if (!initiator
->subscribed
) {
1002 debugs(44, 3, id
<< " initiator lost interest");
1011 PeerSelector::wantsMoreDestinations() const {
1012 const auto maxCount
= Config
.forward_max_tries
;
1013 return maxCount
>= 0 && foundPaths
<
1014 static_cast<std::make_unsigned
<decltype(maxCount
)>::type
>(maxCount
);
1018 PeerSelector::handlePath(const Comm::ConnectionPointer
&path
, FwdServer
&fs
)
1023 path
->peerType
= fs
.code
;
1024 path
->setPeer(fs
._peer
.get());
1026 // check for a configured outgoing address for this destination...
1027 getOutgoingAddress(request
, path
);
1028 debugs(44, 2, id
<< " found " << path
<< ", destination #" << foundPaths
<< " for " << url());
1030 debugs(44, 2, id
<< " found pinned, destination #" << foundPaths
<< " for " << url());
1032 request
->hier
.ping
= ping
; // may be updated later
1034 debugs(44, 2, " always_direct = " << always_direct
);
1035 debugs(44, 2, " never_direct = " << never_direct
);
1036 debugs(44, 2, " timedout = " << ping
.timedout
);
1038 if (const auto initiator
= interestedInitiator())
1039 initiator
->noteDestination(path
);
1042 InstanceIdDefinitions(PeerSelector
, "PeerSelector");
1044 ping_data::ping_data() :
1047 n_replies_expected(0),