💥 Make gateway no longer terminate ws connections
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Text;
|
||||
using dotnet_etcd.interfaces;
|
||||
using Yarp.ReverseProxy.Configuration;
|
||||
using Yarp.ReverseProxy.Forwarder;
|
||||
|
||||
namespace DysonNetwork.Gateway;
|
||||
|
||||
@@ -14,8 +15,11 @@ public class RegistryProxyConfigProvider : IProxyConfigProvider, IDisposable
|
||||
private CancellationTokenSource _cts;
|
||||
private IProxyConfig _config;
|
||||
|
||||
public RegistryProxyConfigProvider(IEtcdClient etcdClient, IConfiguration configuration,
|
||||
ILogger<RegistryProxyConfigProvider> logger)
|
||||
public RegistryProxyConfigProvider(
|
||||
IEtcdClient etcdClient,
|
||||
IConfiguration configuration,
|
||||
ILogger<RegistryProxyConfigProvider> logger
|
||||
)
|
||||
{
|
||||
_etcdClient = etcdClient;
|
||||
_configuration = configuration;
|
||||
@@ -86,6 +90,10 @@ public class RegistryProxyConfigProvider : IProxyConfigProvider, IDisposable
|
||||
Destinations = new Dictionary<string, DestinationConfig>
|
||||
{
|
||||
{ "destination1", new DestinationConfig { Address = serviceUrl } }
|
||||
},
|
||||
HttpRequest = new ForwarderRequestConfig()
|
||||
{
|
||||
ActivityTimeout = directRoute.IsWebsocket ? TimeSpan.FromHours(24) : TimeSpan.FromMinutes(2)
|
||||
}
|
||||
};
|
||||
clusters.Add(cluster);
|
||||
@@ -96,7 +104,7 @@ public class RegistryProxyConfigProvider : IProxyConfigProvider, IDisposable
|
||||
RouteId = $"direct-{directRoute.Service}-{directRoute.Path.Replace("/", "-")}",
|
||||
ClusterId = directRoute.Service,
|
||||
Match = new RouteMatch { Path = directRoute.Path },
|
||||
TimeoutPolicy = directRoute.IsWebsocket ? "Disable" : null
|
||||
Timeout = directRoute.IsWebsocket ? null : TimeSpan.FromSeconds(5),
|
||||
};
|
||||
routes.Add(route);
|
||||
_logger.LogInformation(" Added Direct Route: {Path} -> {Service}", directRoute.Path,
|
||||
@@ -194,16 +202,25 @@ public class RegistryProxyConfigProvider : IProxyConfigProvider, IDisposable
|
||||
{
|
||||
new() { { "PathRemovePrefix", $"/{pathAlias}" } },
|
||||
new() { { "PathPrefix", "/api" } }
|
||||
}
|
||||
},
|
||||
Timeout = TimeSpan.FromSeconds(5)
|
||||
};
|
||||
routes.Add(pathRoute);
|
||||
_logger.LogInformation(" Added Path-based Route: {Path}", pathRoute.Match.Path);
|
||||
}
|
||||
|
||||
return new CustomProxyConfig(routes, clusters, new Microsoft.Extensions.Primitives.CancellationChangeToken(_cts.Token));
|
||||
return new CustomProxyConfig(
|
||||
routes,
|
||||
clusters,
|
||||
new Microsoft.Extensions.Primitives.CancellationChangeToken(_cts.Token)
|
||||
);
|
||||
}
|
||||
|
||||
private class CustomProxyConfig(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters, Microsoft.Extensions.Primitives.IChangeToken changeToken)
|
||||
private class CustomProxyConfig(
|
||||
IReadOnlyList<RouteConfig> routes,
|
||||
IReadOnlyList<ClusterConfig> clusters,
|
||||
Microsoft.Extensions.Primitives.IChangeToken changeToken
|
||||
)
|
||||
: IProxyConfig
|
||||
{
|
||||
public IReadOnlyList<RouteConfig> Routes { get; } = routes;
|
||||
@@ -225,4 +242,4 @@ public class RegistryProxyConfigProvider : IProxyConfigProvider, IDisposable
|
||||
_watchCts.Cancel();
|
||||
_watchCts.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
using DysonNetwork.Shared.Registry;
|
||||
using Microsoft.AspNetCore.Http.Timeouts;
|
||||
using Yarp.ReverseProxy.Configuration;
|
||||
|
||||
namespace DysonNetwork.Gateway.Startup;
|
||||
@@ -7,10 +8,15 @@ public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddGateway(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddReverseProxy();
|
||||
services
|
||||
.AddReverseProxy()
|
||||
.ConfigureHttpClient((context, handler) =>
|
||||
{
|
||||
});
|
||||
|
||||
services.AddRegistryService(configuration);
|
||||
services.AddSingleton<IProxyConfigProvider, RegistryProxyConfigProvider>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
@@ -31,7 +31,7 @@
|
||||
{
|
||||
"Path": "/ws",
|
||||
"Service": "DysonNetwork.Pusher",
|
||||
"IsWebSocket": true
|
||||
"IsWebsocket": true
|
||||
},
|
||||
{
|
||||
"Path": "/.well-known/openid-configuration",
|
||||
|
Reference in New Issue
Block a user