简体   繁体   中英

NEST async call

According to this link , NEST 2.0 internals just moved to a fully fledged async/await implementation.

Does this mean that NEST 2.0 internally works in a complete asynchronous fashion?

If not, shall we use async when calling the NEST API?

The internals for asynchronous calls have been rewritten from using a Task Parallel Library (TPL) based approach to using async/await. This made it easier to simplify the approach to exception and error handling, although both the old TPL and new async/await approaches were both asynchronous (as far as async methods are exposed).

Let's take GetAsync<T>() as an example. The pipeline of calls are:

  1. IElasticClient.LowLevelDispatch.GetDispatchAsync<GetResponse<T>>()
  2. IElasticLowLevelClient.GetAsync<T>() with route values extracted from the previous call
  3. IElasticLowLevelClient.DoRequestAsync<T>() , a general request dispatching method which calls the ITransport 's request async method
  4. ITransport.RequestAsync<T>() , which for the default Transport<TConnectionSettings> will:

    1. create an IRequestPipeline using the IRequestPipelineFactory . The default is RequestPipeline
    2. RequestPipeline.SniffAsync() on first pool usage if the IConnectionPool supports sniffing. A WaitAsync() is performed on a SemaphoreSlim here to block whilst the first sniff happens.
    3. A node is selected from the cluster with the following calls applied:

      1. RequestPipeline.SniffOnStaleClusterAsync() in the event the cluster has been marked as stale previously
      2. RequestPipeline.PingAsync() to ensure the node can be pinged
      3. make the call to Elasticsearch with RequestPipline.CallElasticsearchAsync<TReturn>() which will use the IConnection passed to ConnectionSettings when creating an ElasticClient to make the request using IConnection.RequestAsync<TReturn>() . The default IConnection in .NET 4.5 + (ie full fat CLR) is HttpConnection . Internally, HttpConnection uses HttpWebRequest to make the actual request:

        1. get the request stream with HttpWebRequest.GetRequestStreamAsync()
        2. write data to the request stream if necessary using PostData<T>.WriteAsync()
        3. make the HTTP request with HttpWebRequest.GetResponseAsync()
        4. build a meaningful response from the response stream using a response builder, ResponseBuilder<TReturn>.ToResponseAsync() . Inside of here, the response will be deserialized to TReturn ; for most responses that are json, this will use IElasticsearchSerializer.DerserializeAsync<TReturn>() to deserialize the response. For the default json serializer that uses Json.NET, there is no asynchronous deserialization method so the async version simply wraps the synchronous deserialization call.

That's a brief summary of what happens, hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM