Example #1
0
        public static IPEndPointSource MakeEndPointSource(string host, int port, TimeSpan?endpointCacheDuration)
        {
            if (string.IsNullOrWhiteSpace(host))
            {
                throw new ArgumentException("statsd host is null or empty");
            }

            IPAddress address;

            if (IPAddress.TryParse(host, out address))
            {
                //if we were given an IP instead of a hostname,
                // we can happily keep it the life of this class
                var endpoint = new IPEndPoint(address, port);
                return(new SimpleIpEndpoint(endpoint));
            }

            // we have a host name,
            // so we use DNS lookup
            var uncachedLookup = new DnsLookupIpEndpointSource(host, port);

            if (!endpointCacheDuration.HasValue)
            {
                return(uncachedLookup);
            }

            return(new CachedIpEndpointSource(uncachedLookup, endpointCacheDuration.Value));
        }
Example #2
0
        /// <summary>
        /// Creates an <see cref="IEndPointSource"/> from the specified host IP address or name and port.
        /// </summary>
        /// <param name="host">The host name of IP address of the StatsD server.</param>
        /// <param name="port">The port number to use for the end point.</param>
        /// <param name="endpointCacheDuration">The optional period of time to cache the end point value for.</param>
        /// <returns>
        /// The created instance of <see cref="IEndPointSource"/>.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// <paramref name="host"/> is either <see langword="null"/> or the empty string.
        /// </exception>
        public static IEndPointSource MakeEndPointSource(string host, int port, TimeSpan?endpointCacheDuration)
        {
            if (string.IsNullOrWhiteSpace(host))
            {
                throw new ArgumentException("The StatsD host IP address or name is null or empty.", nameof(host));
            }

            IEndPointSource source;

            if (IPAddress.TryParse(host, out IPAddress address))
            {
                // If we were given an IP instead of a hostname,
                // we can happily keep it the life of this class
                var value = new IPEndPoint(address, port);
                source = new SimpleEndpointSource(value);
            }
            else
            {
                // We have a host name, so we use DNS lookup
                source = new DnsLookupIpEndpointSource(host, port);

                if (endpointCacheDuration.HasValue)
                {
                    source = new CachedEndpointSource(source, endpointCacheDuration.Value);
                }
            }

            return(source);
        }
        public static void GetEndpointPrefersIPV4WhenHostnameIsLocalhost()
        {
            // Arrange
            var target = new DnsLookupIpEndpointSource("localhost", 8125);

            // Act
            EndPoint actual = target.GetEndpoint();

            // Assert
            actual.ShouldNotBeNull();
            actual.AddressFamily.ShouldBe(AddressFamily.InterNetwork);

            var ipActual = actual as IPEndPoint;

            ipActual.ShouldNotBeNull();
            ipActual !.Address.ShouldBe(IPAddress.Parse("127.0.0.1"));
            ipActual.Port.ShouldBe(8125);
        }