mirror of https://github.com/Squidex/squidex.git
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.
48 lines
1.4 KiB
48 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;
|
|
using NodaTime;
|
|
|
|
namespace Squidex.Infrastructure
|
|
{
|
|
public sealed class RetryWindow
|
|
{
|
|
private readonly Duration windowDuration;
|
|
private readonly int windowSize;
|
|
private readonly Queue<Instant> retries = new Queue<Instant>();
|
|
private readonly IClock clock;
|
|
|
|
public RetryWindow(TimeSpan windowDuration, int windowSize, IClock clock = null)
|
|
{
|
|
this.windowDuration = Duration.FromTimeSpan(windowDuration);
|
|
this.windowSize = windowSize + 1;
|
|
|
|
this.clock = clock ?? SystemClock.Instance;
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
retries.Clear();
|
|
}
|
|
|
|
public bool CanRetryAfterFailure()
|
|
{
|
|
var now = clock.GetCurrentInstant();
|
|
|
|
retries.Enqueue(now);
|
|
|
|
while (retries.Count > windowSize)
|
|
{
|
|
retries.Dequeue();
|
|
}
|
|
|
|
return retries.Count < windowSize || (retries.Count > 0 && (now - retries.Peek()) > windowDuration);
|
|
}
|
|
}
|
|
}
|
|
|