private static AgentServiceCheck CreateCheck(ConsulDiscoveryOptions options, HeartbeatOptions heartbeatOptions)
        {
            if (!options.RegisterHealthCheck)
            {
                return(null);
            }

            TimeSpan?deregisterCriticalServiceAfter = null;
            TimeSpan?timeout = null;

            if (!string.IsNullOrWhiteSpace(options.HealthCheckTimeout))
            {
                timeout = DateTimeConversions.ToTimeSpan(options.HealthCheckTimeout);
            }

            if (!string.IsNullOrWhiteSpace(options.HealthCheckCriticalTimeout))
            {
                deregisterCriticalServiceAfter = DateTimeConversions.ToTimeSpan(options.HealthCheckCriticalTimeout);
            }

            var check = new AgentServiceCheck
            {
                Timeout = timeout,
                DeregisterCriticalServiceAfter = deregisterCriticalServiceAfter
            };

            if (heartbeatOptions.Enable)
            {
                return(SetHeartbeat(check, heartbeatOptions) ? check : null);
            }

            return(SetHttpCheck(check, options) ? check : null);
        }
        private static void AddConsulServices(IServiceCollection services, IDiscoveryLifecycle lifecycle)
        {
            services.AddSingleton(s =>
            {
                var consulOptions = s.GetRequiredService <IOptions <ConsulOptions> >().Value;
                return(new ConsulClient(options =>
                {
                    options.Address = new Uri($"{consulOptions.Scheme}://{consulOptions.Host}:{consulOptions.Port}");
                    options.Datacenter = consulOptions.Datacenter;
                    options.Token = consulOptions.Token;
                    if (!string.IsNullOrWhiteSpace(consulOptions.WaitTime))
                    {
                        options.WaitTime = DateTimeConversions.ToTimeSpan(consulOptions.WaitTime);
                    }
                }));
            });
            services.AddSingleton <TtlScheduler>();

            services.AddSingleton <ConsulDiscoveryClient>();

            services.AddSingleton <ConsulServiceRegistry>();

            if (lifecycle == null)
            {
                services.AddSingleton <IDiscoveryLifecycle, ApplicationLifecycle>();
            }
            else
            {
                services.AddSingleton(lifecycle);
            }

            services.AddSingleton <IDiscoveryClient>(p => p.GetService <ConsulDiscoveryClient>());
        }
Esempio n. 3
0
        internal TimeSpan ComputeHearbeatInterval()
        {
            var second = TimeSpan.FromSeconds(1);
            var ttl    = DateTimeConversions.ToTimeSpan(TtlValue, TtlUnit);

            // heartbeat rate at ratio * ttl, but no later than ttl -1s and, (under lesser priority),
            // no sooner than 1s from now
            var interval     = ttl * IntervalRatio;
            var max          = interval > second ? interval : second;
            var ttlMinus1sec = ttl - second;
            var min          = ttlMinus1sec < max ? ttlMinus1sec : max;

            return(min);
        }
        private static bool SetHeartbeat(AgentServiceCheck check, HeartbeatOptions heartbeatOptions)
        {
            if (!heartbeatOptions.Enable || heartbeatOptions.TtlValue <= 0 || string.IsNullOrEmpty(heartbeatOptions.TtlUnit))
            {
                return(false);
            }

            check.Interval = null;
            check.HTTP     = null;

            TimeSpan?ttl = DateTimeConversions.ToTimeSpan(heartbeatOptions.TtlValue + heartbeatOptions.TtlUnit);

            check.TTL = ttl;

            return(true);
        }
Esempio n. 5
0
        internal static AgentServiceCheck CreateCheck(int port, ConsulDiscoveryOptions options)
        {
            AgentServiceCheck check = new AgentServiceCheck();

            if (options.IsHeartBeatEnabled)
            {
                check.TTL = DateTimeConversions.ToTimeSpan(options.Heartbeat.Ttl);
                return(check);
            }

            if (port <= 0)
            {
                throw new ArgumentException("CreateCheck port must be greater than 0");
            }

            if (!string.IsNullOrEmpty(options.HealthCheckUrl))
            {
                check.HTTP = options.HealthCheckUrl;
            }
            else
            {
                var uri = new Uri($"{options.Scheme}://{options.HostName}:{port}{options.HealthCheckPath}");
                check.HTTP = uri.ToString();
            }

            // check.setHeader(properties.getHealthCheckHeaders());
            if (!string.IsNullOrEmpty(options.HealthCheckInterval))
            {
                check.Interval = DateTimeConversions.ToTimeSpan(options.HealthCheckInterval);
            }

            if (!string.IsNullOrEmpty(options.HealthCheckTimeout))
            {
                check.Timeout = DateTimeConversions.ToTimeSpan(options.HealthCheckTimeout);
            }

            if (!string.IsNullOrEmpty(options.HealthCheckCriticalTimeout))
            {
                check.DeregisterCriticalServiceAfter = DateTimeConversions.ToTimeSpan(options.HealthCheckCriticalTimeout);
            }

            check.TLSSkipVerify = options.HealthCheckTlsSkipVerify;
            return(check);
        }
Esempio n. 6
0
        public void CreateCheck_ReturnsExpected()
        {
            ConsulDiscoveryOptions options = new ConsulDiscoveryOptions();
            var result = ConsulRegistration.CreateCheck(1234, options);

            Assert.NotNull(result);
            Assert.Equal(DateTimeConversions.ToTimeSpan(options.Heartbeat.Ttl), result.TTL);
            Assert.Equal(DateTimeConversions.ToTimeSpan(options.HealthCheckCriticalTimeout), result.DeregisterCriticalServiceAfter);

            options.Heartbeat = null;
            Assert.Throws <ArgumentException>(() => ConsulRegistration.CreateCheck(0, options));

            int port = 1234;

            result = ConsulRegistration.CreateCheck(port, options);
            var uri = new Uri($"{options.Scheme}://{options.HostName}:{port}{options.HealthCheckPath}");

            Assert.Equal(uri.ToString(), result.HTTP);
            Assert.Equal(DateTimeConversions.ToTimeSpan(options.HealthCheckInterval), result.Interval);
            Assert.Equal(DateTimeConversions.ToTimeSpan(options.HealthCheckTimeout), result.Timeout);
            Assert.Equal(DateTimeConversions.ToTimeSpan(options.HealthCheckCriticalTimeout), result.DeregisterCriticalServiceAfter);
            Assert.Equal(options.HealthCheckTlsSkipVerify, result.TLSSkipVerify);
        }
        private static bool SetHttpCheck(AgentServiceCheck check, ConsulDiscoveryOptions options)
        {
            var healthCheckUrl = options.HealthCheckUrl;

            if (string.IsNullOrEmpty(healthCheckUrl))
            {
                var hostString = options.HostName;
                var port       = options.Port;
                hostString += ":" + port;

                var healthCheckPath = options.HealthCheckPath;
                if (!healthCheckPath.StartsWith("/"))
                {
                    healthCheckPath = "/" + healthCheckPath;
                }

                healthCheckUrl = $"{options.Scheme}://{hostString}{healthCheckPath}";
            }

            TimeSpan?interval = null;

            if (!string.IsNullOrWhiteSpace(options.HealthCheckInterval))
            {
                interval = DateTimeConversions.ToTimeSpan(options.HealthCheckInterval);
            }

            if (string.IsNullOrEmpty(healthCheckUrl) || interval == null)
            {
                return(false);
            }

            check.HTTP     = healthCheckUrl;
            check.Interval = interval;

            return(true);
        }