feat: 设置与保留期清理(里程碑 5)
- settings.yaml:审计开关(默认开)+ 保留天数(默认 90、最小 7),FormKit 声明式 - plugin.yaml 声明 settingName/configMapName(源码级核对 Plugin.spec 字段名) - AuditSettingProvider:60s TTL 缓存读取(D6),失败/缺省回退默认且默认值同样入缓存 - AuditRecorder 写入前检查开关;设置故障按默认配置继续记录(审计设置故障不影响捕获) - RetentionCleaner:Reactor 调度(风险项 #3 预案,未验证 @Scheduled 不赌插件上下文), 启动补跑一次 + 每日 03:00;按时间索引升序每批 200 循环至不足一批(D5) - 插件 start/stop 联动清理器生命周期 - 新增 17 个测试:TTL 命中/过期重取/降级、批量删除/边界/调度时机、开关跳过
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
package run.halo.operationaudit.housekeeping;
|
||||
|
||||
import java.time.Duration;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Mono;
|
||||
import run.halo.app.plugin.ReactiveSettingFetcher;
|
||||
|
||||
/**
|
||||
* 审计设置的短 TTL 缓存读取(技术方案 D6)。
|
||||
*
|
||||
* <p>开关变更最多延迟 1 分钟生效——自用场景可接受,避免为实时生效引入 ConfigMap 监听器。
|
||||
* 读取失败或未配置时静默回退默认值(默认值同样进入缓存,避免配置持续损坏时
|
||||
* 每个请求都访问存储),绝不影响捕获与清理流程。
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class AuditSettingProvider {
|
||||
|
||||
/** 缓存有效期。 */
|
||||
public static final Duration TTL = Duration.ofSeconds(60);
|
||||
|
||||
private final ReactiveSettingFetcher fetcher;
|
||||
|
||||
/** TTL 缓存条目;包内可见以便测试构造过期条目。 */
|
||||
record CacheEntry(AuditSettings value, long expireAtNanos) {
|
||||
boolean expiredAt(long nowNanos) {
|
||||
return nowNanos >= expireAtNanos;
|
||||
}
|
||||
}
|
||||
|
||||
volatile CacheEntry cache;
|
||||
|
||||
/**
|
||||
* 获取当前设置(TTL 内直接命中缓存)。
|
||||
*
|
||||
* <p>返回的 Mono 永不为空、永不报错:任何异常均回退默认值。
|
||||
*/
|
||||
public Mono<AuditSettings> get() {
|
||||
var entry = cache;
|
||||
if (entry != null && !entry.expiredAt(System.nanoTime())) {
|
||||
return Mono.just(entry.value());
|
||||
}
|
||||
return fetcher.fetch(AuditSettings.GROUP, AuditSettings.class)
|
||||
.onErrorResume(e -> {
|
||||
log.warn("Failed to load audit settings, fall back to defaults", e);
|
||||
return Mono.just(AuditSettings.defaults());
|
||||
})
|
||||
.defaultIfEmpty(AuditSettings.defaults())
|
||||
.doOnNext(value -> cache = new CacheEntry(value, System.nanoTime() + TTL.toNanos()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user