Skip to content

Commit ebcd9d6

Browse files
committed
ConfigureAwait(false) all async methods
The context does not need to be captured and should slightly improve performance Closes #1801
1 parent 194d5ac commit ebcd9d6

File tree

13 files changed

+38
-38
lines changed

13 files changed

+38
-38
lines changed

src/Elasticsearch.Net/Connection/HttpConnection-CoreFx.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,18 @@ public virtual async Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(
103103
try
104104
{
105105
var requestMessage = CreateHttpRequestMessage(requestData);
106-
var response = await client.SendAsync(requestMessage, requestData.CancellationToken);
106+
var response = await client.SendAsync(requestMessage, requestData.CancellationToken).ConfigureAwait(false);
107107
builder.StatusCode = (int)response.StatusCode;
108108

109109
if (response.Content != null)
110-
builder.Stream = await response.Content.ReadAsStreamAsync();
110+
builder.Stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
111111
}
112112
catch (HttpRequestException e)
113113
{
114114
HandleException(builder, e);
115115
}
116116

117-
return await builder.ToResponseAsync();
117+
return await builder.ToResponseAsync().ConfigureAwait(false);
118118
}
119119

120120
private void HandleException<TReturn>(ResponseBuilder<TReturn> builder, HttpRequestException exception)

src/Elasticsearch.Net/Connection/HttpConnection.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,21 @@ public virtual async Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(
157157

158158
if (data != null)
159159
{
160-
using (var stream = await request.GetRequestStreamAsync())
160+
using (var stream = await request.GetRequestStreamAsync().ConfigureAwait(false))
161161
{
162162
if (requestData.HttpCompression)
163163
using (var zipStream = new GZipStream(stream, CompressionMode.Compress))
164-
await data.WriteAsync(zipStream, requestData.ConnectionSettings);
164+
await data.WriteAsync(zipStream, requestData.ConnectionSettings).ConfigureAwait(false);
165165
else
166-
await data.WriteAsync(stream, requestData.ConnectionSettings);
166+
await data.WriteAsync(stream, requestData.ConnectionSettings).ConfigureAwait(false);
167167
}
168168
}
169169

170170
//http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx
171171
//Either the stream or the response object needs to be closed but not both although it won't
172172
//throw any errors if both are closed atleast one of them has to be Closed.
173173
//Since we expose the stream we let closing the stream determining when to close the connection
174-
var response = (HttpWebResponse)(await request.GetResponseAsync());
174+
var response = (HttpWebResponse)(await request.GetResponseAsync().ConfigureAwait(false));
175175
builder.StatusCode = (int)response.StatusCode;
176176
builder.Stream = response.GetResponseStream();
177177
}
@@ -180,7 +180,7 @@ public virtual async Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(
180180
HandleException(builder, e);
181181
}
182182

183-
return await builder.ToResponseAsync();
183+
return await builder.ToResponseAsync().ConfigureAwait(false);
184184
}
185185

186186
private void HandleException<TReturn>(ResponseBuilder<TReturn> builder, WebException exception)

src/Elasticsearch.Net/Serialization/ElasticsearchDefaultSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public T Deserialize<T>(Stream stream)
3434
using (var ms = new MemoryStream())
3535
using (stream)
3636
{
37-
await stream.CopyToAsync(ms, 8096, cancellationToken);
37+
await stream.CopyToAsync(ms, 8096, cancellationToken).ConfigureAwait(false);
3838
var buffer = ms.ToArray();
3939
if (buffer.Length <= 1)
4040
return default(T);

src/Elasticsearch.Net/Transport/Pipeline/RequestPipeline.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public void FirstPoolUsage(SemaphoreSlim semaphore)
144144
public async Task FirstPoolUsageAsync(SemaphoreSlim semaphore)
145145
{
146146
if (!this.FirstPoolUsageNeedsSniffing) return;
147-
var success = await semaphore.WaitAsync(this._settings.RequestTimeout, this._cancellationToken);
147+
var success = await semaphore.WaitAsync(this._settings.RequestTimeout, this._cancellationToken).ConfigureAwait(false);
148148
if (!success)
149149
throw new PipelineException(PipelineFailure.CouldNotStartSniffOnStartup, (Exception)null);
150150

@@ -153,7 +153,7 @@ public async Task FirstPoolUsageAsync(SemaphoreSlim semaphore)
153153
{
154154
using (this.Audit(SniffOnStartup))
155155
{
156-
await this.SniffAsync();
156+
await this.SniffAsync().ConfigureAwait(false);
157157
this._connectionPool.SniffedOnStartup = true;
158158
}
159159
}
@@ -178,7 +178,7 @@ public async Task SniffOnStaleClusterAsync()
178178
if (!StaleClusterState) return;
179179
using (this.Audit(AuditEvent.SniffOnStaleCluster))
180180
{
181-
await this.SniffAsync();
181+
await this.SniffAsync().ConfigureAwait(false);
182182
this._connectionPool.SniffedOnStartup = true;
183183
}
184184
}
@@ -263,7 +263,7 @@ public async Task PingAsync(Node node)
263263
try
264264
{
265265
var pingData = CreatePingRequestData(node, audit);
266-
var response = await this._connection.RequestAsync<VoidResponse>(pingData);
266+
var response = await this._connection.RequestAsync<VoidResponse>(pingData).ConfigureAwait(false);
267267
ThrowBadAuthPipelineExceptionWhenNeeded(response);
268268
//ping should not silently accept bad but valid http responses
269269
if (!response.Success) throw new PipelineException(PipelineFailure.BadResponse);
@@ -301,7 +301,7 @@ public async Task SniffOnConnectionFailureAsync()
301301
{
302302
if (!this.SniffsOnConnectionFailure) return;
303303
using (this.Audit(SniffOnFail))
304-
await this.SniffAsync();
304+
await this.SniffAsync().ConfigureAwait(false);
305305
}
306306

307307
public void Sniff()
@@ -349,7 +349,7 @@ public async Task SniffAsync()
349349
try
350350
{
351351
var requestData = new RequestData(HttpMethod.GET, path, null, this._settings, this._memoryStreamFactory) { Node = node };
352-
var response = await this._connection.RequestAsync<SniffResponse>(requestData);
352+
var response = await this._connection.RequestAsync<SniffResponse>(requestData).ConfigureAwait(false);
353353
ThrowBadAuthPipelineExceptionWhenNeeded(response);
354354
//sniff should not silently accept bad but valid http responses
355355
if (!response.Success) throw new PipelineException(PipelineFailure.BadResponse);
@@ -406,7 +406,7 @@ public async Task<ElasticsearchResponse<TReturn>> CallElasticsearchAsync<TReturn
406406
ElasticsearchResponse<TReturn> response = null;
407407
try
408408
{
409-
response = await this._connection.RequestAsync<TReturn>(requestData);
409+
response = await this._connection.RequestAsync<TReturn>(requestData).ConfigureAwait(false);
410410
response.AuditTrail = this.AuditTrail;
411411
ThrowBadAuthPipelineExceptionWhenNeeded(response);
412412
if (!response.Success) audit.Event = AuditEvent.BadResponse;

src/Elasticsearch.Net/Transport/Pipeline/ResponseBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task<ElasticsearchResponse<TReturn>> ToResponseAsync()
3636
{
3737
var response = Initialize(this.StatusCode, this.Exception);
3838
if (this.Stream != null)
39-
await SetBodyAsync(response, this.Stream);
39+
await SetBodyAsync(response, this.Stream).ConfigureAwait(false);
4040
Finalize(response);
4141
return response;
4242
}
@@ -87,19 +87,19 @@ private async Task SetBodyAsync(ElasticsearchResponse<TReturn> response, Stream
8787
if (NeedsToEagerReadStream())
8888
{
8989
var inMemoryStream = this._requestData.MemoryStreamFactory.Create();
90-
await stream.CopyToAsync(inMemoryStream, BufferSize, this._requestData.CancellationToken);
90+
await stream.CopyToAsync(inMemoryStream, BufferSize, this._requestData.CancellationToken).ConfigureAwait(false);
9191
bytes = this.SwapStreams(ref stream, ref inMemoryStream);
9292
}
9393

9494
if (!SetSpecialTypes(stream, response, bytes))
9595
{
9696
if (this._requestData.CustomConverter != null) response.Body = this._requestData.CustomConverter(response, stream) as TReturn;
97-
else response.Body = await this._requestData.ConnectionSettings.Serializer.DeserializeAsync<TReturn>(stream, this._requestData.CancellationToken);
97+
else response.Body = await this._requestData.ConnectionSettings.Serializer.DeserializeAsync<TReturn>(stream, this._requestData.CancellationToken).ConfigureAwait(false);
9898
}
9999
}
100100
else if (response.HttpStatusCode != null)
101101
{
102-
response.ServerError = await ServerError.TryCreateAsync(stream, this._requestData.CancellationToken);
102+
response.ServerError = await ServerError.TryCreateAsync(stream, this._requestData.CancellationToken).ConfigureAwait(false);
103103
}
104104
}
105105

src/Elasticsearch.Net/Transport/PostData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void Write(Stream writableStream, IConnectionConfigurationValues settings
136136
foreach (var o in _enumerableOfObject)
137137
{
138138
settings.Serializer.Serialize(o, stream, SerializationFormatting.None);
139-
await stream.WriteAsync(new byte[] { (byte)'\n' }, 0, 1, cancellationToken);
139+
await stream.WriteAsync(new byte[] { (byte)'\n' }, 0, 1, cancellationToken).ConfigureAwait(false);
140140
}
141141
break;
142142
case PostType.Serializable:
@@ -152,7 +152,7 @@ public void Write(Stream writableStream, IConnectionConfigurationValues settings
152152
if (ms != null)
153153
{
154154
ms.Position = 0;
155-
await ms.CopyToAsync(writableStream, 8096, cancellationToken);
155+
await ms.CopyToAsync(writableStream, 8096, cancellationToken).ConfigureAwait(false);
156156
}
157157
if (this.Type != 0)
158158
this.WrittenBytes = ms?.ToArray();

src/Elasticsearch.Net/Transport/Transport.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public ElasticsearchResponse<TReturn> Request<TReturn>(HttpMethod method, string
6464
try
6565
{
6666
pipeline.SniffOnStaleCluster();
67-
Ping(pipeline, node, seenExceptions);
67+
Ping(pipeline, node);
6868
response = pipeline.CallElasticsearch<TReturn>(requestData);
6969
if (!response.SuccessOrKnownError)
7070
{
@@ -112,7 +112,7 @@ public async Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(HttpMeth
112112
{
113113
using (var pipeline = this.PipelineProvider.Create(this.Settings, this.DateTimeProvider, this.MemoryStreamFactory, requestParameters))
114114
{
115-
await pipeline.FirstPoolUsageAsync(this.Settings.BootstrapLock);
115+
await pipeline.FirstPoolUsageAsync(this.Settings.BootstrapLock).ConfigureAwait(false);
116116

117117
var requestData = new RequestData(method, path, data, this.Settings, requestParameters, this.MemoryStreamFactory);
118118
ElasticsearchResponse<TReturn> response = null;
@@ -123,13 +123,13 @@ public async Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(HttpMeth
123123
requestData.Node = node;
124124
try
125125
{
126-
await pipeline.SniffOnStaleClusterAsync();
127-
await PingAsync(pipeline, node, seenExceptions);
128-
response = await pipeline.CallElasticsearchAsync<TReturn>(requestData);
126+
await pipeline.SniffOnStaleClusterAsync().ConfigureAwait(false);
127+
await PingAsync(pipeline, node).ConfigureAwait(false);
128+
response = await pipeline.CallElasticsearchAsync<TReturn>(requestData).ConfigureAwait(false);
129129
if (!response.SuccessOrKnownError)
130130
{
131131
pipeline.MarkDead(node);
132-
await pipeline.SniffOnConnectionFailureAsync();
132+
await pipeline.SniffOnConnectionFailureAsync().ConfigureAwait(false);
133133
}
134134
}
135135
catch (PipelineException pipelineException) when (!pipelineException.Recoverable)
@@ -167,7 +167,7 @@ public async Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(HttpMeth
167167
}
168168
}
169169

170-
private static void Ping(IRequestPipeline pipeline, Node node, List<PipelineException> seenExceptions)
170+
private static void Ping(IRequestPipeline pipeline, Node node)
171171
{
172172
try
173173
{
@@ -180,15 +180,15 @@ private static void Ping(IRequestPipeline pipeline, Node node, List<PipelineExce
180180
}
181181
}
182182

183-
private static async Task PingAsync(IRequestPipeline pipeline, Node node, List<PipelineException> seenExceptions)
183+
private static async Task PingAsync(IRequestPipeline pipeline, Node node)
184184
{
185185
try
186186
{
187-
await pipeline.PingAsync(node);
187+
await pipeline.PingAsync(node).ConfigureAwait(false);
188188
}
189189
catch (PipelineException e) when (e.Recoverable)
190190
{
191-
await pipeline.SniffOnConnectionFailureAsync();
191+
await pipeline.SniffOnConnectionFailureAsync().ConfigureAwait(false);
192192
e.RethrowKeepingStackTrace();
193193
}
194194
}

src/Nest/Document/Multiple/MultiGet/ElasticClient-SourceMany.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static IEnumerable<T> SourceMany<T>(this IElasticClient client, IEnumerab
7878
public static async Task<IEnumerable<T>> SourceManyAsync<T>(this IElasticClient client, IEnumerable<string> ids, string index = null, string type = null)
7979
where T : class
8080
{
81-
var response = await client.MultiGetAsync(s => s.GetMany<T>(ids, (gs, i) => gs.Index(index).Type(type)));
81+
var response = await client.MultiGetAsync(s => s.GetMany<T>(ids, (gs, i) => gs.Index(index).Type(type))).ConfigureAwait(false);
8282
return response.SourceMany<T>(ids);
8383
}
8484

src/Nest/Document/Multiple/MultiGet/EllasticClient-GetMany.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static IEnumerable<IMultiGetHit<T>> GetMany<T>(this IElasticClient client
5959
public static async Task<IEnumerable<IMultiGetHit<T>>> GetManyAsync<T>(this IElasticClient client, IEnumerable<string> ids, string index = null, string type = null)
6060
where T : class
6161
{
62-
var response = await client.MultiGetAsync(s => s.GetMany<T>(ids, (gs, i) => gs.Index(index).Type(type)));
62+
var response = await client.MultiGetAsync(s => s.GetMany<T>(ids, (gs, i) => gs.Index(index).Type(type))).ConfigureAwait(false);
6363
return response.GetMany<T>(ids);
6464
}
6565

src/Nest/Document/Single/Source/ElasticClient-Source.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Task<T> SourceAsync<T>(DocumentPath<T> document, Func<SourceDescriptor<T>
4848
public async Task<T> SourceAsync<T>(ISourceRequest request) where T : class
4949
{
5050
request.RouteValues.Resolve(ConnectionSettings);
51-
var response = await this.LowLevelDispatch.GetSourceDispatchAsync<T>(request);
51+
var response = await this.LowLevelDispatch.GetSourceDispatchAsync<T>(request).ConfigureAwait(false);
5252
return response.Body;
5353
}
5454

0 commit comments

Comments
 (0)