ThrottleManager.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Grpc.Extension.BaseService
  5. {
  6. public class ThrottleManager
  7. {
  8. private static readonly object sync = new object();
  9. private List<string> throttleMethods = new List<string>();
  10. private static Lazy<ThrottleManager> instance = new Lazy<ThrottleManager>(() => new ThrottleManager(), true);
  11. public static ThrottleManager Instance => instance.Value;
  12. public void Add(string fullName)
  13. {
  14. if (string.IsNullOrWhiteSpace(fullName)) return;
  15. lock (sync)
  16. {
  17. throttleMethods.Add(fullName.Trim());
  18. throttleMethods = throttleMethods.Distinct().ToList();
  19. }
  20. }
  21. public void Del(string fullName)
  22. {
  23. if (string.IsNullOrWhiteSpace(fullName)) return;
  24. lock (sync)
  25. {
  26. throttleMethods.Remove(fullName.Trim());
  27. }
  28. }
  29. public bool IsThrottled(string fullName)
  30. {
  31. if (string.IsNullOrWhiteSpace(fullName)) return false;
  32. lock (sync)
  33. {
  34. return throttleMethods.Contains(fullName.Trim());
  35. }
  36. }
  37. }
  38. }