FastGithub/FastGithub.Scanner/Middlewares/ConcurrentMiddleware.cs
2021-06-16 20:29:45 +08:00

33 lines
862 B
C#

using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.Scanner.Middlewares
{
[Service(ServiceLifetime.Singleton)]
sealed class ConcurrentMiddleware : IMiddleware<GithubContext>
{
private readonly SemaphoreSlim semaphoreSlim;
public ConcurrentMiddleware()
{
var initialCount = Environment.ProcessorCount;
this.semaphoreSlim = new SemaphoreSlim(initialCount, initialCount * 4);
}
public async Task InvokeAsync(GithubContext context, Func<Task> next)
{
try
{
await this.semaphoreSlim.WaitAsync();
await next();
}
finally
{
this.semaphoreSlim.Release();
}
}
}
}