mirror of
https://github.com/immense/Remotely.git
synced 2025-10-26 11:27:15 +00:00
Add CancellationChangeToken to force eviction of entries.
This commit is contained in:
parent
57f6be848b
commit
a6eaebdcfe
@ -1,11 +1,13 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Remotely.Server.Services;
|
||||
using Remotely.Shared.Enums;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Remotely.Server.API
|
||||
@ -14,7 +16,7 @@ namespace Remotely.Server.API
|
||||
[ApiController]
|
||||
public class AgentUpdateController : ControllerBase
|
||||
{
|
||||
private static readonly MemoryCache downloadingAgents = new MemoryCache(new MemoryCacheOptions()
|
||||
private static readonly MemoryCache _downloadingAgents = new MemoryCache(new MemoryCacheOptions()
|
||||
{ ExpirationScanFrequency = TimeSpan.FromSeconds(10) });
|
||||
|
||||
|
||||
@ -36,7 +38,7 @@ namespace Remotely.Server.API
|
||||
public ActionResult ClearDownload(string downloadId)
|
||||
{
|
||||
DataService.WriteEvent($"Clearing download ID {downloadId}.", EventType.Debug, null);
|
||||
downloadingAgents.Remove(downloadId);
|
||||
_downloadingAgents.Remove(downloadId);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
@ -46,16 +48,28 @@ namespace Remotely.Server.API
|
||||
try
|
||||
{
|
||||
var startWait = DateTimeOffset.Now;
|
||||
while (downloadingAgents.Count >= AppConfig.MaxConcurrentUpdates)
|
||||
|
||||
while (_downloadingAgents.Count >= AppConfig.MaxConcurrentUpdates)
|
||||
{
|
||||
await Task.Delay(new Random().Next(100, 10000));
|
||||
|
||||
_downloadingAgents.TryGetValue(string.Empty, out _);
|
||||
}
|
||||
|
||||
var expirationTimespan = TimeSpan.FromSeconds(10);
|
||||
|
||||
var expirationToken = new CancellationChangeToken(
|
||||
new CancellationTokenSource(expirationTimespan).Token);
|
||||
|
||||
var cacheOptions = new MemoryCacheEntryOptions()
|
||||
.SetAbsoluteExpiration(expirationTimespan)
|
||||
.AddExpirationToken(expirationToken);
|
||||
|
||||
_downloadingAgents.Set(downloadId, string.Empty, cacheOptions);
|
||||
|
||||
var waitTime = DateTimeOffset.Now - startWait;
|
||||
|
||||
downloadingAgents.Set(downloadId, string.Empty, TimeSpan.FromMinutes(10));
|
||||
|
||||
DataService.WriteEvent($"Download started after wait time of {waitTime}. " + "" +
|
||||
$"Current Downloads: {downloadingAgents.Count}. Max Allowed: {AppConfig.MaxConcurrentUpdates}", EventType.Debug, null);
|
||||
$"Current Downloads: {_downloadingAgents.Count}. Max Allowed: {AppConfig.MaxConcurrentUpdates}", EventType.Debug, null);
|
||||
|
||||
|
||||
string filePath;
|
||||
@ -81,7 +95,7 @@ namespace Remotely.Server.API
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
downloadingAgents.Remove(downloadId);
|
||||
_downloadingAgents.Remove(downloadId);
|
||||
DataService.WriteEvent(ex, null);
|
||||
return StatusCode((int)HttpStatusCode.InternalServerError);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user