Skip to content

Commit 45bbfd5

Browse files
authored
Merge pull request #129 from OctopusDeploy/richev/async-dynamic-worker-timeouts-on-octopus-cloud
Async rework
2 parents 0d2ad63 + e41af5c commit 45bbfd5

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

source/Halibut/Transport/Protocol/MessageExchangeStream.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ void SendControlMessage(string message)
5757

5858
async Task SendControlMessageAsync(string message)
5959
{
60-
await streamWriter.WriteLineAsync(message);
61-
await streamWriter.FlushAsync();
60+
await streamWriter.WriteLineAsync(message).ConfigureAwait(false);
61+
await streamWriter.FlushAsync().ConfigureAwait(false);
6262
}
6363

6464
void SendIdentityMessage(string identityLine)
@@ -82,7 +82,7 @@ public void SendProceed()
8282

8383
public async Task SendProceedAsync()
8484
{
85-
await SendControlMessageAsync(Proceed);
85+
await SendControlMessageAsync(Proceed).ConfigureAwait(false);
8686
}
8787

8888
public void SendEnd()
@@ -109,7 +109,7 @@ public bool ExpectNextOrEnd()
109109

110110
public async Task<bool> ExpectNextOrEndAsync()
111111
{
112-
var line = await ReadLineAsync();
112+
var line = await ReadLineAsync().ConfigureAwait(false);
113113
switch (line)
114114
{
115115
case Next:
@@ -146,10 +146,10 @@ string ReadLine()
146146

147147
async Task<string> ReadLineAsync()
148148
{
149-
var line = await streamReader.ReadLineAsync();
149+
var line = await streamReader.ReadLineAsync().ConfigureAwait(false);
150150
while (line == string.Empty)
151151
{
152-
line = await streamReader.ReadLineAsync();
152+
line = await streamReader.ReadLineAsync().ConfigureAwait(false);
153153
}
154154

155155
return line;

source/Halibut/Transport/Protocol/WebSocketStream.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public override int Read(byte[] buffer, int offset, int count)
3838
{
3939
AssertCanReadOrWrite();
4040
var segment = new ArraySegment<byte>(buffer, offset, count);
41-
var recieveResult = context.ReceiveAsync(segment, CancellationToken.None)
41+
var receiveResult = context.ReceiveAsync(segment, CancellationToken.None)
4242
.ConfigureAwait(false).GetAwaiter().GetResult();
43-
return recieveResult.Count;
43+
return receiveResult.Count;
4444
}
4545

4646
public async Task<string> ReadTextMessage()
@@ -53,11 +53,11 @@ public async Task<string> ReadTextMessage()
5353
using(var cts = new CancellationTokenSource(HalibutLimits.TcpClientReceiveTimeout))
5454
using(var combined = CancellationTokenSource.CreateLinkedTokenSource(cts.Token, cancel.Token))
5555
{
56-
var result = await context.ReceiveAsync(buffer, combined.Token);
56+
var result = await context.ReceiveAsync(buffer, combined.Token).ConfigureAwait(false);
5757
if (result.MessageType == WebSocketMessageType.Close)
5858
{
5959
using(var sendCancel = new CancellationTokenSource(TimeSpan.FromSeconds(1)))
60-
await context.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Close received", sendCancel.Token);
60+
await context.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Close received", sendCancel.Token).ConfigureAwait(false);
6161
return null;
6262
}
6363

source/Halibut/Transport/SecureListener.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,15 @@ void WaitForPendingConnectionOrCancellation()
143143
}
144144

145145
var client = listener.AcceptTcpClient();
146-
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
147-
HandleClient(client);
148-
#pragma warning restore CS4014
146+
Task.Run(async () => await HandleClient(client).ConfigureAwait(false)).ConfigureAwait(false);
149147
numberOfFailedAttemptsInRow = 0;
150148
}
151149
catch (SocketException e) when (e.SocketErrorCode == SocketError.Interrupted)
152150
{
153151
}
154152
catch (ObjectDisposedException)
155153
{
154+
// Happens on shutdown
156155
}
157156
catch (Exception ex)
158157
{
@@ -190,7 +189,7 @@ async Task HandleClient(TcpClient client)
190189
client.ReceiveTimeout = (int)HalibutLimits.TcpClientReceiveTimeout.TotalMilliseconds;
191190

192191
log.Write(EventType.ListenerAcceptedClient, "Accepted TCP client: {0}", client.Client.RemoteEndPoint);
193-
await ExecuteRequest(client);
192+
await ExecuteRequest(client).ConfigureAwait(false);
194193
}
195194
catch (ObjectDisposedException)
196195
{
@@ -210,7 +209,7 @@ async Task ExecuteRequest(TcpClient client)
210209
try
211210
{
212211
log.Write(EventType.SecurityNegotiation, "Performing TLS server handshake");
213-
await ssl.AuthenticateAsServerAsync(serverCertificate, true, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, false);
212+
await ssl.AuthenticateAsServerAsync(serverCertificate, true, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, false).ConfigureAwait(false);
214213

215214
log.Write(EventType.SecurityNegotiation, "Secure connection established, client is not yet authenticated, client connected with {0}", ssl.SslProtocol.ToString());
216215

@@ -247,7 +246,7 @@ async Task ExecuteRequest(TcpClient client)
247246
});
248247

249248
tcpClientManager.AddActiveClient(thumbprint, client);
250-
await ExchangeMessages(ssl);
249+
await ExchangeMessages(ssl).ConfigureAwait(false);
251250
}
252251
}
253252
catch (AuthenticationException ex)

source/Halibut/Transport/SecureWebSocketListener.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace Halibut.Transport
1414
public class SecureWebSocketListener : IDisposable
1515
{
1616
readonly string endPoint;
17-
readonly X509Certificate2 serverCertificate;
1817
readonly Func<MessageExchangeProtocol, Task> protocolHandler;
1918
readonly Predicate<string> verifyClientThumbprint;
2019
readonly Func<string, string, UnauthorizedClientConnectResponse> unauthorizedClientConnect;
@@ -53,7 +52,6 @@ public SecureWebSocketListener(string endPoint, X509Certificate2 serverCertifica
5352
endPoint += "/";
5453

5554
this.endPoint = endPoint;
56-
this.serverCertificate = serverCertificate;
5755
this.protocolHandler = protocolHandler;
5856
this.verifyClientThumbprint = verifyClientThumbprint;
5957
this.unauthorizedClientConnect = unauthorizedClientConnect;
@@ -75,7 +73,7 @@ public void Start()
7573

7674
log = logFactory.ForPrefix(endPoint);
7775
log.Write(EventType.ListenerStarted, "Listener started");
78-
Task.Run(async () => await Accept());
76+
Task.Run(async () => await Accept().ConfigureAwait(false)).ConfigureAwait(false);
7977
}
8078

8179
async Task Accept()
@@ -86,12 +84,12 @@ async Task Accept()
8684
{
8785
try
8886
{
89-
var context = await listener.GetContextAsync();
87+
var context = await listener.GetContextAsync().ConfigureAwait(false);
9088

9189
if (context.Request.IsWebSocketRequest)
9290
{
9391
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
94-
HandleClient(context);
92+
Task.Run(async () => await HandleClient(context).ConfigureAwait(false)).ConfigureAwait(false);
9593
#pragma warning restore CS4014
9694
}
9795
else
@@ -115,7 +113,7 @@ async Task HandleClient(HttpListenerContext context)
115113
try
116114
{
117115
log.Write(EventType.ListenerAcceptedClient, "Accepted Web Socket client: {0}", context.Request.RemoteEndPoint);
118-
await ExecuteRequest(context);
116+
await ExecuteRequest(context).ConfigureAwait(false);
119117
}
120118
catch (ObjectDisposedException)
121119
{
@@ -136,10 +134,10 @@ async Task ExecuteRequest(HttpListenerContext listenerContext)
136134
WebSocketStream webSocketStream = null;
137135
try
138136
{
139-
var webSocketContext = await listenerContext.AcceptWebSocketAsync("Octopus");
137+
var webSocketContext = await listenerContext.AcceptWebSocketAsync("Octopus").ConfigureAwait(false);
140138
webSocketStream = new WebSocketStream(webSocketContext.WebSocket);
141139

142-
var req = await webSocketStream.ReadTextMessage(); // Initial message
140+
var req = await webSocketStream.ReadTextMessage().ConfigureAwait(false); // Initial message
143141
if (string.IsNullOrEmpty(req))
144142
{
145143
log.Write(EventType.Diagnostic, "Ignoring empty request");
@@ -152,10 +150,12 @@ async Task ExecuteRequest(HttpListenerContext listenerContext)
152150
return;
153151
}
154152

155-
if (await Authorize(listenerContext, clientName))
153+
var authorized = await Authorize(listenerContext, clientName).ConfigureAwait(false);
154+
155+
if (authorized)
156156
{
157157
// Delegate the open stream to the protocol handler - we no longer own the stream lifetime
158-
await ExchangeMessages(webSocketStream);
158+
await ExchangeMessages(webSocketStream).ConfigureAwait(false);
159159

160160
// Mark the stream as delegated once everything has succeeded
161161
keepConnection = true;
@@ -202,7 +202,7 @@ void SendFriendlyHtmlPage(HttpListenerResponse response)
202202
async Task<bool> Authorize(HttpListenerContext context, EndPoint clientName)
203203
{
204204
log.Write(EventType.Diagnostic, "Begin authorization");
205-
var certificate = await context.Request.GetClientCertificateAsync();
205+
var certificate = await context.Request.GetClientCertificateAsync().ConfigureAwait(false);
206206
if (certificate == null)
207207
{
208208
log.Write(EventType.ClientDenied, "A client at {0} connected, and attempted a message exchange, but did not present a client certificate", clientName);

0 commit comments

Comments
 (0)