ResolvedPeers.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 #ifndef SQUID_SRC_RESOLVEDPEERS_H
10 #define SQUID_SRC_RESOLVEDPEERS_H
11 
12 #include "base/RefCount.h"
13 #include "comm/Connection.h"
14 #include "mem/AllocatorProxy.h"
15 
16 #include <iosfwd>
17 #include <limits>
18 #include <utility>
19 
21 {
22 public:
23  explicit ResolvedPeerPath(const Comm::ConnectionPointer &conn) : connection(conn), available(true) {}
24 
26  bool available;
27 };
28 
30 
34 {
36 
37 public:
38  // ResolvedPeerPaths in addPath() call order
39  typedef std::vector<ResolvedPeerPath> Paths;
40  using size_type = Paths::size_type;
42 
43  ResolvedPeers();
44 
46  bool empty() const { return !availablePaths; }
47 
49  void addPath(const Comm::ConnectionPointer &);
50 
54 
57 
61 
65 
67  bool haveSpare(const Comm::Connection &currentPeer);
68 
70  bool doneWithPrimes(const Comm::Connection &currentPeer);
71 
73  bool doneWithSpares(const Comm::Connection &currentPeer);
74 
76  bool doneWithPeer(const Comm::Connection &currentPeer);
77 
79  size_type size() const { return availablePaths; }
80 
82  bool destinationsFinalized = false;
83 
85  bool notificationPending = false;
86 
87 private:
90  typedef std::pair<Paths::iterator, bool> Finding;
91 
93  static int ConnectionFamily(const Comm::Connection &conn);
94 
95  Paths::iterator start();
96  Finding findSpare(const Comm::Connection &currentPeer);
97  Finding findPrime(const Comm::Connection &currentPeer);
98  Finding findPeer(const Comm::Connection &currentPeer);
99  PeerConnectionPointer extractFound(const char *description, const Paths::iterator &found);
100  Finding makeFinding(const Paths::iterator &found, bool foundOther);
101 
102  bool doneWith(const Finding &findings) const;
103 
104  void increaseAvailability();
105  void decreaseAvailability();
106 
108 
113 
116 };
117 
122 {
123 public:
125 
126  PeerConnectionPointer() = default;
127  PeerConnectionPointer(std::nullptr_t): PeerConnectionPointer() {}
129 
130  /* read-only pointer API; for Connection assignment, see finalize() */
131  explicit operator bool() const { return static_cast<bool>(connection_); }
134 
136  operator const Comm::ConnectionPointer&() const { return connection_; }
137 
139  void finalize(const Comm::ConnectionPointer &conn) { connection_ = conn; }
140 
142  void print(std::ostream &) const;
143 
144 private:
146  static constexpr auto npos = std::numeric_limits<size_type>::max();
147 
150 
153  friend class ResolvedPeers;
154 };
155 
157 std::ostream &operator <<(std::ostream &, const ResolvedPeers &);
158 
159 inline std::ostream &
160 operator <<(std::ostream &os, const PeerConnectionPointer &dest)
161 {
162  dest.print(os);
163  return os;
164 }
165 
166 #endif /* SQUID_SRC_RESOLVEDPEERS_H */
167 
PeerConnectionPointer extractFront()
extracts and returns the first queued address
std::vector< ResolvedPeerPath > Paths
Definition: ResolvedPeers.h:35
Finding findPrime(const Comm::Connection &currentPeer)
bool doneWithPrimes(const Comm::Connection &currentPeer)
whether extractPrime() returns and will continue to return nil
std::ostream & operator<<(std::ostream &, const ResolvedPeers &)
summarized ResolvedPeers (for debugging)
size_type position_
ResolvedPeers-maintained membership index (or npos)
bool destinationsFinalized
whether all of the available candidate paths received from DNS
Definition: ResolvedPeers.h:82
void print(std::ostream &) const
debugging dump
Comm::ConnectionPointer connection_
half-baked, open, failed, or closed Comm::Connection (or nil)
void addPath(const Comm::ConnectionPointer &)
add a candidate path to try after all the existing paths
size_type availablePaths
the total number of currently available elements in paths_
const A & max(A const &lhs, A const &rhs)
C * getRaw() const
Definition: RefCount.h:89
static int ConnectionFamily(const Comm::Connection &conn)
The protocol family of the given path, AF_INET or AF_INET6.
Finding findSpare(const Comm::Connection &currentPeer)
PeerConnectionPointer(const Comm::ConnectionPointer &conn, const size_type pos)
Finding findPeer(const Comm::Connection &currentPeer)
Paths::size_type size_type
Definition: ResolvedPeers.h:40
bool doneWithSpares(const Comm::Connection &currentPeer)
whether extractSpare() returns and will continue to return nil
bool empty() const
whether we lack any known candidate paths
Definition: ResolvedPeers.h:46
static constexpr auto npos
non-existent position for nil connection
std::pair< Paths::iterator, bool > Finding
Definition: ResolvedPeers.h:90
bool haveSpare(const Comm::Connection &currentPeer)
whether extractSpare() would return a non-nil path right now
PeerConnectionPointer extractPrime(const Comm::Connection &currentPeer)
void decreaseAvailability()
decrements the number of available paths
PeerConnectionPointer extractFound(const char *description, const Paths::iterator &found)
convenience method to finish a successful extract*() call
Paths::iterator start()
Paths paths_
resolved addresses in (peer, family) order
size_type pathsToSkip
#define assert(EX)
Definition: assert.h:17
Comm::Connection & operator*() const
PeerConnectionPointer extractSpare(const Comm::Connection &currentPeer)
ResolvedPeers::size_type size_type
bool notificationPending
whether HappyConnOpener::noteCandidatesChange() is scheduled to fire
Definition: ResolvedPeers.h:85
size_type size() const
the current number of candidate paths
Definition: ResolvedPeers.h:79
bool doneWithPeer(const Comm::Connection &currentPeer)
whether doneWithPrimes() and doneWithSpares() are true for currentPeer
bool available
whether this path may be used (i.e., has not been tried already)
Definition: ResolvedPeers.h:26
void reinstatePath(const PeerConnectionPointer &)
#define MEMPROXY_CLASS(CLASS)
Comm::ConnectionPointer connection
(the address of) a path
Definition: ResolvedPeers.h:25
RefCount< ResolvedPeers > Pointer
Definition: ResolvedPeers.h:41
PeerConnectionPointer()=default
ResolvedPeerPath(const Comm::ConnectionPointer &conn)
Definition: ResolvedPeers.h:23
void finalize(const Comm::ConnectionPointer &conn)
upgrade stored peer selection details with a matching actual connection
void increaseAvailability()
increments the number of available paths
Comm::Connection * operator->() const
Finding makeFinding(const Paths::iterator &found, bool foundOther)
finalizes the iterator part of the given preliminary find*() result
bool doneWith(const Finding &findings) const

 

Introduction

Documentation

Support

Miscellaneous