Beispiel #1
0
        Task <bool> ITransportDelegator.PingAsync(ITransportRequestState requestState)
        {
            var pingTimeout = this.Settings.PingTimeout.GetValueOrDefault(DefaultPingTimeout);

            pingTimeout = requestState.RequestConfiguration != null
                                ? requestState.RequestConfiguration.ConnectTimeout.GetValueOrDefault(pingTimeout)
                                : pingTimeout;

            var requestOverrides = new RequestConfiguration
            {
                ConnectTimeout = pingTimeout,
                RequestTimeout = pingTimeout
            };
            var rq = requestState.InitiateRequest(RequestType.Ping);

            try
            {
                return(this.Connection.Head(requestState.CreatePathOnCurrentNode(""), requestOverrides)
                       .ContinueWith(t =>
                {
                    if (t.IsFaulted)
                    {
                        rq.Finish(false, null);
                        rq.Dispose();
                        throw new PingException(requestState.CurrentNode, t.Exception);
                    }
                    rq.Finish(t.Result.Success, t.Result.HttpStatusCode);
                    rq.Dispose();
                    var response = t.Result;
                    if (!response.HttpStatusCode.HasValue || response.HttpStatusCode.Value == -1)
                    {
                        throw new PingException(requestState.CurrentNode, t.Exception);
                    }
                    if (response.HttpStatusCode == (int)HttpStatusCode.Unauthorized)
                    {
                        throw new ElasticsearchAuthenticationException(response);
                    }
                    using (response.Response)
                        return response.Success;
                }));
            }
            catch (ElasticsearchAuthenticationException)
            {
                throw;
            }
            catch (Exception e)
            {
                var tcs           = new TaskCompletionSource <bool>();
                var pingException = new PingException(requestState.CurrentNode, e);
                tcs.SetException(pingException);
                return(tcs.Task);
            }
        }
Beispiel #2
0
        /* PING/SNIFF	*** ********************************************/

        bool ITransportDelegator.Ping(ITransportRequestState requestState)
        {
            var pingTimeout = this.Settings.PingTimeout.GetValueOrDefault(DefaultPingTimeout);

            pingTimeout = requestState.RequestConfiguration != null
                                ? requestState.RequestConfiguration.ConnectTimeout.GetValueOrDefault(pingTimeout)
                                : pingTimeout;

            var requestOverrides = new RequestConfiguration
            {
                ConnectTimeout = pingTimeout,
                RequestTimeout = pingTimeout
            };

            try
            {
                ElasticsearchResponse <Stream> response;
                using (var rq = requestState.InitiateRequest(RequestType.Ping))
                {
                    response = this.Connection.HeadSync(requestState.CreatePathOnCurrentNode(""), requestOverrides);
                    rq.Finish(response.Success, response.HttpStatusCode);
                }
                if (!response.HttpStatusCode.HasValue || response.HttpStatusCode.Value == -1)
                {
                    throw new Exception("ping returned no status code", response.OriginalException);
                }
                if (response.HttpStatusCode == (int)HttpStatusCode.Unauthorized)
                {
                    throw new ElasticsearchAuthenticationException(response);
                }
                if (response.Response == null)
                {
                    return(response.Success);
                }
                using (response.Response)
                    return(response.Success);
            }
            catch (ElasticsearchAuthenticationException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new PingException(requestState.CurrentNode, e);
            }
        }