Skip to content

Commit

Permalink
Add ConfigureAwait(false)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Sharon authored and kapetan committed Oct 4, 2020
1 parent 49df1bc commit 8137012
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion DNS/Client/ClientRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override string ToString() {
/// <returns>The response received from server</returns>
public async Task<IResponse> Resolve(CancellationToken cancellationToken = default(CancellationToken)) {
try {
IResponse response = await resolver.Resolve(this, cancellationToken);
IResponse response = await resolver.Resolve(this, cancellationToken).ConfigureAwait(false);

if (response.Id != this.Id) {
throw new ResponseException(response, "Mismatching request/response IDs");
Expand Down
4 changes: 2 additions & 2 deletions DNS/Client/DnsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ClientRequest Create(IRequest request = null) {
throw new ArgumentException("Invalid record type " + type);
}

IResponse response = await Resolve(domain, type, cancellationToken);
IResponse response = await Resolve(domain, type, cancellationToken).ConfigureAwait(false);
IList<IPAddress> ips = response.AnswerRecords
.Where(r => r.Type == type)
.Cast<IPAddressResourceRecord>()
Expand All @@ -61,7 +61,7 @@ public ClientRequest Create(IRequest request = null) {
}

public async Task<string> Reverse(IPAddress ip, CancellationToken cancellationToken = default(CancellationToken)) {
IResponse response = await Resolve(Domain.PointerName(ip), RecordType.PTR, cancellationToken);
IResponse response = await Resolve(Domain.PointerName(ip), RecordType.PTR, cancellationToken).ConfigureAwait(false);
IResourceRecord ptr = response.AnswerRecords.FirstOrDefault(r => r.Type == RecordType.PTR);

if (ptr == null) {
Expand Down
12 changes: 6 additions & 6 deletions DNS/Client/RequestResolver/TcpRequestResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public TcpRequestResolver(IPEndPoint dns) {

public async Task<IResponse> Resolve(IRequest request, CancellationToken cancellationToken = default(CancellationToken)) {
using(TcpClient tcp = new TcpClient(dns.AddressFamily)) {
await tcp.ConnectAsync(dns.Address, dns.Port);
await tcp.ConnectAsync(dns.Address, dns.Port).ConfigureAwait(false);

Stream stream = tcp.GetStream();
byte[] buffer = request.ToArray();
Expand All @@ -26,18 +26,18 @@ public TcpRequestResolver(IPEndPoint dns) {
Array.Reverse(length);
}

await stream.WriteAsync(length, 0, length.Length, cancellationToken);
await stream.WriteAsync(buffer, 0, buffer.Length, cancellationToken);
await stream.WriteAsync(length, 0, length.Length, cancellationToken).ConfigureAwait(false);
await stream.WriteAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);

buffer = new byte[2];
await Read(stream, buffer, cancellationToken);
await Read(stream, buffer, cancellationToken).ConfigureAwait(false);

if (BitConverter.IsLittleEndian) {
Array.Reverse(buffer);
}

buffer = new byte[BitConverter.ToUInt16(buffer, 0)];
await Read(stream, buffer, cancellationToken);
await Read(stream, buffer, cancellationToken).ConfigureAwait(false);

IResponse response = Response.FromArray(buffer);
return new ClientResponse(request, response, buffer);
Expand All @@ -49,7 +49,7 @@ private static async Task Read(Stream stream, byte[] buffer, CancellationToken c
int offset = 0;
int size = 0;

while (length > 0 && (size = await stream.ReadAsync(buffer, offset, length, cancellationToken)) > 0) {
while (length > 0 && (size = await stream.ReadAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false)) > 0) {
offset += size;
length -= size;
}
Expand Down
6 changes: 3 additions & 3 deletions DNS/Client/RequestResolver/UdpRequestResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ public UdpRequestResolver(IPEndPoint dns, int timeout = 5000) {
using(UdpClient udp = new UdpClient(dns.AddressFamily)) {
await udp
.SendAsync(request.ToArray(), request.Size, dns)
.WithCancellationTimeout(TimeSpan.FromMilliseconds(timeout), cancellationToken);
.WithCancellationTimeout(TimeSpan.FromMilliseconds(timeout), cancellationToken).ConfigureAwait(false);

UdpReceiveResult result = await udp
.ReceiveAsync()
.WithCancellationTimeout(TimeSpan.FromMilliseconds(timeout), cancellationToken);
.WithCancellationTimeout(TimeSpan.FromMilliseconds(timeout), cancellationToken).ConfigureAwait(false);

if(!result.RemoteEndPoint.Equals(dns)) throw new IOException("Remote endpoint mismatch");
byte[] buffer = result.Buffer;
Response response = Response.FromArray(buffer);

if (response.Truncated) {
return await fallback.Resolve(request, cancellationToken);
return await fallback.Resolve(request, cancellationToken).ConfigureAwait(false);
}

return new ClientResponse(request, response, buffer);
Expand Down
6 changes: 3 additions & 3 deletions DNS/Protocol/Utils/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ public static async Task<T> WithCancellation<T>(this Task<T> task, CancellationT
}, tcs);

using (registration) {
if (await Task.WhenAny(task, tcs.Task) != task) {
if (await Task.WhenAny(task, tcs.Task).ConfigureAwait(false) != task) {
throw new OperationCanceledException(token);
}
}

return await task;
return await task.ConfigureAwait(false);
}

public static async Task<T> WithCancellationTimeout<T>(this Task<T> task, TimeSpan timeout, CancellationToken cancellationToken = default(CancellationToken)) {
using (CancellationTokenSource timeoutSource = new CancellationTokenSource(timeout))
using (CancellationTokenSource linkSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutSource.Token, cancellationToken)) {
return await task.WithCancellation(linkSource.Token);
return await task.WithCancellation(linkSource.Token).ConfigureAwait(false);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions DNS/Server/DnsServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void ReceiveCallback(IAsyncResult result) {

udp.BeginReceive(ReceiveCallback, null);
OnEvent(Listening, EventArgs.Empty);
await tcs.Task;
await tcs.Task.ConfigureAwait(false);
}

public void Dispose() {
Expand Down Expand Up @@ -125,12 +125,12 @@ private async void HandleRequest(byte[] data, IPEndPoint remote) {
request = Request.FromArray(data);
OnEvent(Requested, new RequestedEventArgs(request, data, remote));

IResponse response = await resolver.Resolve(request);
IResponse response = await resolver.Resolve(request).ConfigureAwait(false);

OnEvent(Responded, new RespondedEventArgs(request, response, data, remote));
await udp
.SendAsync(response.ToArray(), response.Size, remote)
.WithCancellationTimeout(TimeSpan.FromMilliseconds(UDP_TIMEOUT));
.WithCancellationTimeout(TimeSpan.FromMilliseconds(UDP_TIMEOUT)).ConfigureAwait(false);
}
catch (SocketException e) { OnError(e); }
catch (ArgumentException e) { OnError(e); }
Expand All @@ -148,7 +148,7 @@ await udp
try {
await udp
.SendAsync(response.ToArray(), response.Size, remote)
.WithCancellationTimeout(TimeSpan.FromMilliseconds(UDP_TIMEOUT));
.WithCancellationTimeout(TimeSpan.FromMilliseconds(UDP_TIMEOUT)).ConfigureAwait(false);
}
catch (SocketException) {}
catch (OperationCanceledException) {}
Expand Down Expand Up @@ -201,7 +201,7 @@ public FallbackRequestResolver(params IRequestResolver[] resolvers) {
IResponse response = null;

foreach (IRequestResolver resolver in resolvers) {
response = await resolver.Resolve(request, cancellationToken);
response = await resolver.Resolve(request, cancellationToken).ConfigureAwait(false);
if (response.AnswerRecords.Count > 0) break;
}

Expand Down
2 changes: 1 addition & 1 deletion Examples/Client/ClientExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async static Task MainAsync(string[] args) {
DnsClient client = new DnsClient("8.8.8.8");

foreach (string domain in args) {
IList<IPAddress> ips = await client.Lookup(domain);
IList<IPAddress> ips = await client.Lookup(domain).ConfigureAwait(false);
Console.WriteLine("{0} => {1}", domain, string.Join(", ", ips));
}
}
Expand Down
6 changes: 3 additions & 3 deletions Examples/ClientServer/ClientServerExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public async static Task MainAsync() {
server.Listening += async (sender, e) => {
DnsClient client = new DnsClient("127.0.0.1", PORT);

await client.Lookup("google.com");
await client.Lookup("cnn.com");
await client.Lookup("google.com").ConfigureAwait(false);
await client.Lookup("cnn.com").ConfigureAwait(false);

server.Dispose();
};

await server.Listen(PORT);
await server.Listen(PORT).ConfigureAwait(false);
}
}
}
2 changes: 1 addition & 1 deletion Examples/Server/ServerExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async static Task MainAsync(string[] args) {
if(responseError != null) Console.WriteLine(responseError.Response);
};

await server.Listen();
await server.Listen().ConfigureAwait(false);
}
}
}
6 changes: 3 additions & 3 deletions Tests/Client/DnsClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class DnsClientTest {
[Fact]
public async Task ClientLookup() {
DnsClient client = new DnsClient(new IPAddressRequestResolver());
IList<IPAddress> ips = await client.Lookup("google.com");
IList<IPAddress> ips = await client.Lookup("google.com").ConfigureAwait(false);

Assert.Equal(1, ips.Count);
Assert.Equal("192.168.0.1", ips[0].ToString());
Expand All @@ -24,7 +24,7 @@ public async Task ClientLookup() {
[Fact]
public async Task ClientReverse() {
DnsClient client = new DnsClient(new PointerRequestResolver());
string domain = await client.Reverse("192.168.0.1");
string domain = await client.Reverse("192.168.0.1").ConfigureAwait(false);

Assert.Equal("google.com", domain);
}
Expand All @@ -35,7 +35,7 @@ public async Task ClientNameError() {

await Assert.ThrowsAsync<ResponseException>(() => {
return client.Lookup("google.com");
});
}).ConfigureAwait(false);
}

private class IPAddressRequestResolver : IRequestResolver {
Expand Down
12 changes: 6 additions & 6 deletions Tests/Server/DnsServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ await Create(new IPAddressRequestResolver(), async server => {
clientRequest.Questions.Add(clientRequestQuestion);
clientRequest.OperationCode = OperationCode.Query;

IResponse clientResponse = await Resolve(clientRequest);
IResponse clientResponse = await Resolve(clientRequest).ConfigureAwait(false);

Assert.Equal(1, clientResponse.Id);
Assert.Equal(1, clientResponse.Questions.Count);
Expand Down Expand Up @@ -93,7 +93,7 @@ await Create(new IPAddressRequestResolver(), async server => {
Assert.Equal(RecordType.A, respondedResponseRecord.Type);

Assert.Null(erroredEvent);
});
}).ConfigureAwait(false);
}

private async static Task Create(IRequestResolver requestResolver, Func<DnsServer, Task> action) {
Expand All @@ -102,7 +102,7 @@ private async static Task Create(IRequestResolver requestResolver, Func<DnsServe

server.Listening += async (sender, _) => {
try {
await action(server);
await action(server).ConfigureAwait(false);
tcs.SetResult(null);
} catch(Exception e) {
tcs.SetException(e);
Expand All @@ -111,15 +111,15 @@ private async static Task Create(IRequestResolver requestResolver, Func<DnsServe
}
};

await Task.WhenAll(server.Listen(PORT), tcs.Task);
await Task.WhenAll(server.Listen(PORT), tcs.Task).ConfigureAwait(false);
}

private async static Task<IResponse> Resolve(IRequest request) {
using (UdpClient udp = new UdpClient()) {
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), PORT);

await udp.SendAsync(request.ToArray(), request.Size, endPoint);
UdpReceiveResult result = await udp.ReceiveAsync();
await udp.SendAsync(request.ToArray(), request.Size, endPoint).ConfigureAwait(false);
UdpReceiveResult result = await udp.ReceiveAsync().ConfigureAwait(false);
return Response.FromArray(result.Buffer);
}
}
Expand Down

0 comments on commit 8137012

Please sign in to comment.