hangfire.entityframeworkcore这个库因为System.Threading.Timer未停止也未释放而导致的性能问题
如题
hangfire.entityframeworkcore这个库因为System.Threading.Timer未停止也未释放,导致产生大量的Timer一直在执行,占用大量cpu和内存。
https://github.com/sergezhigunov/Hangfire.EntityFrameworkCore/issues/32
public class Worker : IBackgroundProcess
{
public void Execute(BackgroundProcessContext context)
{
using (var fetchedJob = connection.FetchNextJob(_queues.ToArray(), context.StoppingToken))
{
...
fetchedJob.RemoveFromQueue();// 它会设置EFCoreFetchedJob._completed=true,进而导致后续的_timer.Dispose()未执行
...
}
}
}
internal sealed class EFCoreFetchedJob : IFetchedJob
{
private bool _completed;
public void RemoveFromQueue()
{
lock (_lock)
{
_storage.UseContext(context =>
{
context.Remove(_queuedJob);
try
{
context.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
// Someone else already has removed item, database wins
}
});
_completed = true;//这里在Dispose前就执行了
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing && !_completed)//因为_completed=true,所以_timer.Dispose()不执行
{
_timer.Dispose();
Requeue();
}
_disposed = true;
}
}
}
作者:Rick Carter
出处:http://pains.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
浙公网安备 33010602011771号