Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

47 lines
1.4 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschränkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Collections.Generic;
namespace Squidex.Infrastructure
{
public sealed class RetryWindow
{
private readonly TimeSpan windowDuration;
private readonly int windowSize;
private readonly Queue<DateTime> retries = new Queue<DateTime>();
public RetryWindow(TimeSpan windowDuration, int windowSize)
{
this.windowDuration = windowDuration;
this.windowSize = windowSize + 1;
}
public void Reset()
{
retries.Clear();
}
public bool CanRetryAfterFailure()
{
return CanRetryAfterFailure(DateTime.UtcNow);
}
public bool CanRetryAfterFailure(DateTime utcNow)
{
retries.Enqueue(utcNow);
while (retries.Count > windowSize)
{
retries.Dequeue();
}
return retries.Count < windowSize || (retries.Count > 0 && (utcNow - retries.Peek()) > windowDuration);
}
}
}