Skip to content

Commit d206e6f

Browse files
merge bug26133 into default
2 parents 269a499 + c4a62a3 commit d206e6f

File tree

5 files changed

+17
-182
lines changed

5 files changed

+17
-182
lines changed

projects/client/RabbitMQ.Client/src/client/api/ConnectionFactory.cs

Lines changed: 13 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -203,118 +203,24 @@ public String Uri
203203
///their respective defaults.</summary>
204204
public ConnectionFactory() { }
205205

206-
protected virtual IConnection FollowRedirectChain
207-
(int maxRedirects,
208-
IDictionary<AmqpTcpEndpoint, int> connectionAttempts,
209-
IDictionary<AmqpTcpEndpoint, Exception> connectionErrors,
210-
ref AmqpTcpEndpoint[] mostRecentKnownHosts,
211-
AmqpTcpEndpoint endpoint)
212-
{
213-
AmqpTcpEndpoint candidate = endpoint;
214-
try {
215-
while (true) {
216-
int attemptCount =
217-
connectionAttempts.ContainsKey(candidate)
218-
? (int) connectionAttempts[candidate]
219-
: 0;
220-
connectionAttempts[candidate] = attemptCount + 1;
221-
bool insist = attemptCount >= maxRedirects;
222-
223-
IProtocol p = Protocol;
224-
IFrameHandler fh = p.CreateFrameHandler(candidate,
225-
SocketFactory,
226-
RequestedConnectionTimeout);
227-
228-
// At this point, we may be able to create
229-
// and fully open a successful connection,
230-
// in which case we're done, and the
231-
// connection should be returned.
232-
return p.CreateConnection(this, insist, fh);
233-
}
234-
} catch (Exception e) {
235-
connectionErrors[candidate] = e;
236-
return null;
237-
}
238-
}
239-
240-
protected virtual IConnection CreateConnection(int maxRedirects,
241-
IDictionary<AmqpTcpEndpoint, int> connectionAttempts,
242-
IDictionary<AmqpTcpEndpoint, Exception> connectionErrors,
243-
params AmqpTcpEndpoint[] endpoints)
206+
///<summary>Create a connection to the specified endpoint.</summary>
207+
public virtual IConnection CreateConnection()
244208
{
245-
foreach (AmqpTcpEndpoint endpoint in endpoints)
209+
IConnection conn = null;
210+
try
211+
{
212+
IProtocol p = Protocols.DefaultProtocol;
213+
IFrameHandler fh = p.CreateFrameHandler(Endpoint,
214+
SocketFactory,
215+
RequestedConnectionTimeout);
216+
conn = p.CreateConnection(this, false, fh);
217+
} catch (Exception e)
246218
{
247-
AmqpTcpEndpoint[] mostRecentKnownHosts = new AmqpTcpEndpoint[0];
248-
// ^^ holds a list of known-hosts that came back with
249-
// a connection.redirect. If, once we reach the end of
250-
// a chain of redirects, we still haven't managed to
251-
// get a usable connection, we recurse on
252-
// mostRecentKnownHosts, trying each of those in
253-
// turn. Finally, if neither the initial
254-
// chain-of-redirects for the current endpoint, nor
255-
// the chains-of-redirects for each of the
256-
// mostRecentKnownHosts gives us a usable connection,
257-
// we give up on this particular endpoint, and
258-
// continue with the foreach loop, trying the
259-
// remainder of the array we were given.
260-
IConnection conn = FollowRedirectChain(maxRedirects,
261-
connectionAttempts,
262-
connectionErrors,
263-
ref mostRecentKnownHosts,
264-
endpoint);
265-
if (conn != null) {
266-
return conn;
267-
}
268-
269-
// Connection to this endpoint failed at some point
270-
// down the redirection chain - either the first
271-
// entry, or one of the re.Host values from subsequent
272-
// RedirectExceptions. We recurse into
273-
// mostRecentKnownHosts, to see if one of those is
274-
// suitable.
275-
if (mostRecentKnownHosts.Length > 0) {
276-
// Only bother recursing if we know of some
277-
// hosts. If we were to recurse with no endpoints
278-
// in the array, we'd stomp on
279-
// mostRecentException, which makes debugging
280-
// connectivity problems needlessly more
281-
// difficult.
282-
conn = CreateConnection(maxRedirects,
283-
connectionAttempts,
284-
connectionErrors,
285-
mostRecentKnownHosts);
286-
if (conn != null) {
287-
return conn;
288-
}
289-
}
290-
}
291-
return null;
292-
}
293219

294-
///<summary>Create a connection to the first available
295-
///endpoint in the list provided. Up to a maximum of
296-
///maxRedirects broker-originated redirects are permitted for
297-
///each endpoint tried.</summary>
298-
public virtual IConnection CreateConnection(int maxRedirects)
299-
{
300-
IDictionary<AmqpTcpEndpoint, int> connectionAttempts = new Dictionary<AmqpTcpEndpoint, int>();
301-
Dictionary<AmqpTcpEndpoint, Exception> connectionErrors = new Dictionary<AmqpTcpEndpoint, Exception>();
302-
IConnection conn = CreateConnection(maxRedirects,
303-
connectionAttempts,
304-
connectionErrors,
305-
new AmqpTcpEndpoint[]{Endpoint});
306-
if (conn != null) {
307-
return conn;
220+
throw new BrokerUnreachableException(e);
308221
}
309-
Exception Inner = connectionErrors[Endpoint] as Exception;
310-
throw new BrokerUnreachableException(connectionAttempts, connectionErrors, Inner);
311-
}
312222

313-
///<summary>Create a connection to the specified endpoint
314-
///No broker-originated redirects are permitted.</summary>
315-
public virtual IConnection CreateConnection()
316-
{
317-
return CreateConnection(0);
223+
return conn;
318224
}
319225

320226
///<summary>Given a list of mechanism names supported by the

projects/client/RabbitMQ.Client/src/client/api/IModel.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -928,13 +928,6 @@ void _Private_ConnectionOpen(string virtualHost,
928928
void HandleConnectionOpenOk([AmqpFieldMapping("RabbitMQ.Client.Framing.v0_9_1", "reserved1")]
929929
string knownHosts);
930930

931-
///<summary>Handle an incoming Connection.Redirect.
932-
/// (not available in AMQP 0-9-1)
933-
///</summary>
934-
[AmqpMethodDoNotImplement("RabbitMQ.Client.Framing.v0_9_1")]
935-
void HandleConnectionRedirect(string host,
936-
string knownHosts);
937-
938931
///<summary>Used to send a Connection.Close. Called during
939932
///connection shutdown.</summary>
940933
[AmqpMethodMapping(null, "connection", "close")]

projects/client/RabbitMQ.Client/src/client/exceptions/BrokerUnreachableException.cs

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -48,61 +48,11 @@ namespace RabbitMQ.Client.Exceptions {
4848

4949
///<summary>Thrown when no connection could be opened during a
5050
///ConnectionFactory.CreateConnection attempt.</summary>
51-
///<remarks>
52-
/// CreateConnection (optionally) handles redirections, so even a
53-
/// single-endpoint connection attempt may end up attempting to
54-
/// connect to multiple TCP endpoints. This exception contains
55-
/// information on how many times each endpoint was tried, and the
56-
/// outcome of the most recent attempt against each endpoint. See
57-
/// the ConnectionAttempts and ConnectionErrors properties.
58-
///</remarks>
5951
public class BrokerUnreachableException: IOException
6052
{
61-
private IDictionary<AmqpTcpEndpoint, int> m_connectionAttempts;
62-
private IDictionary<AmqpTcpEndpoint, Exception> m_connectionErrors;
63-
64-
///<summary>A map from AmqpTcpEndpoint to int, counting the
65-
///number of attempts that were made against each
66-
///endpoint.</summary>
67-
public IDictionary<AmqpTcpEndpoint, int> ConnectionAttempts { get { return m_connectionAttempts; } }
68-
69-
///<summary>A map from AmqpTcpEndpoint to Exception, recording
70-
///the outcome of the most recent connection attempt against
71-
///each endpoint.</summary>
72-
public IDictionary<AmqpTcpEndpoint, Exception> ConnectionErrors { get { return m_connectionErrors; } }
73-
74-
///<summary>same as ConnectionErrors property</summary>
75-
public override IDictionary Data { get { return new Dictionary<AmqpTcpEndpoint, Exception>(m_connectionErrors); } }
76-
77-
///<summary>Construct a BrokerUnreachableException. Expects
78-
///maps as per the description of the ConnectionAttempts and
79-
///ConnectionErrors properties. The inner exception is associated
53+
///<summary>Construct a BrokerUnreachableException. The inner exception is associated
8054
///with only one connection attempt.</summary>
81-
public BrokerUnreachableException(IDictionary<AmqpTcpEndpoint, int> connectionAttempts,
82-
IDictionary<AmqpTcpEndpoint, Exception> connectionErrors,
83-
Exception Inner)
84-
: base("None of the specified endpoints were reachable", Inner)
85-
{
86-
m_connectionAttempts = connectionAttempts;
87-
m_connectionErrors = connectionErrors;
88-
}
89-
90-
///<summary>Provide a full description of the various
91-
///connection attempts that were made, as well as the usual
92-
///Exception stack trace.</summary>
93-
public override string ToString() {
94-
StringBuilder sb = new StringBuilder(base.Message);
95-
sb.Append("\nEndpoints attempted:\n");
96-
foreach (KeyValuePair<AmqpTcpEndpoint, int> entry in m_connectionAttempts) {
97-
sb.Append("------------------------------------------------\n");
98-
sb.Append("endpoint=").Append(entry.Key);
99-
sb.Append(", attempts=").Append(entry.Value).Append("\n");
100-
sb.Append(m_connectionErrors[entry.Key] as Exception);
101-
}
102-
sb.Append("\n================================================\n");
103-
sb.Append("Stack trace:\n");
104-
sb.Append(base.StackTrace);
105-
return sb.ToString();
106-
}
55+
public BrokerUnreachableException(Exception Inner)
56+
: base("None of the specified endpoints were reachable", Inner) {}
10757
}
10858
}

projects/client/RabbitMQ.Client/src/client/impl/ModelBase.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,16 +1507,6 @@ public void HandleConnectionOpenOk(string knownHosts)
15071507
k.HandleCommand(null); // release the continuation.
15081508
}
15091509

1510-
public void HandleConnectionRedirect(string host,
1511-
string knownHosts)
1512-
{
1513-
ConnectionOpenContinuation k = (ConnectionOpenContinuation)m_continuationQueue.Next();
1514-
k.m_redirect = true;
1515-
k.m_host = host;
1516-
k.m_knownHosts = knownHosts;
1517-
k.HandleCommand(null); // release the continuation.
1518-
}
1519-
15201510
public abstract void _Private_ConnectionClose(ushort replyCode,
15211511
string replyText,
15221512
ushort classId,

projects/client/Unit/src/unit/TestAuth.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,7 @@ public void TestAuthFailure()
7070
}
7171
catch (BrokerUnreachableException bue)
7272
{
73-
foreach (Object failureReason in bue.ConnectionErrors.Values)
74-
{
75-
Assert.IsInstanceOf<AuthenticationFailureException>(
76-
failureReason);
77-
}
73+
Assert.IsInstanceOf<AuthenticationFailureException>(bue.InnerException);
7874
}
7975
}
8076
}

0 commit comments

Comments
 (0)