smart-poll-loop
它能做什么
这个技能描述一种轮询循环:服务持续失败时放慢检查频率,成功后恢复加快。适合经常检查外部服务的开发者。
适用场景
- ✓当你按计时器检查外部服务,而它偶尔会失败时。
- ✓当重复的失败请求可能压垮一个正在苦苦支撑的服务时。
- ✓当你希望服务恢复后检查立即恢复正常速度时。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/smart-poll-loop/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我每分钟检查一次服务器状态,但在它持续失败时放慢检查频率。”
每周 5 个新技能,1 分钟读完。
工作原理
每次失败后,等待时间会翻倍,直到达到设定的上限。成功一次后,循环恢复到正常间隔。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Smart Poll Loop Pattern
Overview
The Smart Poll Loop pattern implements resilient, adaptive polling for periodic data refresh operations. It combines exponential backoff on consecutive failures with automatic recovery on success, ensuring system resources are protected during outages while maintaining responsiveness when services are healthy.
This pattern is essential for production applications that need to poll external services or APIs continuously but must gracefully handle intermittent failures, rate limits, or temporary service disruptions without overwhelming the system or degrading user experience.
Key Patterns Identified
Pattern 1: Exponential Backoff on Failure
Purpose: Prevent overwhelming failing services with repeated requests while giving them time to recover.
Implementation: Each consecutive failure doubles the polling interval up to a configurable maximum multiplier. This creates exponentially increasing delays: 1x → 2x → 4x → 8x (default max).
Key Elements:
consecutiveFailures: Counter tracking sequential failurescurrentBackoffMultiplier: Current delay multiplier (starts at 1)maxBackoffMultiplier: Upper bound to prevent infinite delays (typically 4-8x)- Boolean return from callbacks (
true= success,false= failure)
Code Pattern:
// Track failure state per runner
interface RunnerEntry {
consecutiveFailures: number;
currentBackoffMultiplier: number;
intervalMs: number; // base interval
// ... other fields
}
// On failure, increase backoff
if (!success) {
entry.consecutiveFailures++;
const newMultiplier = Math.min(
Math.pow(2, entry.consecutiveFailures),
maxBackoffMultiplier
);
entry.currentBackoffMultiplier = newMultiplier;
// Reschedule with new interval
const effectiveInterval = entry.intervalMs * entry.currentBackoffMultiplier;
scheduleNextRun(effectiveInterval);
}
Pattern 2: Automatic Recovery on Success
Purpose: Immediately restore normal polling frequency when a service recovers, ensuring fresh data flows without unnecessary delay.
Implementation: On any successful callback execution, reset both the failure counter and backoff multiplier to their initial states (0 and 1), then reschedule at the base interval.
Key Elements:
- Reset logic triggered by
success === trueor no error thrown - Immediate rescheduling at base interval (no waiting for current timer)
- Logging state transitions for observability
Code Pattern:
if (success) {
// Auto-recovery: reset backoff and failure count
if (entry.consecutiveFailures > 0 || entry.currentBackoffMultiplier > 1) {
entry.consecutiveFailures = 0;
entry.currentBackoffMultiplier = 1;
// Immediately reschedule at normal interval
if (!paused && entry.timer) {
scheduleNextRun(name, entry); // uses base intervalMs
}
}
}
Pattern 3: Failure Detection via Boolean Return or Exception
Purpose: Provide flexible signaling for success/failure states, supporting both explicit boolean returns and exception-based error handling.
Implementation: Callbacks can return boolean (explicit success/failure), void (success assumed), or throw exceptions (failure). All three mechanisms trigger the same backoff/recovery logic.
Key Elements:
- Type signature:
() => Promise<boolean | void> true= explicit success, triggers recoveryfalse= explicit failure, triggers backoffvoid/undefined= implicit success (no news is good news)- Exception thrown = failure, triggers backoff
Code Pattern:
let success = true;
try {
const result = await entry.fn();
// Boolean return indicates explicit success/failure
if (typeof result === 'boolean') {
success = result;
}
// void/undefined is treated as success
if (success) {
// trigger recovery...
} else {
// trigger backoff...
}
} catch (err) {
// Exception is also treated as failure
success = false;
// trigger backoff...
}
Pattern 4: Visibility-Aware Scheduling
Purpose: Pause or slow down polling when the page is hidden to conserve resources, then resume or refresh immediately when visible.
Implementation: Listen to visibilitychange events. On hide, clear all timers. On show, either flush stale data (if enough time passed) or resume normal polling.
Key Elements:
document.hiddenstate checkpauseWhenHiddenflag (full stop vs slower polling
常见问题
等待时间最长能到多少?
技能会设置一个最大倍数,通常是正常间隔的四到八倍。
一次成功会重置等待时间吗?
会。一次成功的检查就会让循环恢复到正常速度。
失败多次后它会停止尝试吗?
循环会以最长的等待时间继续运行,因此服务恢复后它会重新开始检查。
详情
作者的原始描述
Adaptive polling pattern with exponential backoff on failure, automatic recovery on success, and visibility-aware scheduling
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #13082
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。