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?
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?
Copyright © 2021 Jogjafile Inc.
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:IElasticClient.LowLevelDispatch.GetDispatchAsync<GetResponse<T>>()IElasticLowLevelClient.GetAsync<T>()with route values extracted from the previous callIElasticLowLevelClient.DoRequestAsync<T>(), a general request dispatching method which calls theITransport's request async methodITransport.RequestAsync<T>(), which for the defaultTransport<TConnectionSettings>will:IRequestPipelineusing theIRequestPipelineFactory. The default isRequestPipelineRequestPipeline.SniffAsync()on first pool usage if theIConnectionPoolsupports sniffing. AWaitAsync()is performed on aSemaphoreSlimhere to block whilst the first sniff happens.A node is selected from the cluster with the following calls applied:
RequestPipeline.SniffOnStaleClusterAsync()in the event the cluster has been marked as stale previouslyRequestPipeline.PingAsync()to ensure the node can be pingedmake the call to Elasticsearch with
RequestPipline.CallElasticsearchAsync<TReturn>()which will use theIConnectionpassed toConnectionSettingswhen creating an ElasticClient to make the request usingIConnection.RequestAsync<TReturn>(). The defaultIConnectionin .NET 4.5 + (i.e. full fat CLR) isHttpConnection. Internally,HttpConnectionusesHttpWebRequestto make the actual request:HttpWebRequest.GetRequestStreamAsync()PostData<T>.WriteAsync()HttpWebRequest.GetResponseAsync()ResponseBuilder<TReturn>.ToResponseAsync(). Inside of here, the response will be deserialized toTReturn; for most responses that are json, this will useIElasticsearchSerializer.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 :)