28 lines
907 B
C
28 lines
907 B
C
// Service restart budget: three automatic attempts in any sliding five-minute interval.
|
|
|
|
#pragma once
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
typedef struct INIT_RESTART_BUDGET {
|
|
uint64_t Attempts[3];
|
|
uint32_t Count;
|
|
} INIT_RESTART_BUDGET;
|
|
|
|
static inline bool InitConsumeRestartBudget(INIT_RESTART_BUDGET* budget,
|
|
uint64_t now, uint64_t frequency) {
|
|
if (!budget || !frequency || budget->Count > 3u ||
|
|
frequency > UINT64_MAX / 300u) return false;
|
|
uint64_t window = frequency * 300u;
|
|
uint32_t keep = 0;
|
|
for (uint32_t i = 0; i < budget->Count; ++i) {
|
|
uint64_t started = budget->Attempts[i];
|
|
if (now >= started && now - started < window)
|
|
budget->Attempts[keep++] = started;
|
|
}
|
|
budget->Count = keep;
|
|
if (keep == 3u) return false;
|
|
budget->Attempts[budget->Count++] = now;
|
|
return true;
|
|
}
|