Beispiel #1
0
        /* ASYNC *** */
        public Task <ElasticsearchResponse <T> > DoRequestAsync <T>(string method, string path, object data = null, IRequestParameters requestParameters = null)
        {
            using (var tracer = new ElasticsearchResponseTracer <T>(this.Settings.TraceEnabled))
            {
                var postData     = PostData(data);
                var requestState = new TransportRequestState <T>(tracer, method, path, postData, requestParameters);

                return(this.DoRequestAsync <T>(requestState)
                       .ContinueWith(t =>
                {
                    var tcs = new TaskCompletionSource <ElasticsearchResponse <T> >();
                    if (t.Exception != null)
                    {
                        tcs.SetException(t.Exception.Flatten());
                    }
                    else
                    {
                        tcs.SetResult(t.Result);
                    }

                    requestState.Tracer.SetResult(t.Result);
                    return tcs.Task;
                }).Unwrap());
            }
        }
Beispiel #2
0
        public IList <Uri> Sniff()
        {
            var pingTimeout      = this.Settings.PingTimeout.GetValueOrDefault(50);
            var requestOverrides = new RequestConfiguration()
                                   .ConnectTimeout(pingTimeout)
                                   .RequestTimeout(pingTimeout)
                                   .DisableSniffing();
            var requestParameters = new FluentRequestParameters()
                                    .RequestConfiguration(r => requestOverrides);

            var path = "_nodes/_all/clear?timeout=" + pingTimeout;

            using (var tracer = new ElasticsearchResponseTracer <Stream>(this.Settings.TraceEnabled))
            {
                var requestState = new TransportRequestState <Stream>(tracer, "GET", path, requestParameters: requestParameters);
                var response     = this.DoRequest(requestState);
                if (response.Response == null)
                {
                    return(null);
                }

                using (response.Response)
                {
                    return(Sniffer.FromStream(response, response.Response, this.Serializer));
                }
            }
        }
Beispiel #3
0
        /* SYNC *** */
        public ElasticsearchResponse <T> DoRequest <T>(string method, string path, object data = null, IRequestParameters requestParameters = null)
        {
            using (var tracer = new ElasticsearchResponseTracer <T>(this.Settings.TraceEnabled))
            {
                var postData     = PostData(data);
                var requestState = new TransportRequestState <T>(tracer, method, path, postData, requestParameters);

                var result = this.DoRequest <T>(requestState);
                tracer.SetResult(result);
                return(result);
            }
        }
Beispiel #4
0
        /* SYNC *** */
        public ElasticsearchResponse <T> DoRequest <T>(string method, string path, object data = null, IRequestParameters requestParameters = null)
        {
            using (var tracer = new ElasticsearchResponseTracer <T>(this.Settings.TraceEnabled))
            {
                var postData     = PostData(data);
                var requestState = new TransportRequestState <T>(tracer, method, path, postData, requestParameters);

                var result = this.DoRequest <T>(requestState);
                var objectNeedsResponseRef = result.Response as IResponseWithRequestInformation;
                if (objectNeedsResponseRef != null)
                {
                    objectNeedsResponseRef.RequestInformation = result;
                }
                tracer.SetResult(result);
                return(result);
            }
        }
        private IEnumerable<Task> _AsyncSteps(HttpWebRequest request, TaskCompletionSource<ElasticsearchResponse> tcs, byte[] data = null)
        {
            using (var tracer = new ElasticsearchResponseTracer(this._ConnectionSettings.TraceEnabled))
            {
                var timeout = this._ConnectionSettings.Timeout;

                var state = new ConnectionState { Connection = request };

                if (data != null)
                {
                    var getRequestStream = Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null);
                    //ThreadPool.RegisterWaitForSingleObject((getRequestStream as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
                    yield return getRequestStream;

                    var requestStream = getRequestStream.Result;
                    try
                    {
                        var writeToRequestStream = Task.Factory.FromAsync(requestStream.BeginWrite, requestStream.EndWrite, data, 0, data.Length, state);
                        yield return writeToRequestStream;
                    }
                    finally
                    {
                        requestStream.Close();
                    }
                }

                // Get the response
                var getResponse = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
                //ThreadPool.RegisterWaitForSingleObject((getResponse as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
                yield return getResponse;

                var path = request.RequestUri.ToString();
                var method = request.Method;

                // Get the response stream
                using (var response = (HttpWebResponse)getResponse.Result)
                using (var responseStream = response.GetResponseStream())
                using (var memoryStream = new MemoryStream())
                {
                    // Copy all data from the response stream
                    var buffer = new byte[BUFFER_SIZE];
                    while (responseStream != null)
                    {
                        var read = Task<int>.Factory.FromAsync(responseStream.BeginRead, responseStream.EndRead, buffer, 0, BUFFER_SIZE, null);
                        yield return read;
                        if (read.Result == 0) break;
                        memoryStream.Write(buffer, 0, read.Result);
                    }
                    var cs = ElasticsearchResponse.Create(this._ConnectionSettings, (int) response.StatusCode, method, path, data, memoryStream.ToArray());
                    tcs.TrySetResult(cs);
                    tracer.SetResult(cs);
                    _ConnectionSettings.ConnectionStatusHandler(cs);
                }
            }
        }
 protected virtual ElasticsearchResponse DoSynchronousRequest(HttpWebRequest request, byte[] data = null)
 {
     var path = request.RequestUri.ToString();
     var method = request.Method;
     using (var tracer = new ElasticsearchResponseTracer(this._ConnectionSettings.TraceEnabled))
     {
         ElasticsearchResponse cs = null;
         if (data != null)
         {
             using (var r = request.GetRequestStream())
             {
                 r.Write(data, 0, data.Length);
             }
         }
         try
         {
             using (var response = (HttpWebResponse)request.GetResponse())
             using (var responseStream = response.GetResponseStream())
             using (var memoryStream = new MemoryStream())
             {
                 responseStream.CopyTo(memoryStream);
                 cs = ElasticsearchResponse.Create(this._ConnectionSettings, (int) response.StatusCode, method, path, data,
                     memoryStream.ToArray());
                 tracer.SetResult(cs);
                 return cs;
             }
         }
         catch (WebException webException)
         {
             cs = ElasticsearchResponse.CreateError(this._ConnectionSettings, webException, method, path, data);
             tracer.SetResult(cs);
             _ConnectionSettings.ConnectionStatusHandler(cs);
             return cs;
         }
     }
 }
        protected virtual Task<ElasticsearchResponse> DoAsyncRequest(HttpWebRequest request, byte[] data = null)
        {
            var tcs = new TaskCompletionSource<ElasticsearchResponse>();
            if (this._ConnectionSettings.MaximumAsyncConnections <= 0
              || this._ResourceLock == null)
                return this.CreateIterateTask(request, data, tcs);

            var timeout = this._ConnectionSettings.Timeout;
            var path = request.RequestUri.ToString();
            var method = request.Method;
            if (!this._ResourceLock.WaitOne(timeout))
            {
                using (var tracer = new ElasticsearchResponseTracer(this._ConnectionSettings.TraceEnabled))
                {
                    var m = "Could not start the operation before the timeout of " + timeout +
                      "ms completed while waiting for the semaphore";
                    var cs = ElasticsearchResponse.CreateError(this._ConnectionSettings, new TimeoutException(m), method, path, data);
                    tcs.SetResult(cs);
                    tracer.SetResult(cs);
                    _ConnectionSettings.ConnectionStatusHandler(cs);
                    return tcs.Task;
                }
            }
            try
            {
                return this.CreateIterateTask(request, data, tcs);
            }
            finally
            {
                this._ResourceLock.Release();
            }
        }