Skip to content

Commit

Permalink
Better limits on the number of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Sep 7, 2024
1 parent 3258297 commit 9e84481
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 46 deletions.
1 change: 0 additions & 1 deletion dkgCommon/Models/StatusResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
// POSSIBILITY OF SUCH DAMAGE.

using dkgCommon.Constants;
using System.Threading.Tasks;

namespace dkgCommon.Models
{
Expand Down
9 changes: 5 additions & 4 deletions dkgServiceNode/Controllers/RoundsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,13 @@ public async Task<ActionResult<Round>> CancelRound(int id)
internal async Task TryRunRound(Round round)
{
List<Node> rNodes = dkgContext.GetAllNodes()
.Where(n => n.RoundId == round.Id)
.ToList();
.Where(n => n.RoundId == round.Id && n.Status == NStatus.WaitingRoundStart)
.ToList();


List<Node> fiNodes = rNodes
.Where(node => dkgContext.CheckNodeQualification(node.Id, round.Id - 1))
.ToList();
.Where(node => dkgContext.CheckNodeQualification(node.Id, round.Id - 1))
.ToList();

if (fiNodes.Count < 3)
{
Expand Down
1 change: 1 addition & 0 deletions dkgServiceNode/Data/DbEnsure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ public static void Ensure(string connectionString)
PuVersionUpdate("0.10.2", connection);
PuVersionUpdate("0.11.0", connection);
EnsureVersion("0.12.1", sqlScript_0_12_1, connection);
PuVersionUpdate("0.12.2", connection);
}
}

Expand Down
123 changes: 89 additions & 34 deletions dkgServiceNode/Data/DkgContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,57 @@ public class DkgContext : DbContext
private readonly NodesCache nodesCache;
private readonly RoundsCache roundsCache;
private readonly NodesRoundHistoryCache nodesRoundHistoryCache;

private readonly ILogger logger;
public DkgContext(DbContextOptions<DkgContext> options,
NodesCache nc,
RoundsCache rc,
NodesRoundHistoryCache nrhc) : base(options)
NodesRoundHistoryCache nrhc,
ILogger<DkgContext> lggr) : base(options)
{
nodesCache = nc;
roundsCache = rc;
nodesRoundHistoryCache = nrhc;
if (Nodes is not null)
{
nodesCache.LoadNodesToCache(Nodes);
}
if (Rounds is not null)

logger = lggr;

try
{
roundsCache.LoadRoundsToCache(Rounds);
if (Nodes is not null)
{
nodesCache.LoadNodesToCache(Nodes);
}
if (Rounds is not null)
{
roundsCache.LoadRoundsToCache(Rounds);
}
if (NodesRoundHistory is not null)
{
nodesRoundHistoryCache.LoadNodesRoundHistoriesToCache(NodesRoundHistory);
}
}
if (NodesRoundHistory is not null)
catch (Exception ex)
{
nodesRoundHistoryCache.LoadNodesRoundHistoriesToCache(NodesRoundHistory);
logger.LogError("Error loading caches: {msg}", ex.Message);
}
}
public Node? GetNodeById(int id) => nodesCache.GetNodeById(id);
public Node? GetNodeByPublicKey(string publicKey) => nodesCache.GetNodeByPublicKey(publicKey);
public Node? GetNodeByAddress(string address) => nodesCache.GetNodeByAddress(address);
public async Task AddNodeAsync(Node node)
{
Nodes.Add(node);
await SaveChangesAsync();
nodesCache.LoadNodeToCache(node);
await LoadNodesRoundHistory(node);
nodesRoundHistoryCache.UpdateNodeCounts(null, NStatus.NotRegistered, node.RoundId, node.Status);
try
{
Nodes.Add(node);
await SaveChangesAsync();
nodesCache.LoadNodeToCache(node);
await LoadNodesRoundHistory(node);
nodesRoundHistoryCache.UpdateNodeCounts(null, NStatus.NotRegistered, node.RoundId, node.Status);
}
catch (Exception ex)
{
logger.LogError("Error adding node: {msg}", ex.Message);
}
}

private async Task LoadNodesRoundHistory(Node node)
Expand All @@ -90,44 +110,79 @@ await Database.ExecuteSqlRawAsync(
public List<Node> GetFilteredNodes(string search = "") => nodesCache.GetFilteredNodes(search);
public async Task UpdateNodeAsync(Node node)
{
NodesRoundHistory? nrh = GetLastNodeRoundHistory(node.Id, -1);
nodesRoundHistoryCache.UpdateNodeCounts(nrh?.RoundId, nrh != null ? nrh.NodeFinalStatus : NStatus.NotRegistered,
node.RoundId, node.Status);
try
{
NodesRoundHistory? nrh = GetLastNodeRoundHistory(node.Id, -1);
nodesRoundHistoryCache.UpdateNodeCounts(nrh?.RoundId, nrh != null ? nrh.NodeFinalStatus : NStatus.NotRegistered,
node.RoundId, node.Status);

Entry(node).State = EntityState.Modified;
await SaveChangesAsync();
await LoadNodesRoundHistory(node);
nodesCache.UpdateNodeInCache(node);
Entry(node).State = EntityState.Modified;
await SaveChangesAsync();
await LoadNodesRoundHistory(node);
nodesCache.UpdateNodeInCache(node);
}
catch (Exception ex)
{
logger.LogError("Error updating node: {msg}", ex.Message);
}
}

public async Task DeleteNodeAsync(Node node)
{
nodesRoundHistoryCache.UpdateNodeCounts(node.RoundId, node.Status, null, NStatus.NotRegistered);
Nodes.Remove(node);
await SaveChangesAsync();
nodesCache.DeleteNodeFromCache(node);
try
{
nodesRoundHistoryCache.UpdateNodeCounts(node.RoundId, node.Status, null, NStatus.NotRegistered);
Nodes.Remove(node);
await SaveChangesAsync();
nodesCache.DeleteNodeFromCache(node);
}
catch (Exception ex)
{
logger.LogError("Error deleting node: {msg}", ex.Message);
}
}

public Round? GetRoundById(int id) => roundsCache.GetRoundById(id);
public List<Round> GetAllRounds() => roundsCache.GetAllRounds();
public List<Round> GetAllRoundsSortedByIdDescending() => roundsCache.GetAllRoundsSortedByIdDescending();
public async Task AddRoundAsync(Round round)
{
Rounds.Add(round);
await SaveChangesAsync();
roundsCache.AddRoundToCache(round);
try
{
Rounds.Add(round);
await SaveChangesAsync();
roundsCache.AddRoundToCache(round);
}
catch (Exception ex)
{
logger.LogError("Error adding round: {msg}", ex.Message);
}
}
public async Task UpdateRoundAsync(Round round)
{
Rounds.Update(round);
await SaveChangesAsync();
roundsCache.UpdateRoundInCache(round);
try
{
Rounds.Update(round);
await SaveChangesAsync();
roundsCache.UpdateRoundInCache(round);
}
catch (Exception ex)
{
logger.LogError("Error updating round: {msg}", ex.Message);
}
}
public async Task DeleteRoundAsync(Round round)
{
Rounds.Remove(round);
await SaveChangesAsync();
roundsCache.DeleteRoundFromCache(round.Id);
try
{
Rounds.Remove(round);
await SaveChangesAsync();
roundsCache.DeleteRoundFromCache(round.Id);
}
catch (Exception ex)
{
logger.LogError("Error deleting round: {msg}", ex.Message);
}
}
public bool RoundExists(int id) => roundsCache.RoundExists(id);
public int? LastRoundResult() => roundsCache.LastRoundResult();
Expand Down
3 changes: 2 additions & 1 deletion dkgServiceNode/Services/Cache/NodesCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class NodesCache

public void LoadNodesToCache(IEnumerable<Node> nodes)
{
if (_isCacheNodesLoaded) return;
lock (_cacheNodesLock)
{
if (!_isCacheNodesLoaded)
Expand Down Expand Up @@ -150,7 +151,7 @@ private List<Node> GetFilteredNodesInternal(string search)
(kvp.Value.RoundId != null && kvp.Value.RoundId.ToString()!.Contains(search)) ||
(kvp.Value.RoundId == null && ("null".Contains(search) || "--".Contains(search))) ||
NodeStatusConstants.GetNodeStatusById(kvp.Value.StatusValue).ToString().Contains(search))
.Select(kvp => new Node(kvp.Value)) // Copy the filtered nodes
.Select(kvp => new Node(kvp.Value))
.ToList();
}
return filteredNodes;
Expand Down
1 change: 1 addition & 0 deletions dkgServiceNode/Services/Cache/NodesRoundHistoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class NodesRoundHistoryCache

public void LoadNodesRoundHistoriesToCache(IEnumerable<NodesRoundHistory> histories)
{
if (_isCacheNodesRoundHistoryLoaded) return;
lock (_cacheNodesRoundHistoryLock)
{
if (!_isCacheNodesRoundHistoryLoaded)
Expand Down
1 change: 1 addition & 0 deletions dkgServiceNode/Services/Cache/RoundsCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class RoundsCache

public void LoadRoundsToCache(IEnumerable<Round> rounds)
{
if (_isCacheRoundsLoaded) return;
lock (_cacheRoundsLock)
{
if (!_isCacheRoundsLoaded)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ public RequestLimitingMiddleware(RequestDelegate next, int maxConcurrentRequests

public async Task InvokeAsync(HttpContext context)
{
await _semaphore.WaitAsync();

try
if (context.Request.Path.StartsWithSegments($"/api/ops", StringComparison.OrdinalIgnoreCase))
{
await _next(context);
await _semaphore.WaitAsync();

try
{
await _next(context);
}
finally
{
_semaphore.Release();
}
}
finally
else
{
_semaphore.Release();
// If not the specific controller, just pass through
await _next(context);
}
}
}

0 comments on commit 9e84481

Please sign in to comment.