mirror of
https://github.com/239573049/FastGateway.git
synced 2026-10-17 05:20:30 +08:00
优化内存
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using FastGateway.Service.DataAccess;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FastGateway.Service.BackgroundTask;
|
||||
|
||||
public class LogCleaningBackgroundService(ILogger<LogCleaningBackgroundService> logger,IServiceProvider serviceProvider) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
// 暂停1分钟
|
||||
await Task.Delay(1000 * 60, stoppingToken);
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 暂停一天
|
||||
await Task.Delay(TimeSpan.FromDays(1), stoppingToken);
|
||||
|
||||
await using var scope = serviceProvider.CreateAsyncScope();
|
||||
|
||||
var loggerContext = scope.ServiceProvider.GetRequiredService<LoggerContext>();
|
||||
|
||||
// 清理掉一个月前的日志
|
||||
var date = DateTime.Now.AddMonths(-1);
|
||||
var count = await loggerContext.ApplicationLoggers
|
||||
.Where(x => x.RequestTime < date)
|
||||
.ExecuteDeleteAsync(cancellationToken: stoppingToken);
|
||||
|
||||
logger.LogInformation($"清理日志:{count}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "日志清理失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,33 +32,32 @@ public sealed class LoggerBackgroundTask(IServiceProvider serviceProvider, ISear
|
||||
try
|
||||
{
|
||||
int count = 0;
|
||||
while (LoggerBag.Reader.TryRead(out var item))
|
||||
var item = await LoggerBag.Reader.ReadAsync(stoppingToken);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(item.Ip))
|
||||
continue;
|
||||
|
||||
var locations = searcher.Search(item.Ip)?.Split("|", StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
locations = locations?.Where(x => x != "0").ToArray();
|
||||
if (locations == null || locations?.Length == 0)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(item.Ip))
|
||||
continue;
|
||||
continue;
|
||||
}
|
||||
|
||||
var locations = searcher.Search(item.Ip)?.Split("|", StringSplitOptions.RemoveEmptyEntries);
|
||||
item.Country = locations?.First();
|
||||
item.Region = string.Join("|", locations)
|
||||
.Replace("电信", "")
|
||||
.Replace("联通", "")
|
||||
.Replace("移动", "")
|
||||
.TrimStart('|')
|
||||
.TrimEnd('|');
|
||||
|
||||
locations = locations?.Where(x => x != "0").ToArray();
|
||||
if (locations == null || locations?.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
item.Country = locations?.First();
|
||||
item.Region = string.Join("|", locations)
|
||||
.Replace("电信", "")
|
||||
.Replace("联通", "")
|
||||
.Replace("移动", "")
|
||||
.TrimStart('|')
|
||||
.TrimEnd('|');
|
||||
|
||||
await loggerContext.ApplicationLoggers.AddAsync(item, stoppingToken);
|
||||
count++;
|
||||
if (count >= 100)
|
||||
{
|
||||
await loggerContext.SaveChangesAsync(stoppingToken);
|
||||
}
|
||||
await loggerContext.ApplicationLoggers.AddAsync(item, stoppingToken);
|
||||
count++;
|
||||
if (count >= 100)
|
||||
{
|
||||
await loggerContext.SaveChangesAsync(stoppingToken);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -63,7 +63,7 @@ public class RenewSSLBackgroundService(ILogger<RenewSSLBackgroundService> logger
|
||||
|
||||
if (isRenew)
|
||||
{
|
||||
CertService.InitCert(await dbContext.Certs.ToListAsync(cancellationToken: stoppingToken));
|
||||
CertService.InitCert(await dbContext.Certs.ToArrayAsync(cancellationToken: stoppingToken));
|
||||
}
|
||||
|
||||
// 每24小时检查一次
|
||||
|
||||
@@ -47,7 +47,7 @@ public static class Gateway
|
||||
/// </summary>
|
||||
/// <param name="server"></param>
|
||||
/// <param name="domainNames"></param>
|
||||
public static void ReloadGateway(Server server, List<DomainName> domainNames)
|
||||
public static void ReloadGateway(Server server, DomainName[] domainNames)
|
||||
{
|
||||
if (GatewayWebApplications.TryGetValue(server.Id, out var webApplication))
|
||||
{
|
||||
@@ -107,7 +107,7 @@ public static class Gateway
|
||||
/// <summary>
|
||||
/// 构建网关
|
||||
/// </summary>
|
||||
public static async Task BuilderGateway(Server server, List<DomainName> domainNames,
|
||||
public static async Task BuilderGateway(Server server, DomainName[] domainNames,
|
||||
List<BlacklistAndWhitelist> blacklistAndWhitelists, List<RateLimit> rateLimits)
|
||||
{
|
||||
try
|
||||
@@ -339,7 +339,7 @@ public static class Gateway
|
||||
}
|
||||
|
||||
private static (IReadOnlyList<RouteConfig> routes,
|
||||
IReadOnlyList<ClusterConfig> clusters) BuildConfig(List<DomainName> domainNames)
|
||||
IReadOnlyList<ClusterConfig> clusters) BuildConfig(DomainName[] domainNames)
|
||||
{
|
||||
var routes = new List<RouteConfig>();
|
||||
var clusters = new List<ClusterConfig>();
|
||||
|
||||
@@ -76,6 +76,7 @@ public static class Program
|
||||
builder.Services.AddHostedService<LoggerBackgroundTask>();
|
||||
builder.Services.AddHostedService<RenewSSLBackgroundService>();
|
||||
builder.Services.AddHostedService<ClientRequestBackgroundTask>();
|
||||
builder.Services.AddHostedService<LogCleaningBackgroundService>();
|
||||
builder.Services.AddDbContext<MasterContext>(optionsBuilder =>
|
||||
{
|
||||
// 判断当前目录是否存在data文件夹
|
||||
@@ -109,16 +110,17 @@ public static class Program
|
||||
await dbContext.Database.MigrateAsync();
|
||||
await loggerContext.Database.MigrateAsync();
|
||||
|
||||
CertService.InitCert(await dbContext.Certs.Where(x => x.Expired == false).ToListAsync());
|
||||
var certs = await dbContext.Certs.Where(x => x.Expired == false).ToArrayAsync();
|
||||
|
||||
var server = await dbContext.Servers.ToListAsync();
|
||||
var domainNames = await dbContext.DomainNames.ToListAsync();
|
||||
CertService.InitCert(certs);
|
||||
|
||||
var domainNames = await dbContext.DomainNames.ToArrayAsync();
|
||||
var blacklistAndWhitelists = await dbContext.BlacklistAndWhitelists.ToListAsync();
|
||||
var rateLimits = await dbContext.RateLimits.ToListAsync();
|
||||
foreach (var item in server)
|
||||
foreach (var item in await dbContext.Servers.ToArrayAsync())
|
||||
{
|
||||
await Task.Factory.StartNew(async () =>
|
||||
await Gateway.Gateway.BuilderGateway(item, domainNames.Where(x => x.ServerId == item.Id).ToList(),
|
||||
await Gateway.Gateway.BuilderGateway(item, domainNames.Where(x => x.ServerId == item.Id).ToArray(),
|
||||
blacklistAndWhitelists, rateLimits));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public static class CertService
|
||||
return CertWebApplications.TryGetValue(domain, out var cert) ? cert : null;
|
||||
}
|
||||
|
||||
public static void InitCert(List<Cert> certs)
|
||||
public static void InitCert(Cert[] certs)
|
||||
{
|
||||
foreach (var cert in certs)
|
||||
{
|
||||
@@ -111,7 +111,7 @@ public static class CertService
|
||||
.SetProperty(x => x.RenewTime, x => cert.RenewTime)
|
||||
);
|
||||
|
||||
InitCert(await masterContext.Certs.Where(x => !x.Expired).ToListAsync());
|
||||
InitCert(await masterContext.Certs.Where(x => !x.Expired).ToArrayAsync());
|
||||
}
|
||||
|
||||
return ResultDto.CreateSuccess();
|
||||
|
||||
@@ -90,7 +90,7 @@ public static class ServerService
|
||||
if (!Gateway.Gateway.CheckServerOnline(id))
|
||||
{
|
||||
var server = await dbContext.Servers.FirstOrDefaultAsync(x => x.Id == id);
|
||||
var domainNames = await dbContext.DomainNames.Where(x => x.ServerId == id).ToListAsync();
|
||||
var domainNames = await dbContext.DomainNames.Where(x => x.ServerId == id).ToArrayAsync();
|
||||
var blacklistAndWhitelists = await dbContext.BlacklistAndWhitelists.ToListAsync();
|
||||
var rateLimits = await dbContext.RateLimits.ToListAsync();
|
||||
await Task.Factory.StartNew(async () =>
|
||||
@@ -121,7 +121,7 @@ public static class ServerService
|
||||
throw new ValidationException("服务不存在");
|
||||
}
|
||||
|
||||
var domainNames = await dbContext.DomainNames.Where(x => x.ServerId == id).ToListAsync();
|
||||
var domainNames = await dbContext.DomainNames.Where(x => x.ServerId == id).ToArrayAsync();
|
||||
|
||||
Gateway.Gateway.ReloadGateway(server, domainNames);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user