Skip to content

Commit 555c306

Browse files
gedemgedem
authored andcommitted
Try connect resilience improvements
1 parent 170bd54 commit 555c306

9 files changed

Lines changed: 30 additions & 89 deletions

File tree

src/NetCoreStack.WebSockets.ProxyClient/ApplicationBuilderExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
using Microsoft.Extensions.DependencyInjection;
44
using Microsoft.Extensions.Logging;
55
using System;
6+
using System.Threading;
67
using System.Threading.Tasks;
78

89
namespace NetCoreStack.WebSockets.ProxyClient
910
{
1011
public static class ApplicationBuilderExtensions
1112
{
12-
public static IApplicationBuilder UseProxyWebSockets(this IApplicationBuilder app)
13+
public static IApplicationBuilder UseProxyWebSockets(this IApplicationBuilder app, CancellationTokenSource cancellationTokenSource = null)
1314
{
1415
var appLifeTime = app.ApplicationServices.GetService<IApplicationLifetime>();
1516
var loggerFactory = app.ApplicationServices.GetService<ILoggerFactory>();
@@ -19,7 +20,7 @@ public static IApplicationBuilder UseProxyWebSockets(this IApplicationBuilder ap
1920
if (webSocketConnector != null && appLifeTime != null)
2021
{
2122
appLifeTime.ApplicationStopping.Register(OnShutdown, webSocketConnector);
22-
Task.Run(async () => await webSocketConnector.ConnectAsync());
23+
Task.Run(async () => await webSocketConnector.ConnectAsync(cancellationTokenSource));
2324
}
2425

2526
return app;

src/NetCoreStack.WebSockets.ProxyClient/ClientWebSocketConnector.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ public ClientWebSocketConnector(IOptions<ProxyOptions> options,
5050
Options = options.Value;
5151
}
5252

53-
private async Task TryConnectAsync()
53+
private async Task TryConnectAsync(CancellationTokenSource cancellationTokenSource = null)
5454
{
5555
var name = Options.ConnectorName;
5656
var uri = new Uri($"ws://{Options.WebSocketHostAddress}");
5757
_webSocket = new ClientWebSocket();
5858
_webSocket.Options.SetRequestHeader(SocketsConstants.ConnectorName, Options.ConnectorName);
5959
try
6060
{
61+
CancellationToken token = cancellationTokenSource != null ? cancellationTokenSource.Token : CancellationToken.None;
6162
await _webSocket.ConnectAsync(uri, CancellationToken.None);
6263
}
6364
catch (Exception ex)
@@ -79,20 +80,26 @@ private async Task TryConnectAsync()
7980
await receiver.ReceiveAsync();
8081
}
8182

82-
public async Task ConnectAsync()
83+
public async Task ConnectAsync(CancellationTokenSource cancellationTokenSource = null)
8384
{
84-
try
85-
{
86-
await TryConnectAsync();
87-
}
88-
catch (Exception ex)
89-
{
90-
ProxyLogHelper.Log(_loggerFactory, Options, "Error", ex);
91-
}
92-
finally
85+
if (cancellationTokenSource == null)
86+
cancellationTokenSource = new CancellationTokenSource();
87+
88+
while (!cancellationTokenSource.IsCancellationRequested)
9389
{
94-
if (_webSocket != null)
95-
_webSocket.Dispose();
90+
try
91+
{
92+
await TryConnectAsync(cancellationTokenSource);
93+
}
94+
catch (Exception ex)
95+
{
96+
ProxyLogHelper.Log(_loggerFactory, Options, "Error", ex);
97+
}
98+
99+
if (WebSocketState == WebSocketState.Open)
100+
{
101+
break;
102+
}
96103
}
97104
}
98105

src/NetCoreStack.WebSockets.ProxyClient/ConsoleApplicationBuilderExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using Microsoft.Extensions.Logging;
33
using System;
4+
using System.Threading;
45
using System.Threading.Tasks;
56

67
namespace NetCoreStack.WebSockets.ProxyClient.Console
78
{
89
public static class ConsoleApplicationBuilderExtensions
910
{
10-
public static IServiceProvider UseProxyWebSocket(this IServiceProvider services)
11+
public static IServiceProvider UseProxyWebSocket(this IServiceProvider services, CancellationTokenSource cancellationTokenSource = null)
1112
{
1213
var loggerFactory = services.GetService<ILoggerFactory>();
1314
if (loggerFactory == null)
@@ -18,7 +19,7 @@ public static IServiceProvider UseProxyWebSocket(this IServiceProvider services)
1819
if (webSocketConnector == null)
1920
throw new ArgumentNullException($"{nameof(webSocketConnector)} please try AddProxyWebSockets");
2021

21-
Task.Run(async () => await webSocketConnector.ConnectAsync());
22+
Task.Run(async () => await webSocketConnector.ConnectAsync(cancellationTokenSource));
2223

2324
return services;
2425
}

src/NetCoreStack.WebSockets.ProxyClient/IWebSocketConnector.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Net.WebSockets;
2+
using System.Threading;
23
using System.Threading.Tasks;
34

45
namespace NetCoreStack.WebSockets.ProxyClient
@@ -8,7 +9,7 @@ public interface IWebSocketConnector
89
string ConnectionId { get; }
910
WebSocketState WebSocketState { get; }
1011
ProxyOptions Options { get; }
11-
Task ConnectAsync();
12+
Task ConnectAsync(CancellationTokenSource cancellationTokenSource);
1213
Task SendAsync(WebSocketMessageContext context);
1314
Task SendBinaryAsync(byte[] bytes);
1415
}

src/NetCoreStack.WebSockets.ProxyClient/TaskManager.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/NetCoreStack.WebSockets.ProxyClient/WebSocketConnectorExtensions.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

test/ConsoleAppProxyClient/Program.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
using NetCoreStack.WebSockets.ProxyClient;
44
using NetCoreStack.WebSockets.ProxyClient.Console;
55
using System;
6-
using System.Diagnostics;
7-
using System.Net.WebSockets;
8-
using System.Threading.Tasks;
96

107
namespace ConsoleAppProxyClient
118
{

test/WebClientTestApp/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void ConfigureServices(IServiceCollection services)
3434

3535
// Client WebSocket - DMZ to API side connections
3636
services.AddProxyWebSockets(options => {
37-
options.ConnectorName = Environment.MachineName;
37+
options.ConnectorName = $"TestApp-{Environment.MachineName}";
3838
options.WebSocketHostAddress = "localhost:7803";
3939
options.RegisterInvocator<CustomWebSocketCommandInvocator>(WebSocketCommands.All);
4040
});

test/WebClientTestApp2/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void ConfigureServices(IServiceCollection services)
3434

3535
// Client WebSocket - DMZ to API side connections
3636
services.AddProxyWebSockets(options => {
37-
options.ConnectorName = Environment.MachineName;
37+
options.ConnectorName = $"TestApp2-{Environment.MachineName}";
3838
options.WebSocketHostAddress = "localhost:7803";
3939
options.RegisterInvocator<WebSocketCommandInvocator>(WebSocketCommands.All);
4040
});

0 commit comments

Comments
 (0)