Path: redb-route/redb.Route.GenericFile/PollBackoff.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.GenericFile/PollBackoff.cs
----
namespace redb.Route.GenericFile;
/// Outcome of a single poll, feeding the backoff counters.
public enum PollOutcome
{
/// The poll itself threw (e.g. the server is unreachable, no directory permission).
Error,
/// The poll completed but created no exchange (empty directory, or every file was filtered out).
Idle,
/// The poll created at least one exchange.
Success
}
/// Result of processing a single polled file, aggregated into the poll tally.
public enum FileProcessResult
{
/// No exchange was created (age / done-file / read-lock / idempotent duplicate / unreadable).
Skipped,
/// An exchange was created and the route completed without an unhandled failure.
Processed,
/// An exchange was created but the route left an unhandled failure on it.
Failed
}
/// Per-poll tally: how many exchanges were created and how many of those failed unhandled.
public readonly record struct PollTally(int Created, int Failed);
/// Set when a poll's outcome has just armed the skip countdown — for a single log line.
public readonly record struct BackoffTrigger(bool ByError, int Skips);
///
/// Apache Camel ScheduledPollConsumer-style poll backoff: after a threshold number of consecutive
/// idle or error polls, skip the next multiplier polls, then reset and resume. Pure and timer-free —
/// the caller drives the loop and applies the delay.
///
/// Counters follow Camel: an error poll increments the error count and clears idle; an idle poll increments
/// idle; a successful poll clears both. When a configured threshold is met the countdown is armed to
/// multiplier; the countdown is consumed one skip at a time and resets the counters on its last unit.
///
///
public sealed class PollBackoff
{
private readonly int _multiplier;
private readonly int _idleThreshold;
private readonly int _errorThreshold;
private int _idle;
private int _error;
private int _countdown;
/// Creates a backoff with the given multiplier and idle/error thresholds (0 = that part disabled).
public PollBackoff(int multiplier, int idleThreshold, int errorThreshold)
{
_multiplier = multiplier;
_idleThreshold = idleThreshold;
_errorThreshold = errorThreshold;
}
/// True when a multiplier and at least one threshold are configured (otherwise a no-op).
public bool Enabled => _multiplier > 0 && (_idleThreshold > 0 || _errorThreshold > 0);
///
/// Classifies a completed (non-throwing) poll from its tally. No exchange → .
/// When is set and every created exchange failed unhandled →
/// (the Camel-superset behaviour); otherwise → .
/// A poll whose own call threw is classified by the caller, not here.
///
public static PollOutcome Classify(PollTally tally, bool backoffOnFailedExchanges)
{
if (tally.Created == 0)
return PollOutcome.Idle;
if (backoffOnFailedExchanges && tally.Failed == tally.Created)
return PollOutcome.Error;
return PollOutcome.Success;
}
///
/// Called before each cycle. Returns true when this cycle must be skipped (no poll). Consumes one unit
/// of the armed countdown; on the last skip it resets the counters so polling resumes on the next cycle.
///
public bool ShouldSkip()
{
if (_countdown <= 0)
return false;
_countdown--;
if (_countdown == 0)
{
_idle = 0;
_error = 0;
}
return true;
}
///
/// Records the outcome of an actual poll. Returns a when this outcome just
/// armed the skip countdown (so the caller can log one line), otherwise null.
///
public BackoffTrigger? Record(PollOutcome outcome)
{
if (!Enabled)
return null;
switch (outcome)
{
case PollOutcome.Error:
_error++;
_idle = 0;
break;
case PollOutcome.Idle:
_idle++;
break;
case PollOutcome.Success:
_idle = 0;
_error = 0;
break;
}
var errorHit = _errorThreshold > 0 && _error >= _errorThreshold;
var idleHit = _idleThreshold > 0 && _idle >= _idleThreshold;
if (errorHit || idleHit)
{
_countdown = _multiplier;
return new BackoffTrigger(errorHit, _multiplier);
}
return null;
}
}