ConcurrentMiddleware.cs 693 B

1234567891011121314151617181920212223242526
  1. using Microsoft.Extensions.DependencyInjection;
  2. using System;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. namespace FastGithub.Scanner.Middlewares
  6. {
  7. [Service(ServiceLifetime.Singleton)]
  8. sealed class ConcurrentMiddleware : IGithubScanMiddleware
  9. {
  10. private readonly SemaphoreSlim semaphoreSlim = new(Environment.ProcessorCount * 4);
  11. public async Task InvokeAsync(GithubContext context, Func<Task> next)
  12. {
  13. try
  14. {
  15. await this.semaphoreSlim.WaitAsync();
  16. await next();
  17. }
  18. finally
  19. {
  20. this.semaphoreSlim.Release();
  21. }
  22. }
  23. }
  24. }