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?
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 the ITransport
's request async methodITransport.RequestAsync<T>()
, which for the default Transport<TConnectionSettings>
will:
IRequestPipeline
using the IRequestPipelineFactory
. The default is RequestPipeline
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.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 the IConnection
passed to ConnectionSettings
when creating an ElasticClient to make the request using IConnection.RequestAsync<TReturn>()
. The default IConnection
in .NET 4.5 + (i.e. full fat CLR) is HttpConnection
. Internally, HttpConnection
uses HttpWebRequest
to make the actual request:
HttpWebRequest.GetRequestStreamAsync()
PostData<T>.WriteAsync()
HttpWebRequest.GetResponseAsync()
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 :)