Skip to content

Commit 350be31

Browse files
merge bug26131 into bug26133
2 parents 2c6b637 + f774135 commit 350be31

File tree

1 file changed

+22
-125
lines changed

1 file changed

+22
-125
lines changed

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

Lines changed: 22 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -204,139 +204,36 @@ public String Uri
204204
///their respective defaults.</summary>
205205
public ConnectionFactory() { }
206206

207-
protected virtual IConnection FollowRedirectChain
208-
(int maxRedirects,
209-
IDictionary<AmqpTcpEndpoint, int> connectionAttempts,
210-
IDictionary<AmqpTcpEndpoint, Exception> connectionErrors,
211-
ref AmqpTcpEndpoint[] mostRecentKnownHosts,
212-
AmqpTcpEndpoint endpoint)
207+
///<summary>Create a connection to the specified endpoint
208+
///No broker-originated redirects are permitted.</summary>
209+
public virtual IConnection CreateConnection()
213210
{
214-
AmqpTcpEndpoint candidate = endpoint;
215-
try {
216-
while (true) {
217-
int attemptCount =
218-
connectionAttempts.ContainsKey(candidate)
219-
? (int) connectionAttempts[candidate]
220-
: 0;
221-
connectionAttempts[candidate] = attemptCount + 1;
222-
bool insist = attemptCount >= maxRedirects;
223-
224-
try {
225-
IProtocol p = candidate.Protocol;
226-
IFrameHandler fh = p.CreateFrameHandler(candidate,
227-
SocketFactory,
228-
RequestedConnectionTimeout);
229-
230-
// At this point, we may be able to create
231-
// and fully open a successful connection,
232-
// in which case we're done, and the
233-
// connection should be returned.
234-
return p.CreateConnection(this, insist, fh);
235-
} catch (RedirectException re) {
236-
if (insist) {
237-
// We've been redirected, but we insisted that
238-
// we shouldn't be redirected! Well-behaved
239-
// brokers should never do this.
240-
string message = string.Format("Server {0} ignored 'insist' flag, redirecting us to {1}",
241-
candidate,
242-
re.Host);
243-
throw new ProtocolViolationException(message);
244-
} else {
245-
// We've been redirected. Follow this new link
246-
// in the chain, by setting
247-
// mostRecentKnownHosts (in case the chain
248-
// runs out), and updating candidate for the
249-
// next time round the loop.
250-
connectionErrors[candidate] = re;
251-
mostRecentKnownHosts = re.KnownHosts;
252-
candidate = re.Host;
253-
}
254-
}
255-
}
256-
} catch (Exception e) {
257-
connectionErrors[candidate] = e;
258-
return null;
259-
}
260-
}
211+
IDictionary<AmqpTcpEndpoint, int> attempts = new Dictionary<AmqpTcpEndpoint, int>();
212+
Dictionary<AmqpTcpEndpoint, Exception> errors = new Dictionary<AmqpTcpEndpoint, Exception>();
261213

262-
protected virtual IConnection CreateConnection(int maxRedirects,
263-
IDictionary<AmqpTcpEndpoint, int> connectionAttempts,
264-
IDictionary<AmqpTcpEndpoint, Exception> connectionErrors,
265-
params AmqpTcpEndpoint[] endpoints)
266-
{
267-
foreach (AmqpTcpEndpoint endpoint in endpoints)
214+
IConnection conn = null;
215+
try
216+
{
217+
IProtocol p = Endpoint.Protocol;
218+
IFrameHandler fh = p.CreateFrameHandler(Endpoint,
219+
SocketFactory,
220+
RequestedConnectionTimeout);
221+
conn = p.CreateConnection(this, false, fh);
222+
attempts[Endpoint] = 1;
223+
} catch (Exception e)
268224
{
269-
AmqpTcpEndpoint[] mostRecentKnownHosts = new AmqpTcpEndpoint[0];
270-
// ^^ holds a list of known-hosts that came back with
271-
// a connection.redirect. If, once we reach the end of
272-
// a chain of redirects, we still haven't managed to
273-
// get a usable connection, we recurse on
274-
// mostRecentKnownHosts, trying each of those in
275-
// turn. Finally, if neither the initial
276-
// chain-of-redirects for the current endpoint, nor
277-
// the chains-of-redirects for each of the
278-
// mostRecentKnownHosts gives us a usable connection,
279-
// we give up on this particular endpoint, and
280-
// continue with the foreach loop, trying the
281-
// remainder of the array we were given.
282-
IConnection conn = FollowRedirectChain(maxRedirects,
283-
connectionAttempts,
284-
connectionErrors,
285-
ref mostRecentKnownHosts,
286-
endpoint);
287-
if (conn != null) {
288-
return conn;
289-
}
290225

291-
// Connection to this endpoint failed at some point
292-
// down the redirection chain - either the first
293-
// entry, or one of the re.Host values from subsequent
294-
// RedirectExceptions. We recurse into
295-
// mostRecentKnownHosts, to see if one of those is
296-
// suitable.
297-
if (mostRecentKnownHosts.Length > 0) {
298-
// Only bother recursing if we know of some
299-
// hosts. If we were to recurse with no endpoints
300-
// in the array, we'd stomp on
301-
// mostRecentException, which makes debugging
302-
// connectivity problems needlessly more
303-
// difficult.
304-
conn = CreateConnection(maxRedirects,
305-
connectionAttempts,
306-
connectionErrors,
307-
mostRecentKnownHosts);
308-
if (conn != null) {
309-
return conn;
310-
}
311-
}
226+
errors[Endpoint] = e;
312227
}
313-
return null;
314-
}
315228

316-
///<summary>Create a connection to the first available
317-
///endpoint in the list provided. Up to a maximum of
318-
///maxRedirects broker-originated redirects are permitted for
319-
///each endpoint tried.</summary>
320-
public virtual IConnection CreateConnection(int maxRedirects)
321-
{
322-
IDictionary<AmqpTcpEndpoint, int> connectionAttempts = new Dictionary<AmqpTcpEndpoint, int>();
323-
Dictionary<AmqpTcpEndpoint, Exception> connectionErrors = new Dictionary<AmqpTcpEndpoint, Exception>();
324-
IConnection conn = CreateConnection(maxRedirects,
325-
connectionAttempts,
326-
connectionErrors,
327-
new AmqpTcpEndpoint[]{Endpoint});
328-
if (conn != null) {
229+
if(conn != null)
230+
{
329231
return conn;
232+
} else
233+
{
234+
Exception cause = errors[Endpoint] as Exception;
235+
throw new BrokerUnreachableException(attempts, errors, cause);
330236
}
331-
Exception Inner = connectionErrors[Endpoint] as Exception;
332-
throw new BrokerUnreachableException(connectionAttempts, connectionErrors, Inner);
333-
}
334-
335-
///<summary>Create a connection to the specified endpoint
336-
///No broker-originated redirects are permitted.</summary>
337-
public virtual IConnection CreateConnection()
338-
{
339-
return CreateConnection(0);
340237
}
341238

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

0 commit comments

Comments
 (0)