GithubScanResults.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Microsoft.Extensions.DependencyInjection;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. namespace FastGithub.Scanner
  6. {
  7. /// <summary>
  8. /// GithubContext集合
  9. /// </summary>
  10. [Service(ServiceLifetime.Singleton)]
  11. sealed class GithubScanResults
  12. {
  13. private readonly object syncRoot = new();
  14. private readonly List<GithubContext> contexts = new();
  15. /// <summary>
  16. /// 添加GithubContext
  17. /// </summary>
  18. /// <param name="context"></param>
  19. /// <returns></returns>
  20. public bool Add(GithubContext context)
  21. {
  22. lock (this.syncRoot)
  23. {
  24. if (this.contexts.Contains(context))
  25. {
  26. return false;
  27. }
  28. this.contexts.Add(context);
  29. return true;
  30. }
  31. }
  32. /// <summary>
  33. /// 转换为数组
  34. /// </summary>
  35. /// <returns></returns>
  36. public GithubContext[] ToArray()
  37. {
  38. lock (this.syncRoot)
  39. {
  40. return this.contexts.ToArray();
  41. }
  42. }
  43. /// <summary>
  44. /// 查找最优的ip
  45. /// </summary>
  46. /// <param name="domain"></param>
  47. /// <returns></returns>
  48. public IPAddress? FindBestAddress(string domain)
  49. {
  50. lock (this.syncRoot)
  51. {
  52. return this.contexts
  53. .Where(item => item.Domain == domain && item.AvailableRate > 0d)
  54. .OrderByDescending(item => item.AvailableRate)
  55. .ThenByDescending(item => item.Available)
  56. .ThenBy(item => item.AvgElapsed)
  57. .Select(item => item.Address)
  58. .FirstOrDefault();
  59. }
  60. }
  61. }
  62. }