Example #1
0
        public async Task <bool> Status()
        {
            try
            {
                var request  = new StatusRequest();
                var response = await _client.StatusASync(request);

                return(response != null);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }
        }
        /// <summary>
        ///     Optional connection warm-up to speed up initial access.
        /// </summary>
        public async Task <bool> ConnectAsync()
        {
            try
            {
                var statusRequest = new StatusRequest();

                StatusResponse response = await _etcdClient.StatusASync(statusRequest);

                _logger.LogDebug("Successfully connected to etcd: {response}", response.ToString());
            }
            catch (Exception e)
            {
                _logger.LogWarning(e, "Error connecting to etcd using {client}", _etcdClient);
                return(false);
            }

            return(true);
        }
Example #3
0
        /// <summary>
        /// Initialise etcd client
        /// </summary>
        /// <param name="options"></param>
        private async Task <EtcdClient> CreateClient(EtcdConfigurationRepositoryOptions options)
        {
            EtcdClient etcdClient = null;

            var lookup = new LookupClient();

            if (options.UseEtcdClientDiscovery)
            {
                var dnsHostName = string.IsNullOrEmpty(options.DnsHostOverride)
                    ? $"_etcd-client${(options.UseTLS ? "-ssl" : "")}.${options.EtcdClientDiscoveryDomain}"
                    : options.DnsHostOverride;

                _logger.LogInformation("Attempting service discovery using hostname {dnsHostName}", dnsHostName);

                var dnsResponse = await lookup.QueryAsync(dnsHostName, QueryType.SRV);

                if (dnsResponse.Answers.Count > 0)
                {
                    foreach (var srvRecord in dnsResponse.Answers.SrvRecords())
                    {
                        _logger.LogInformation($"Connecting to etcd host {srvRecord.Target} using port {options.PortOverride ?? srvRecord.Port}.");
                        var tmpEtcdClient = new EtcdClient(srvRecord.Target, options.PortOverride ?? srvRecord.Port, options.Username, options.Password);
                        try
                        {
                            await tmpEtcdClient.StatusASync(new Etcdserverpb.StatusRequest());

                            etcdClient = tmpEtcdClient;
                            break;
                        }
                        catch (Exception ex)
                        {
                            _logger.LogWarning(ex, "Failed to connect to etcd cluster.");
                            tmpEtcdClient.Dispose();
                        }
                    }
                }

                if (etcdClient == null)
                {
                    if (!options.UseTLS) //Try again with TLS
                    {
                        _logger.LogInformation("Retrying discovery etcd cluster using TLS.");
                        options.UseTLS = true;
                        etcdClient     = await CreateClient(options);
                    }
                    else
                    {
                        throw new InvalidOperationException("Unable to connect to a etcd cluster because service discovery operations failed.");
                    }
                }
            }
            else if (!string.IsNullOrEmpty(options.DnsHostOverride))
            {
                _logger.LogInformation($"Connecting to etcd host {options.DnsHostOverride} using port {options.PortOverride ?? 2379}.");
                etcdClient = new EtcdClient(options.DnsHostOverride, options.PortOverride ?? 2379, options.Username, options.Password);
            }
            else
            {
                throw new ArgumentException("Must specify 'DnsHostOverride option' if 'UseEtcdDiscovery' option is set to false.");
            }

            return(etcdClient);
        }