Doing some dangerous experiements to optimize memory usage

This commit is contained in:
2026-01-01 23:14:17 +08:00
parent b3633538cd
commit 683fbf1a68
10 changed files with 398 additions and 116 deletions

View File

@@ -1,4 +1,5 @@
using DysonNetwork.Shared.Cache;
using DysonNetwork.Shared.Registry;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Configuration;
@@ -45,20 +46,27 @@ public static class Extensions
// Turn on service discovery by default
http.AddServiceDiscovery();
// Ignore CA
http.ConfigurePrimaryHttpMessageHandler(sp => new HttpClientHandler
http.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (_, _, _, _) => true,
MaxConnectionsPerServer = 5,
});
});
builder.Services.AddSingleton<IClock>(SystemClock.Instance);
builder.Services.AddSharedGrpcChannels();
builder.AddNatsClient("Queue");
builder.AddRedisClient(
"Cache",
configureOptions: opts =>
{
opts.AbortOnConnectFail = false;
opts.ConnectRetry = 3;
opts.ConnectTimeout = 5000;
opts.SyncTimeout = 3000;
opts.AsyncTimeout = 3000;
}
);
@@ -81,7 +89,7 @@ public static class Extensions
return builder;
}
public TBuilder ConfigureOpenTelemetry()
private TBuilder ConfigureOpenTelemetry()
{
builder.Logging.AddOpenTelemetry(logging =>
{

View File

@@ -0,0 +1,71 @@
using Grpc.Net.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
namespace DysonNetwork.Shared.Registry;
public class GrpcChannelManager : IDisposable
{
private readonly ConcurrentDictionary<string, GrpcChannel> _channels = new();
private readonly ILogger<GrpcChannelManager> _logger;
public GrpcChannelManager(ILogger<GrpcChannelManager> logger)
{
_logger = logger;
}
public GrpcChannel GetOrCreateChannel(string endpoint, string serviceName)
{
return _channels.GetOrAdd(endpoint, ep =>
{
_logger.LogInformation("Creating gRPC channel for {Service} at {Endpoint}", serviceName, ep);
var options = new GrpcChannelOptions
{
MaxReceiveMessageSize = 100 * 1024 * 1024, // 100MB
MaxSendMessageSize = 100 * 1024 * 1024, // 100MB
};
return GrpcChannel.ForAddress(ep, options);
});
}
public void Dispose()
{
foreach (var channel in _channels.Values)
{
channel.Dispose();
}
_channels.Clear();
}
}
public static class GrpcSharedChannelExtensions
{
public static IServiceCollection AddSharedGrpcChannels(this IServiceCollection services)
{
services.AddSingleton<GrpcChannelManager>();
return services;
}
public static IHttpClientBuilder ConfigureGrpcDefaults(this IHttpClientBuilder builder)
{
builder.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (_, _, _, _) => true,
MaxConnectionsPerServer = 2,
});
return builder;
}
public static IServiceCollection AddGrpcClientWithSharedChannel<TClient>(
this IServiceCollection services,
string endpoint,
string serviceName
) where TClient : class
{
services.AddGrpcClient<TClient>(options => { options.Address = new Uri(endpoint); }).ConfigureGrpcDefaults();
return services;
}
}

View File

@@ -0,0 +1,51 @@
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace DysonNetwork.Shared.Registry;
public interface IGrpcClientFactory<out TClient> where TClient : class
{
TClient CreateClient();
}
public class LazyGrpcClientFactory<TClient>(
IServiceProvider serviceProvider,
ILogger<LazyGrpcClientFactory<TClient>> logger
) : IGrpcClientFactory<TClient> where TClient : class
{
private TClient? _client;
private readonly Lock _lock = new();
public TClient CreateClient()
{
if (Volatile.Read(ref _client) != null)
{
return Volatile.Read(ref _client)!;
}
lock (_lock)
{
if (Volatile.Read(ref _client) != null)
{
return Volatile.Read(ref _client)!;
}
var client = serviceProvider.GetRequiredService<TClient>();
Volatile.Write(ref _client, client);
logger.LogInformation("Lazy initialized gRPC client: {ClientType}", typeof(TClient).Name);
return Volatile.Read(ref _client)!;
}
}
}
public static class GrpcClientFactoryExtensions
{
public static IServiceCollection AddLazyGrpcClientFactory<TClient>(this IServiceCollection services)
where TClient : class
{
services.AddScoped<LazyGrpcClientFactory<TClient>>();
services.AddScoped<IGrpcClientFactory<TClient>>(sp => sp.GetRequiredService<LazyGrpcClientFactory<TClient>>());
return services;
}
}

View File

@@ -5,136 +5,107 @@ namespace DysonNetwork.Shared.Registry;
public static class ServiceInjectionHelper
{
public static IServiceCollection AddRingService(this IServiceCollection services)
extension(IServiceCollection services)
{
services
.AddGrpcClient<RingService.RingServiceClient>(o => o.Address = new Uri("https://_grpc.ring"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
public IServiceCollection AddRingService()
{
services.AddGrpcClientWithSharedChannel<RingService.RingServiceClient>(
"https://_grpc.ring",
"RingService");
return services;
}
return services;
}
public static IServiceCollection AddAuthService(this IServiceCollection services)
{
services
.AddGrpcClient<AuthService.AuthServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
public IServiceCollection AddAuthService()
{
services.AddGrpcClientWithSharedChannel<AuthService.AuthServiceClient>(
"https://_grpc.pass",
"AuthService");
services
.AddGrpcClient<PermissionService.PermissionServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddGrpcClientWithSharedChannel<PermissionService.PermissionServiceClient>(
"https://_grpc.pass",
"PermissionService");
return services;
}
return services;
}
public static IServiceCollection AddAccountService(this IServiceCollection services)
{
services
.AddGrpcClient<AccountService.AccountServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddSingleton<RemoteAccountService>();
public IServiceCollection AddAccountService()
{
services.AddGrpcClientWithSharedChannel<AccountService.AccountServiceClient>(
"https://_grpc.pass",
"AccountService");
services.AddSingleton<RemoteAccountService>();
services
.AddGrpcClient<BotAccountReceiverService.BotAccountReceiverServiceClient>(o =>
o.Address = new Uri("https://_grpc.pass")
)
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddGrpcClientWithSharedChannel<BotAccountReceiverService.BotAccountReceiverServiceClient>(
"https://_grpc.pass",
"BotAccountReceiverService");
services.AddGrpcClient<ActionLogService.ActionLogServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddGrpcClientWithSharedChannel<ActionLogService.ActionLogServiceClient>(
"https://_grpc.pass",
"ActionLogService");
services.AddGrpcClient<PaymentService.PaymentServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddGrpcClientWithSharedChannel<PaymentService.PaymentServiceClient>(
"https://_grpc.pass",
"PaymentService");
services.AddGrpcClient<WalletService.WalletServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddGrpcClientWithSharedChannel<WalletService.WalletServiceClient>(
"https://_grpc.pass",
"WalletService");
services
.AddGrpcClient<RealmService.RealmServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddSingleton<RemoteRealmService>();
services
.AddGrpcClient<SocialCreditService.SocialCreditServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services
.AddGrpcClient<ExperienceService.ExperienceServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddGrpcClientWithSharedChannel<RealmService.RealmServiceClient>(
"https://_grpc.pass",
"RealmService");
services.AddSingleton<RemoteRealmService>();
return services;
}
services.AddGrpcClientWithSharedChannel<SocialCreditService.SocialCreditServiceClient>(
"https://_grpc.pass",
"SocialCreditService");
public static IServiceCollection AddDriveService(this IServiceCollection services)
{
services.AddGrpcClient<FileService.FileServiceClient>(o => o.Address = new Uri("https://_grpc.drive"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddGrpcClientWithSharedChannel<ExperienceService.ExperienceServiceClient>(
"https://_grpc.pass",
"ExperienceService");
services.AddGrpcClient<FileReferenceService.FileReferenceServiceClient>(o =>
o.Address = new Uri("https://_grpc.drive"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
return services;
}
return services;
}
public IServiceCollection AddDriveService()
{
services.AddGrpcClientWithSharedChannel<FileService.FileServiceClient>(
"https://_grpc.drive",
"FileService");
public static IServiceCollection AddSphereService(this IServiceCollection services)
{
services
.AddGrpcClient<PostService.PostServiceClient>(o => o.Address = new Uri("https://_grpc.sphere"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddGrpcClientWithSharedChannel<FileReferenceService.FileReferenceServiceClient>(
"https://_grpc.drive",
"FileReferenceService");
services
.AddGrpcClient<PublisherService.PublisherServiceClient>(o => o.Address = new Uri("https://_grpc.sphere"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
return services;
}
services
.AddGrpcClient<PollService.PollServiceClient>(o => o.Address = new Uri("https://_grpc.sphere"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddSingleton<RemotePublisherService>();
public IServiceCollection AddSphereService()
{
services.AddGrpcClientWithSharedChannel<PostService.PostServiceClient>(
"https://_grpc.sphere",
"PostService");
return services;
}
services.AddGrpcClientWithSharedChannel<PublisherService.PublisherServiceClient>(
"https://_grpc.sphere",
"PublisherService");
public static IServiceCollection AddDevelopService(this IServiceCollection services)
{
services.AddGrpcClient<CustomAppService.CustomAppServiceClient>(o =>
o.Address = new Uri("https://_grpc.develop"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddGrpcClientWithSharedChannel<PollService.PollServiceClient>(
"https://_grpc.sphere",
"PollService");
services.AddSingleton<RemotePublisherService>();
return services;
return services;
}
public IServiceCollection AddDevelopService()
{
services.AddGrpcClientWithSharedChannel<CustomAppService.CustomAppServiceClient>(
"https://_grpc.develop",
"CustomAppService");
return services;
}
}
}