diff --git a/src/FastGateway.Service/BackgroundTask/LogCleaningBackgroundService.cs b/src/FastGateway.Service/BackgroundTask/LogCleaningBackgroundService.cs new file mode 100644 index 0000000..e3317b9 --- /dev/null +++ b/src/FastGateway.Service/BackgroundTask/LogCleaningBackgroundService.cs @@ -0,0 +1,38 @@ +using FastGateway.Service.DataAccess; +using Microsoft.EntityFrameworkCore; + +namespace FastGateway.Service.BackgroundTask; + +public class LogCleaningBackgroundService(ILogger 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(); + + // 清理掉一个月前的日志 + 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, "日志清理失败"); + } + } + } +} \ No newline at end of file diff --git a/src/FastGateway.Service/BackgroundTask/LoggerBackgroundTask.cs b/src/FastGateway.Service/BackgroundTask/LoggerBackgroundTask.cs index 105891b..cd79668 100644 --- a/src/FastGateway.Service/BackgroundTask/LoggerBackgroundTask.cs +++ b/src/FastGateway.Service/BackgroundTask/LoggerBackgroundTask.cs @@ -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) diff --git a/src/FastGateway.Service/BackgroundTask/RenewSSLBackgroundService.cs b/src/FastGateway.Service/BackgroundTask/RenewSSLBackgroundService.cs index 96e702b..c0fcfb2 100644 --- a/src/FastGateway.Service/BackgroundTask/RenewSSLBackgroundService.cs +++ b/src/FastGateway.Service/BackgroundTask/RenewSSLBackgroundService.cs @@ -63,7 +63,7 @@ public class RenewSSLBackgroundService(ILogger logger if (isRenew) { - CertService.InitCert(await dbContext.Certs.ToListAsync(cancellationToken: stoppingToken)); + CertService.InitCert(await dbContext.Certs.ToArrayAsync(cancellationToken: stoppingToken)); } // 每24小时检查一次 diff --git a/src/FastGateway.Service/Gateway/Gateway.cs b/src/FastGateway.Service/Gateway/Gateway.cs index 3eca08b..fd3cabb 100644 --- a/src/FastGateway.Service/Gateway/Gateway.cs +++ b/src/FastGateway.Service/Gateway/Gateway.cs @@ -47,7 +47,7 @@ public static class Gateway /// /// /// - public static void ReloadGateway(Server server, List 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 /// /// 构建网关 /// - public static async Task BuilderGateway(Server server, List domainNames, + public static async Task BuilderGateway(Server server, DomainName[] domainNames, List blacklistAndWhitelists, List rateLimits) { try @@ -339,7 +339,7 @@ public static class Gateway } private static (IReadOnlyList routes, - IReadOnlyList clusters) BuildConfig(List domainNames) + IReadOnlyList clusters) BuildConfig(DomainName[] domainNames) { var routes = new List(); var clusters = new List(); diff --git a/src/FastGateway.Service/Program.cs b/src/FastGateway.Service/Program.cs index e731423..944f4cb 100644 --- a/src/FastGateway.Service/Program.cs +++ b/src/FastGateway.Service/Program.cs @@ -76,6 +76,7 @@ public static class Program builder.Services.AddHostedService(); builder.Services.AddHostedService(); builder.Services.AddHostedService(); + builder.Services.AddHostedService(); builder.Services.AddDbContext(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)); } } diff --git a/src/FastGateway.Service/Services/CertService.cs b/src/FastGateway.Service/Services/CertService.cs index 492f650..c488e15 100644 --- a/src/FastGateway.Service/Services/CertService.cs +++ b/src/FastGateway.Service/Services/CertService.cs @@ -43,7 +43,7 @@ public static class CertService return CertWebApplications.TryGetValue(domain, out var cert) ? cert : null; } - public static void InitCert(List 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(); diff --git a/src/FastGateway.Service/Services/ServerService.cs b/src/FastGateway.Service/Services/ServerService.cs index 9f9e2ae..28675d8 100644 --- a/src/FastGateway.Service/Services/ServerService.cs +++ b/src/FastGateway.Service/Services/ServerService.cs @@ -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);