2 * Copyright (C) 1996-2018 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 // convert the list of FwdServer destinations into destinations IP addresses
305 if (fs
&& wantsMoreDestinations()) {
306 // send the next one off for DNS lookup.
307 const char *host
= fs
->_peer
.valid() ? fs
->_peer
->host
: request
->url
.host();
308 debugs(44, 2, "Find IP destination for: " << url() << "' via " << host
);
309 Dns::nbgethostbyname(host
, this);
313 // Bug 3605: clear any extra listed FwdServer destinations, when the options exceeds max_foward_tries.
314 // due to the allocation method of fs, we must deallocate each manually.
315 // TODO: use a std::list so we can get the size and abort adding whenever the selection loops reach Config.forward_max_tries
317 assert(fs
== servers
);
325 // done with DNS lookups. pass back to caller
327 debugs(44, 2, id
<< " found all " << foundPaths
<< " destinations for " << url());
328 debugs(44, 2, " always_direct = " << always_direct
);
329 debugs(44, 2, " never_direct = " << never_direct
);
330 debugs(44, 2, " timedout = " << ping
.timedout
);
332 ping
.stop
= current_time
;
333 request
->hier
.ping
= ping
; // final result
335 if (lastError
&& foundPaths
) {
336 // nobody cares about errors if we found destinations despite them
337 debugs(44, 3, "forgetting the last error");
342 if (const auto initiator
= interestedInitiator())
343 initiator
->noteDestinationsEnd(lastError
);
344 lastError
= nullptr; // initiator owns the ErrorState object now
349 PeerSelector::noteLookup(const Dns::LookupDetails
&details
)
351 /* ignore lookup delays that occurred after the initiator moved on */
353 if (selectionAborted())
356 if (!wantsMoreDestinations())
359 request
->recordLookup(details
);
363 PeerSelector::noteIp(const Ip::Address
&ip
)
365 if (selectionAborted())
368 if (!wantsMoreDestinations())
371 const auto peer
= servers
->_peer
.valid();
373 // for TPROXY spoofing, we must skip unusable addresses
374 if (request
->flags
.spoofClientIp
&& !(peer
&& peer
->options
.no_tproxy
) ) {
375 if (ip
.isIPv4() != request
->client_addr
.isIPv4())
376 return; // cannot spoof the client address on this link
379 Comm::ConnectionPointer p
= new Comm::Connection();
381 p
->remote
.port(peer
? peer
->http_port
: request
->url
.port());
382 handlePath(p
, *servers
);
386 PeerSelector::noteIps(const Dns::CachedIps
*ia
, const Dns::LookupDetails
&details
)
388 if (selectionAborted())
391 FwdServer
*fs
= servers
;
393 debugs(44, 3, "Unknown host: " << (fs
->_peer
.valid() ? fs
->_peer
->host
: request
->url
.host()));
394 // discard any previous error.
397 if (fs
->code
== HIER_DIRECT
) {
398 lastError
= new ErrorState(ERR_DNS_FAIL
, Http::scServiceUnavailable
, request
);
399 lastError
->dnsError
= details
.error
;
402 // else noteIp() calls have already processed all IPs in *ia
407 // continue resolving selected peers
412 PeerSelector::checkNetdbDirect()
419 if (direct
== DIRECT_NO
)
422 /* base lookup on RTT and Hops if ICMP NetDB is enabled. */
424 myrtt
= netdbHostRtt(request
->url
.host());
425 debugs(44, 3, "MY RTT = " << myrtt
<< " msec");
426 debugs(44, 3, "minimum_direct_rtt = " << Config
.minDirectRtt
<< " msec");
428 if (myrtt
&& myrtt
<= Config
.minDirectRtt
)
431 myhops
= netdbHostHops(request
->url
.host());
433 debugs(44, 3, "MY hops = " << myhops
);
434 debugs(44, 3, "minimum_direct_hops = " << Config
.minDirectHops
);
436 if (myhops
&& myhops
<= Config
.minDirectHops
)
439 p
= whichPeer(closest_parent_miss
);
444 debugs(44, 3, "closest_parent_miss RTT = " << ping
.p_rtt
<< " msec");
446 if (myrtt
&& myrtt
<= ping
.p_rtt
)
449 #endif /* USE_ICMP */
455 PeerSelector::selectMore()
457 if (selectionAborted())
460 debugs(44, 3, request
->method
<< ' ' << request
->url
.host());
462 /** If we don't know whether DIRECT is permitted ... */
463 if (direct
== DIRECT_UNKNOWN
) {
464 if (always_direct
== ACCESS_DUNNO
) {
465 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (always_direct to be checked)");
466 /** check always_direct; */
467 ACLFilledChecklist
*ch
= new ACLFilledChecklist(Config
.accessList
.AlwaysDirect
, request
, NULL
);
470 acl_checklist
->syncAle(request
, nullptr);
471 acl_checklist
->nonBlockingCheck(CheckAlwaysDirectDone
, this);
473 } else if (never_direct
== ACCESS_DUNNO
) {
474 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (never_direct to be checked)");
475 /** check never_direct; */
476 ACLFilledChecklist
*ch
= new ACLFilledChecklist(Config
.accessList
.NeverDirect
, request
, NULL
);
479 acl_checklist
->syncAle(request
, nullptr);
480 acl_checklist
->nonBlockingCheck(CheckNeverDirectDone
, this);
482 } else if (request
->flags
.noDirect
) {
483 /** if we are accelerating, direct is not an option. */
485 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (forced non-direct)");
486 } else if (request
->flags
.loopDetected
) {
487 /** if we are in a forwarding-loop, direct is not an option. */
489 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (forwarding loop detected)");
490 } else if (checkNetdbDirect()) {
492 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (checkNetdbDirect)");
494 direct
= DIRECT_MAYBE
;
495 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (default)");
498 debugs(44, 3, "direct = " << DirectStr
[direct
]);
501 if (!entry
|| entry
->ping_status
== PING_NONE
)
505 } else if (entry
->ping_status
== PING_NONE
) {
506 selectSomeNeighbor();
508 if (entry
->ping_status
== PING_WAITING
)
510 } else if (entry
->ping_status
== PING_WAITING
) {
511 selectSomeNeighborReplies();
512 entry
->ping_status
= PING_DONE
;
528 if (Config
.onoff
.prefer_direct
)
531 if (request
->flags
.hierarchical
|| !Config
.onoff
.nonhierarchical_direct
) {
536 if (!Config
.onoff
.prefer_direct
)
542 // end peer selection; start resolving selected peers
546 bool peerAllowedToUse(const CachePeer
*, PeerSelector
*);
548 /// Selects a pinned connection if it exists, is valid, and is allowed.
550 PeerSelector::selectPinned()
552 // TODO: Avoid all repeated calls. Relying on PING_DONE is not enough.
553 if (!request
->pinnedConnection())
555 CachePeer
*pear
= request
->pinnedConnection()->pinnedPeer();
556 if (Comm::IsConnOpen(request
->pinnedConnection()->validatePinnedConnection(request
, pear
))) {
557 const bool usePinned
= pear
? peerAllowedToUse(pear
, this) : (direct
!= DIRECT_NO
);
559 addSelection(pear
, PINNED
);
561 entry
->ping_status
= PING_DONE
; // skip ICP
564 // If the pinned connection is prohibited (for this request) or gone, then
565 // the initiator must decide whether it is OK to open a new one instead.
569 * Selects a neighbor (parent or sibling) based on one of the
573 * ICMP Netdb RTT estimates
577 PeerSelector::selectSomeNeighbor()
580 hier_code code
= HIER_NONE
;
581 assert(entry
->ping_status
== PING_NONE
);
583 if (direct
== DIRECT_YES
) {
584 entry
->ping_status
= PING_DONE
;
588 #if USE_CACHE_DIGESTS
589 if ((p
= neighborsDigestSelect(this))) {
590 if (neighborType(p
, request
->url
) == PEER_PARENT
)
591 code
= CD_PARENT_HIT
;
593 code
= CD_SIBLING_HIT
;
596 if ((p
= netdbClosestParent(this))) {
597 code
= CLOSEST_PARENT
;
598 } else if (peerSelectIcpPing(this, direct
, entry
)) {
599 debugs(44, 3, "Doing ICP pings");
600 ping
.start
= current_time
;
601 ping
.n_sent
= neighborsUdpPing(request
,
605 &ping
.n_replies_expected
,
608 if (ping
.n_sent
== 0)
609 debugs(44, DBG_CRITICAL
, "WARNING: neighborsUdpPing returned 0");
610 debugs(44, 3, ping
.n_replies_expected
<<
611 " ICP replies expected, RTT " << ping
.timeout
<<
614 if (ping
.n_replies_expected
> 0) {
615 entry
->ping_status
= PING_WAITING
;
616 eventAdd("PeerSelector::HandlePingTimeout",
619 0.001 * ping
.timeout
,
625 if (code
!= HIER_NONE
) {
627 addSelection(p
, code
);
630 entry
->ping_status
= PING_DONE
;
633 /// Selects a neighbor (parent or sibling) based on ICP/HTCP replies.
635 PeerSelector::selectSomeNeighborReplies()
638 hier_code code
= HIER_NONE
;
639 assert(entry
->ping_status
== PING_WAITING
);
640 assert(direct
!= DIRECT_YES
);
642 if (checkNetdbDirect()) {
643 code
= CLOSEST_DIRECT
;
644 addSelection(nullptr, code
);
649 code
= hit_type
== PEER_PARENT
? PARENT_HIT
: SIBLING_HIT
;
651 if (!closest_parent_miss
.isAnyAddr()) {
652 p
= whichPeer(closest_parent_miss
);
653 code
= CLOSEST_PARENT_MISS
;
654 } else if (!first_parent_miss
.isAnyAddr()) {
655 p
= whichPeer(first_parent_miss
);
656 code
= FIRST_PARENT_MISS
;
659 if (p
&& code
!= HIER_NONE
) {
660 addSelection(p
, code
);
664 /// Adds a "direct" entry if the request can be forwarded to the origin server.
666 PeerSelector::selectSomeDirect()
668 if (direct
== DIRECT_NO
)
671 /* WAIS is not implemented natively */
672 if (request
->url
.getScheme() == AnyP::PROTO_WAIS
)
675 addSelection(nullptr, HIER_DIRECT
);
679 PeerSelector::selectSomeParent()
682 hier_code code
= HIER_NONE
;
683 debugs(44, 3, request
->method
<< ' ' << request
->url
.host());
685 if (direct
== DIRECT_YES
)
688 if ((p
= peerSourceHashSelectParent(this))) {
689 code
= SOURCEHASH_PARENT
;
691 } else if ((p
= peerUserHashSelectParent(this))) {
692 code
= USERHASH_PARENT
;
694 } else if ((p
= carpSelectParent(this))) {
696 } else if ((p
= getRoundRobinParent(this))) {
697 code
= ROUNDROBIN_PARENT
;
698 } else if ((p
= getWeightedRoundRobinParent(this))) {
699 code
= ROUNDROBIN_PARENT
;
700 } else if ((p
= getFirstUpParent(this))) {
701 code
= FIRSTUP_PARENT
;
702 } else if ((p
= getDefaultParent(this))) {
703 code
= DEFAULT_PARENT
;
706 if (code
!= HIER_NONE
) {
707 addSelection(p
, code
);
711 /// Adds alive parents. Used as a last resort for never_direct.
713 PeerSelector::selectAllParents()
716 /* Add all alive parents */
718 for (p
= Config
.peers
; p
; p
= p
->next
) {
719 /* XXX: neighbors.c lacks a public interface for enumerating
720 * parents to a request so we have to dig some here..
723 if (neighborType(p
, request
->url
) != PEER_PARENT
)
726 if (!peerHTTPOkay(p
, this))
729 addSelection(p
, ANY_OLD_PARENT
);
732 /* XXX: should add dead parents here, but it is currently
733 * not possible to find out which parents are dead or which
734 * simply are not configured to handle the request.
736 /* Add default parent as a last resort */
737 if ((p
= getDefaultParent(this))) {
738 addSelection(p
, DEFAULT_PARENT
);
743 PeerSelector::handlePingTimeout()
745 debugs(44, 3, url());
748 entry
->ping_status
= PING_DONE
;
750 if (selectionAborted())
753 ++PeerStats
.timeouts
;
759 PeerSelector::HandlePingTimeout(void *data
)
761 static_cast<PeerSelector
*>(data
)->handlePingTimeout();
767 memset(&PeerStats
, '\0', sizeof(PeerStats
));
771 PeerSelector::handleIcpParentMiss(CachePeer
*p
, icp_common_t
*header
)
776 if (Config
.onoff
.query_icmp
) {
777 if (header
->flags
& ICP_FLAG_SRC_RTT
) {
778 rtt
= header
->pad
& 0xFFFF;
779 int hops
= (header
->pad
>> 16) & 0xFFFF;
781 if (rtt
> 0 && rtt
< 0xFFFF)
782 netdbUpdatePeer(request
->url
, p
, rtt
, hops
);
784 if (rtt
&& (ping
.p_rtt
== 0 || rtt
< ping
.p_rtt
)) {
785 closest_parent_miss
= p
->in_addr
;
790 #endif /* USE_ICMP */
792 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
793 if (p
->options
.closest_only
)
796 /* set FIRST_MISS if there is no CLOSEST parent */
797 if (!closest_parent_miss
.isAnyAddr())
800 rtt
= (tvSubMsec(ping
.start
, current_time
) - p
->basetime
) / p
->weight
;
805 if (first_parent_miss
.isAnyAddr() || rtt
< ping
.w_rtt
) {
806 first_parent_miss
= p
->in_addr
;
812 PeerSelector::handleIcpReply(CachePeer
*p
, const peer_t type
, icp_common_t
*header
)
814 icp_opcode op
= header
->getOpCode();
815 debugs(44, 3, icp_opcode_str
[op
] << ' ' << url());
816 #if USE_CACHE_DIGESTS && 0
817 /* do cd lookup to count false misses */
820 peerNoteDigestLookup(request
, p
,
821 peerDigestLookup(p
, this));
827 if (op
== ICP_MISS
|| op
== ICP_DECHO
) {
828 if (type
== PEER_PARENT
)
829 handleIcpParentMiss(p
, header
);
830 } else if (op
== ICP_HIT
) {
837 if (ping
.n_recv
< ping
.n_replies_expected
)
845 PeerSelector::handleHtcpReply(CachePeer
*p
, const peer_t type
, HtcpReplyData
*htcp
)
847 debugs(44, 3, (htcp
->hit
? "HIT" : "MISS") << ' ' << url());
857 if (type
== PEER_PARENT
)
858 handleHtcpParentMiss(p
, htcp
);
860 if (ping
.n_recv
< ping
.n_replies_expected
)
867 PeerSelector::handleHtcpParentMiss(CachePeer
*p
, HtcpReplyData
*htcp
)
872 if (Config
.onoff
.query_icmp
) {
873 if (htcp
->cto
.rtt
> 0) {
874 rtt
= (int) htcp
->cto
.rtt
* 1000;
875 int hops
= (int) htcp
->cto
.hops
* 1000;
876 netdbUpdatePeer(request
->url
, p
, rtt
, hops
);
878 if (rtt
&& (ping
.p_rtt
== 0 || rtt
< ping
.p_rtt
)) {
879 closest_parent_miss
= p
->in_addr
;
884 #endif /* USE_ICMP */
886 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
887 if (p
->options
.closest_only
)
890 /* set FIRST_MISS if there is no CLOSEST parent */
891 if (!closest_parent_miss
.isAnyAddr())
894 rtt
= (tvSubMsec(ping
.start
, current_time
) - p
->basetime
) / p
->weight
;
899 if (first_parent_miss
.isAnyAddr() || rtt
< ping
.w_rtt
) {
900 first_parent_miss
= p
->in_addr
;
908 PeerSelector::HandlePingReply(CachePeer
* p
, peer_t type
, AnyP::ProtocolType proto
, void *pingdata
, void *data
)
910 if (proto
== AnyP::PROTO_ICP
)
911 static_cast<PeerSelector
*>(data
)->handleIcpReply(p
, type
, static_cast<icp_common_t
*>(pingdata
));
915 else if (proto
== AnyP::PROTO_HTCP
)
916 static_cast<PeerSelector
*>(data
)->handleHtcpReply(p
, type
, static_cast<HtcpReplyData
*>(pingdata
));
921 debugs(44, DBG_IMPORTANT
, "ERROR: ignoring an ICP reply with unknown protocol " << proto
);
925 PeerSelector::addSelection(CachePeer
*peer
, const hier_code code
)
927 // Find the end of the servers list. Bail on a duplicate destination.
928 auto **serversTail
= &servers
;
929 while (const auto server
= *serversTail
) {
930 // There can be at most one PINNED destination.
931 // Non-PINNED destinations are uniquely identified by their CachePeer
932 // (even though a DIRECT destination might match a cache_peer address).
933 const bool duplicate
= (server
->code
== PINNED
) ?
934 (code
== PINNED
) : (server
->_peer
== peer
);
936 debugs(44, 3, "skipping " << PeerSelectionDumper(this, peer
, code
) <<
937 "; have " << PeerSelectionDumper(this, server
->_peer
.get(), server
->code
));
940 serversTail
= &server
->next
;
943 debugs(44, 3, "adding " << PeerSelectionDumper(this, peer
, code
));
944 *serversTail
= new FwdServer(peer
, code
);
947 PeerSelector::PeerSelector(PeerSelectionInitiator
*initiator
):
950 always_direct(Config
.accessList
.AlwaysDirect
?ACCESS_DUNNO
:ACCESS_DENIED
),
951 never_direct(Config
.accessList
.NeverDirect
?ACCESS_DUNNO
:ACCESS_DENIED
),
952 direct(DIRECT_UNKNOWN
),
956 closest_parent_miss(),
959 acl_checklist (NULL
),
960 initiator_(initiator
)
962 ; // no local defaults.
966 PeerSelector::url() const
969 return SBuf(entry
->url());
972 return request
->effectiveRequestUri();
974 static const SBuf
noUrl("[no URL]");
978 /// \returns valid/interested peer initiator or nil
979 PeerSelectionInitiator
*
980 PeerSelector::interestedInitiator()
982 const auto initiator
= initiator_
.valid();
985 debugs(44, 3, id
<< " initiator gone");
989 if (!initiator
->subscribed
) {
990 debugs(44, 3, id
<< " initiator lost interest");
999 PeerSelector::wantsMoreDestinations() const {
1000 const auto maxCount
= Config
.forward_max_tries
;
1001 return maxCount
>= 0 && foundPaths
<
1002 static_cast<std::make_unsigned
<decltype(maxCount
)>::type
>(maxCount
);
1006 PeerSelector::handlePath(Comm::ConnectionPointer
&path
, FwdServer
&fs
)
1010 path
->peerType
= fs
.code
;
1011 path
->setPeer(fs
._peer
.get());
1013 // check for a configured outgoing address for this destination...
1014 getOutgoingAddress(request
, path
);
1016 request
->hier
.ping
= ping
; // may be updated later
1018 debugs(44, 2, id
<< " found " << path
<< ", destination #" << foundPaths
<< " for " << url());
1019 debugs(44, 2, " always_direct = " << always_direct
);
1020 debugs(44, 2, " never_direct = " << never_direct
);
1021 debugs(44, 2, " timedout = " << ping
.timedout
);
1023 if (const auto initiator
= interestedInitiator())
1024 initiator
->noteDestination(path
);
1027 InstanceIdDefinitions(PeerSelector
, "PeerSelector");
1029 ping_data::ping_data() :
1032 n_replies_expected(0),