diff --git a/FastGithub.Scanner/GithubContext.cs b/FastGithub.Scanner/GithubContext.cs
index dffefca..4273e49 100644
--- a/FastGithub.Scanner/GithubContext.cs
+++ b/FastGithub.Scanner/GithubContext.cs
@@ -18,24 +18,19 @@ namespace FastGithub.Scanner
///
/// 获取或设置是否可用
///
- public bool Available { get; set; } = false;
+ public bool Available { get; set; }
///
- /// 获取或设置扫描总耗时
+ /// 获取统计信息
///
- public TimeSpan Elapsed { get; set; } = TimeSpan.MaxValue;
+ public GithubContextStatistics Statistics { get; } = new();
public GithubContext(string domain, IPAddress address)
{
this.Domain = domain;
this.Address = address;
- }
-
- public override string ToString()
- {
- return $"{Address}\t{Domain}\t# {Elapsed}";
- }
+ }
public override bool Equals(object? obj)
{
diff --git a/FastGithub.Scanner/GithubContextCollection.cs b/FastGithub.Scanner/GithubContextCollection.cs
index 3abf00d..8108aca 100644
--- a/FastGithub.Scanner/GithubContextCollection.cs
+++ b/FastGithub.Scanner/GithubContextCollection.cs
@@ -8,21 +8,12 @@ namespace FastGithub.Scanner
{
private readonly object syncRoot = new();
private readonly HashSet contextHashSet = new();
- private readonly Dictionary domainAdressCache = new();
- public void AddOrUpdate(GithubContext context)
+ public bool Add(GithubContext context)
{
lock (this.syncRoot)
{
- if (this.contextHashSet.TryGetValue(context, out var value))
- {
- value.Elapsed = context.Elapsed;
- value.Available = context.Available;
- }
- else
- {
- this.contextHashSet.Add(context);
- }
+ return this.contextHashSet.Add(context);
}
}
@@ -39,35 +30,16 @@ namespace FastGithub.Scanner
///
///
///
- public IPAddress? FindFastAddress(string domain)
+ public IPAddress? FindBestAddress(string domain)
{
lock (this.syncRoot)
{
- // 如果上一次的ip可以使用,就返回上一次的ip
- if (this.domainAdressCache.TryGetValue(domain, out var address))
- {
- var key = new GithubContext(domain, address);
- if (this.contextHashSet.TryGetValue(key, out var context) && context.Available)
- {
- return address;
- }
- }
-
- var fastAddress = this.contextHashSet
+ return this.contextHashSet
.Where(item => item.Available && item.Domain == domain)
- .OrderBy(item => item.Elapsed)
+ .OrderByDescending(item => item.Statistics.GetSuccessRate())
+ .ThenBy(item => item.Statistics.GetAvgElapsed())
.Select(item => item.Address)
.FirstOrDefault();
-
- if (fastAddress != null)
- {
- this.domainAdressCache[domain] = fastAddress;
- }
- else
- {
- this.domainAdressCache.Remove(domain);
- }
- return fastAddress;
}
}
}
diff --git a/FastGithub.Scanner/GithubContextStatistics.cs b/FastGithub.Scanner/GithubContextStatistics.cs
new file mode 100644
index 0000000..f151fd3
--- /dev/null
+++ b/FastGithub.Scanner/GithubContextStatistics.cs
@@ -0,0 +1,57 @@
+using System;
+
+namespace FastGithub.Scanner
+{
+ sealed class GithubContextStatistics
+ {
+ ///
+ /// 扫描总次数
+ ///
+ public int TotalScanCount { get; private set; }
+
+
+ ///
+ /// 扫描总成功次数
+ ///
+ public int TotalSuccessCount { get; private set; }
+
+ ///
+ /// 扫描总耗时
+ ///
+ public TimeSpan TotalSuccessElapsed { get; private set; }
+
+
+ public void SetScan()
+ {
+ this.TotalScanCount += 1;
+ }
+
+ public void SetScanSuccess(TimeSpan elapsed)
+ {
+ this.TotalSuccessCount += 1;
+ this.TotalSuccessElapsed = this.TotalSuccessElapsed.Add(elapsed);
+ }
+
+ ///
+ /// 获取成功率
+ ///
+ ///
+ public double GetSuccessRate()
+ {
+ return this.TotalScanCount > 0 ?
+ (double)this.TotalSuccessCount / this.TotalScanCount
+ : 0d;
+ }
+
+ ///
+ /// 获取平均耗时
+ ///
+ ///
+ public TimeSpan GetAvgElapsed()
+ {
+ return this.TotalScanCount > 0
+ ? this.TotalSuccessElapsed / this.TotalScanCount
+ : TimeSpan.MaxValue;
+ }
+ }
+}
diff --git a/FastGithub.Scanner/GithubScanService.cs b/FastGithub.Scanner/GithubScanService.cs
index 480b573..c74e9d7 100644
--- a/FastGithub.Scanner/GithubScanService.cs
+++ b/FastGithub.Scanner/GithubScanService.cs
@@ -30,18 +30,18 @@ namespace FastGithub.Scanner
this.fullScanDelegate = pipelineBuilder
.New()
.Use()
- .Use()
- .Use()
- .Use()
.Use()
+ .Use()
+ .Use()
+ .Use()
.Build();
this.resultScanDelegate = pipelineBuilder
.New()
- .Use()
- .Use()
- .Use()
.Use()
+ .Use()
+ .Use()
+ .Use()
.Build();
}
@@ -62,7 +62,7 @@ namespace FastGithub.Scanner
await this.fullScanDelegate(context);
if (context.Available == true)
{
- this.results.AddOrUpdate(context);
+ this.results.Add(context);
}
}
}
@@ -83,7 +83,7 @@ namespace FastGithub.Scanner
public IPAddress? FindFastAddress(string domain)
{
return domain.Contains("github", StringComparison.OrdinalIgnoreCase)
- ? this.results.FindFastAddress(domain)
+ ? this.results.FindBestAddress(domain)
: default;
}
}
diff --git a/FastGithub.Scanner/Middlewares/ScanOkLogMiddleware.cs b/FastGithub.Scanner/Middlewares/ScanOkLogMiddleware.cs
index 18dfd16..62c68a0 100644
--- a/FastGithub.Scanner/Middlewares/ScanOkLogMiddleware.cs
+++ b/FastGithub.Scanner/Middlewares/ScanOkLogMiddleware.cs
@@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
+using System.Net;
using System.Threading.Tasks;
namespace FastGithub.Scanner.Middlewares
@@ -10,19 +11,29 @@ namespace FastGithub.Scanner.Middlewares
{
private readonly ILogger logger;
+ private record ScanOk(string Domain, IPAddress Address, int TotalScanCount, double SuccessRate, TimeSpan AvgElapsed);
+
public ScanOkLogMiddleware(ILogger logger)
{
this.logger = logger;
}
- public Task InvokeAsync(GithubContext context, Func next)
+ public async Task InvokeAsync(GithubContext context, Func next)
{
+ await next();
+
if (context.Available)
{
- this.logger.LogInformation(context.ToString());
- }
+ var mesage = new ScanOk(
+ context.Domain,
+ context.Address,
+ context.Statistics.TotalScanCount,
+ context.Statistics.GetSuccessRate(),
+ context.Statistics.GetAvgElapsed()
+ );
- return next();
+ this.logger.LogInformation(mesage.ToString());
+ }
}
}
}
diff --git a/FastGithub.Scanner/Middlewares/ScanElapsedMiddleware.cs b/FastGithub.Scanner/Middlewares/StatisticsMiddleware.cs
similarity index 67%
rename from FastGithub.Scanner/Middlewares/ScanElapsedMiddleware.cs
rename to FastGithub.Scanner/Middlewares/StatisticsMiddleware.cs
index 382c37b..ed2bfcb 100644
--- a/FastGithub.Scanner/Middlewares/ScanElapsedMiddleware.cs
+++ b/FastGithub.Scanner/Middlewares/StatisticsMiddleware.cs
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace FastGithub.Scanner.Middlewares
{
[Service(ServiceLifetime.Singleton)]
- sealed class ScanElapsedMiddleware : IMiddleware
+ sealed class StatisticsMiddleware : IMiddleware
{
public async Task InvokeAsync(GithubContext context, Func next)
{
@@ -14,12 +14,16 @@ namespace FastGithub.Scanner.Middlewares
try
{
stopwatch.Start();
+ context.Statistics.SetScan();
await next();
}
finally
{
stopwatch.Stop();
- context.Elapsed = stopwatch.Elapsed;
+ if (context.Available == true)
+ {
+ context.Statistics.SetScanSuccess(stopwatch.Elapsed);
+ }
}
}
}