public RedirectException(AmqpTcpEndpoint host, AmqpTcpEndpoint[] knownHosts) : base(string.Format("The connection.open attempt was redirected to host '{0}'", host)) { m_host = host; m_knownHosts = knownHosts; }
///<summary>Compares this instance by value (protocol, ///hostname, port) against another instance</summary> public override bool Equals(object obj) { AmqpTcpEndpoint other = obj as AmqpTcpEndpoint; if (other == null) { return(false); } if (other.HostName != HostName) { return(false); } if (other.Port != Port) { return(false); } return(true); }
internal bool InternalStart(int maxConnectionRetry, ConnectionFailureException callReason = null) { var ok = false; var retries = 0; var exceptions = new Dictionary<AmqpTcpEndpoint, Exception>(1); var attempts = new Dictionary<AmqpTcpEndpoint, int>(1); if (HasAlreadyStartedOnce) OnTemporaryConnectionFailureFailure(callReason); while (!ok && (retries++ <= maxConnectionRetry || maxConnectionRetry == -1 || maxConnectionRetry == Timeout.Infinite) && !Cancellation.IsCancellationRequested) { try { if (Model != null) { try { Model.Close(); } catch { // best effort to close the previous channel, ignore errors } } Connection = CreateConnection(); Model = Connection.CreateModel(); Connection.AutoClose = true; TryRedeclareTopology(); SpecificRestart(Model); ok = true; HasAlreadyStartedOnce = true; } catch (Exception e) { var endpoint = new AmqpTcpEndpoint(); if (_settings.ConnectionFactory != null && _settings.ConnectionFactory.Endpoint != null) endpoint = _settings.ConnectionFactory.Endpoint; exceptions[endpoint] = e; attempts[endpoint] = retries; OnTemporaryConnectionFailureFailure(e); Thread.Sleep(_settings.IntervalConnectionTries); } } if (!ok) { var e = new BrokerUnreachableException(attempts, exceptions); OnPermanentConnectionFailureFailure(e); } return ok; }
public abstract IFrameHandler CreateFrameHandler(TcpClient socket, AmqpTcpEndpoint endpoint, int timeout);
public void Can_create_connections_to_different_endpoints_which_have_the_same_virtualHost() { // Arrange var endpoint1 = new AmqpTcpEndpoint("localhost", 5672); var connectionFactory1 = CreateMockConnectionFactory<ManagedConnectionFactory>("/", endpoint1); var durableConnection1 = new DurableConnection(Substitute.For<IRetryPolicy>(), Substitute.For<IRabbitWatcher>(), connectionFactory1); var endpoint2 = new AmqpTcpEndpoint("localhost", 5673); var connectionFactory2 = CreateMockConnectionFactory<ManagedConnectionFactory>("/", endpoint2); var durableConnection2 = new DurableConnection(Substitute.For<IRetryPolicy>(), Substitute.For<IRabbitWatcher>(), connectionFactory2); // Action var tasks = new List<Task>(); for (var i = 0; i < 100; i++) { tasks.Add(Task.Factory.StartNew(durableConnection1.Connect)); tasks.Add(Task.Factory.StartNew(durableConnection2.Connect)); } Task.WaitAll(tasks.ToArray()); // Assert connectionFactory1.Received(1).CreateConnection(); connectionFactory2.Received(1).CreateConnection(); Assert.AreEqual(2, ManagedConnectionFactory.SharedConnections.Count); }
private void SetUri(Uri uri) { Endpoint = new AmqpTcpEndpoint(); if (string.Equals("amqp", uri.Scheme, StringComparison.OrdinalIgnoreCase)) { // nothing special to do } else if (string.Equals("amqps", uri.Scheme, StringComparison.OrdinalIgnoreCase)) { Ssl.Enabled = true; #if !(NETFX_CORE) Ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch; #endif Port = AmqpTcpEndpoint.DefaultAmqpSslPort; } else { throw new ArgumentException("Wrong scheme in AMQP URI: " + uri.Scheme); } string host = uri.Host; if (!string.IsNullOrEmpty(host)) { HostName = host; } Ssl.ServerName = HostName; int port = uri.Port; if (port != -1) { Port = port; } string userInfo = uri.UserInfo; if (!string.IsNullOrEmpty(userInfo)) { string[] userPass = userInfo.Split(':'); if (userPass.Length > 2) { throw new ArgumentException("Bad user info in AMQP " + "URI: " + userInfo); } UserName = UriDecode(userPass[0]); if (userPass.Length == 2) { Password = UriDecode(userPass[1]); } } /* C# automatically changes URIs into a canonical form that has at least the path segment "/". */ if (uri.Segments.Length > 2) { throw new ArgumentException("Multiple segments in " + "path of AMQP URI: " + string.Join(", ", uri.Segments)); } if (uri.Segments.Length == 2) { VirtualHost = UriDecode(uri.Segments[1]); } }
public IFrameHandler CreateFrameHandler(AmqpTcpEndpoint endpoint, ConnectionFactoryBase.ObtainSocket socketFactory, int timeout) { return new SocketFrameHandler(endpoint, socketFactory, timeout); }
public override IFrameHandler CreateFrameHandler(AmqpTcpEndpoint endpoint, ConnectionFactory.ObtainSocket socketFactory, int timeout) { return new SocketFrameHandler_0_9(endpoint, socketFactory, timeout); }
public abstract IFrameHandler CreateFrameHandler(AmqpTcpEndpoint endpoint);
public abstract IFrameHandler CreateFrameHandler(AmqpTcpEndpoint endpoint, ConnectionFactory.ObtainSocket socketFactory, int timeout);
public override IFrameHandler CreateFrameHandler(TcpClient socket, AmqpTcpEndpoint endpoint, int timeout) { return new SocketFrameHandler_0_9(socket, endpoint, timeout); }
public IFrameHandler CreateFrameHandler(AmqpTcpEndpoint endpoint) { return(Protocols.DefaultProtocol.CreateFrameHandler(endpoint, SocketFactory, RequestedConnectionTimeout)); }
protected virtual IConnection FollowRedirectChain(int maxRedirects, IDictionary connectionAttempts, IDictionary connectionErrors, ref AmqpTcpEndpoint[] mostRecentKnownHosts, AmqpTcpEndpoint endpoint) { AmqpTcpEndpoint candidate = endpoint; try { while (true) { int attemptCount = connectionAttempts.Contains(candidate) ? (int) connectionAttempts[candidate] : 0; connectionAttempts[candidate] = attemptCount + 1; bool insist = attemptCount >= maxRedirects; try { IProtocol p = candidate.Protocol; TcpClient socket = null; IFrameHandler fh = null; if (Socket.OSSupportsIPv6) { try { socket = new TcpClient(AddressFamily.InterNetworkV6); ConfigureSocket(socket); fh = p.CreateFrameHandler(socket, candidate, RequestedConnectionTimeout); } // Don't attempt to use an IPv4 socket, as timeout was exceeded catch (TimeoutException) { throw; } // Socket error, do try an IPv4 socket. catch (SocketException) { socket = null; fh = null; } // IPv4 address was used for endpoint hostname, try IPv4 socket catch (ArgumentException) { socket = null; fh = null; } } if (socket == null) { socket = new TcpClient(AddressFamily.InterNetwork); ConfigureSocket(socket); fh = p.CreateFrameHandler(socket, candidate, RequestedConnectionTimeout); } // At this point, we may be able to create // and fully open a successful connection, // in which case we're done, and the // connection should be returned. return p.CreateConnection(this, insist, fh); } catch (RedirectException re) { if (insist) { // We've been redirected, but we insisted that // we shouldn't be redirected! Well-behaved // brokers should never do this. string message = string.Format("Server {0} ignored 'insist' flag, redirecting us to {1}", candidate, re.Host); throw new ProtocolViolationException(message); } else { // We've been redirected. Follow this new link // in the chain, by setting // mostRecentKnownHosts (in case the chain // runs out), and updating candidate for the // next time round the loop. connectionErrors[candidate] = re; mostRecentKnownHosts = re.KnownHosts; candidate = re.Host; } } } } catch (Exception e) { connectionErrors[candidate] = e; return null; } }
protected virtual IConnection CreateConnection(int maxRedirects, IDictionary<AmqpTcpEndpoint, int> connectionAttempts, IDictionary<AmqpTcpEndpoint, Exception> connectionErrors, params AmqpTcpEndpoint[] endpoints) { foreach (AmqpTcpEndpoint endpoint in endpoints) { AmqpTcpEndpoint[] mostRecentKnownHosts = new AmqpTcpEndpoint[0]; // ^^ holds a list of known-hosts that came back with // a connection.redirect. If, once we reach the end of // a chain of redirects, we still haven't managed to // get a usable connection, we recurse on // mostRecentKnownHosts, trying each of those in // turn. Finally, if neither the initial // chain-of-redirects for the current endpoint, nor // the chains-of-redirects for each of the // mostRecentKnownHosts gives us a usable connection, // we give up on this particular endpoint, and // continue with the foreach loop, trying the // remainder of the array we were given. IConnection conn = FollowRedirectChain(maxRedirects, connectionAttempts, connectionErrors, ref mostRecentKnownHosts, endpoint); if (conn != null) { return conn; } // Connection to this endpoint failed at some point // down the redirection chain - either the first // entry, or one of the re.Host values from subsequent // RedirectExceptions. We recurse into // mostRecentKnownHosts, to see if one of those is // suitable. if (mostRecentKnownHosts.Length > 0) { // Only bother recursing if we know of some // hosts. If we were to recurse with no endpoints // in the array, we'd stomp on // mostRecentException, which makes debugging // connectivity problems needlessly more // difficult. conn = CreateConnection(maxRedirects, connectionAttempts, connectionErrors, mostRecentKnownHosts); if (conn != null) { return conn; } } } return null; }
protected virtual IConnection FollowRedirectChain (int maxRedirects, IDictionary connectionAttempts, IDictionary connectionErrors, ref AmqpTcpEndpoint[] mostRecentKnownHosts, AmqpTcpEndpoint endpoint) { AmqpTcpEndpoint candidate = endpoint; try { while (true) { int attemptCount = connectionAttempts.Contains(candidate) ? (int)connectionAttempts[candidate] : 0; connectionAttempts[candidate] = attemptCount + 1; bool insist = attemptCount >= maxRedirects; try { IProtocol p = candidate.Protocol; IFrameHandler fh = p.CreateFrameHandler(candidate); // At this point, we may be able to create // and fully open a successful connection, // in which case we're done, and the // connection should be returned. return(p.CreateConnection(this, insist, fh)); } catch (RedirectException re) { if (insist) { // We've been redirected, but we insisted that // we shouldn't be redirected! Well-behaved // brokers should never do this. string message = string.Format("Server {0} ignored 'insist' flag, redirecting us to {1}", candidate, re.Host); throw new ProtocolViolationException(message); } else { // We've been redirected. Follow this new link // in the chain, by setting // mostRecentKnownHosts (in case the chain // runs out), and updating candidate for the // next time round the loop. connectionErrors[candidate] = re; mostRecentKnownHosts = re.KnownHosts; candidate = re.Host; } } } } catch (Exception e) { connectionErrors[candidate] = e; return(null); } }
protected virtual IConnection FollowRedirectChain(int maxRedirects, IDictionary<AmqpTcpEndpoint, int> connectionAttempts, IDictionary<AmqpTcpEndpoint, Exception> connectionErrors, ref AmqpTcpEndpoint[] mostRecentKnownHosts, AmqpTcpEndpoint endpoint) { AmqpTcpEndpoint candidate = endpoint; try { while (true) { int attemptCount = connectionAttempts.ContainsKey(candidate) ? (int) connectionAttempts[candidate] : 0; connectionAttempts[candidate] = attemptCount + 1; bool insist = attemptCount >= maxRedirects; try { IProtocol p = candidate.Protocol; IFrameHandler fh = p.CreateFrameHandler(candidate, SocketFactory, RequestedConnectionTimeout); // At this point, we may be able to create // and fully open a successful connection, // in which case we're done, and the // connection should be returned. return p.CreateConnection(this, insist, fh); } catch (RedirectException re) { if (insist) { // We've been redirected, but we insisted that // we shouldn't be redirected! Well-behaved // brokers should never do this. string message = string.Format("Server {0} ignored 'insist' flag, redirecting us to {1}", candidate, re.Host); throw new ProtocolViolationException(message); } else { // We've been redirected. Follow this new link // in the chain, by setting // mostRecentKnownHosts (in case the chain // runs out), and updating candidate for the // next time round the loop. connectionErrors[candidate] = re; mostRecentKnownHosts = re.KnownHosts; candidate = re.Host; } } } } catch (Exception e) { connectionErrors[candidate] = e; return null; } }
public override IFrameHandler CreateFrameHandler(AmqpTcpEndpoint endpoint) { return new SocketFrameHandler_0_9(endpoint); }
///<summary>Create a connection to the host (and optional ///port) specified, with the IProtocol from ///Protocols.FromEnvironment(). The format of the address ///string is the same as that accepted by ///AmqpTcpEndpoint.Parse().</summary> public IConnection CreateConnection(string address) { return(CreateConnection(AmqpTcpEndpoint.Parse(Protocols.FromEnvironment(), address))); }
public IFrameHandler CreateFrameHandler(AmqpTcpEndpoint endpoint) { return Protocols.DefaultProtocol.CreateFrameHandler(endpoint, SocketFactory, RequestedConnectionTimeout); }
public IFrameHandler CreateFrameHandler(AmqpTcpEndpoint endpoint) { var fh = Protocols.DefaultProtocol.CreateFrameHandler(endpoint, SocketFactory, RequestedConnectionTimeout, SocketReadTimeout, SocketWriteTimeout); return ConfigureFrameHandler(fh); }
internal IFrameHandler CreateFrameHandler(AmqpTcpEndpoint endpoint) { IFrameHandler fh = new SocketFrameHandler(endpoint, SocketFactory, RequestedConnectionTimeout, SocketReadTimeout, SocketWriteTimeout); return(ConfigureFrameHandler(fh)); }
private void SetUri(Uri uri) { Endpoint = new AmqpTcpEndpoint(); if (string.Equals("amqp", uri.Scheme, StringComparison.OrdinalIgnoreCase)) { // nothing special to do } else if (string.Equals("amqps", uri.Scheme, StringComparison.OrdinalIgnoreCase)) { Ssl.Enabled = true; Ssl.Version = AmqpUriSslProtocols; Ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch; Port = AmqpTcpEndpoint.DefaultAmqpSslPort; } else { throw new ArgumentException($"Wrong scheme in AMQP URI: {uri.Scheme}"); } string host = uri.Host; if (!string.IsNullOrEmpty(host)) { HostName = host; } Ssl.ServerName = HostName; int port = uri.Port; if (port != -1) { Port = port; } string userInfo = uri.UserInfo; if (!string.IsNullOrEmpty(userInfo)) { string[] userPass = userInfo.Split(':'); if (userPass.Length > 2) { throw new ArgumentException($"Bad user info in AMQP URI: {userInfo}"); } UserName = UriDecode(userPass[0]); if (userPass.Length == 2) { Password = UriDecode(userPass[1]); } } /* C# automatically changes URIs into a canonical form * that has at least the path segment "/". */ if (uri.Segments.Length > 2) { throw new ArgumentException($"Multiple segments in path of AMQP URI: {string.Join(", ", uri.Segments)}"); } if (uri.Segments.Length == 2) { VirtualHost = UriDecode(uri.Segments[1]); } _uri = uri; }
private void SetUri(Uri uri) { Endpoint = new AmqpTcpEndpoint(); if ("amqp".CompareTo(uri.Scheme.ToLower()) == 0) { // nothing special to do } else if ("amqps".CompareTo(uri.Scheme.ToLower()) == 0) { Ssl.Enabled = true; Ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch; Port = AmqpTcpEndpoint.DefaultAmqpSslPort; } else { throw new ArgumentException("Wrong scheme in AMQP URI: " + uri.Scheme); } string host = uri.Host; if (!String.IsNullOrEmpty(host)) { HostName = host; } Ssl.ServerName = HostName; int port = uri.Port; if (port != -1) { Port = port; } string userInfo = uri.UserInfo; if (!String.IsNullOrEmpty(userInfo)) { string[] userPass = userInfo.Split(':'); if (userPass.Length > 2) { throw new ArgumentException("Bad user info in AMQP " + "URI: " + userInfo); } UserName = UriDecode(userPass[0]); if (userPass.Length == 2) { Password = UriDecode(userPass[1]); } } /* C# automatically changes URIs into a canonical form * that has at least the path segment "/". */ if (uri.Segments.Length > 2) { throw new ArgumentException("Multiple segments in " + "path of AMQP URI: " + String.Join(", ", uri.Segments)); } else if (uri.Segments.Length == 2) { VirtualHost = UriDecode(uri.Segments[1]); } }