Feishu/Lark API rate limit handling strategy. Automatically activates during Feishu API calls to implement smart interval control and 429 error handling. Essential for batch operations, document writes, and bitable operations.
针对飞书/Lark API的智能速率限制处理。避免429错误,确保批量操作成功完成。
| 情况 | 操作 |
|---|---|
| API调用返回429错误 | 解析Retry-After头,等待,重试(最多3次) |
| 批量写入操作 |
在以下情况下激活此技能:
| 限制类型 | 配额 | 描述 |
|---|---|---|
| 每分钟 | 100次调用/分钟/应用 | 应用级分钟限制 |
| 每天 |
| API类型 | 特殊限制 |
|---|---|
| 文档写入 | 更严格,建议2-3秒间隔 |
| 多维表格批量写入 |
官方文档:https://open.feishu.cn/document/platform-notices/platform-updates-/custom-app-api-call-limit
初始间隔:1秒
最小间隔:1秒
最大间隔:10秒
警告阈值:50次调用/分钟
python
class FeishuRateLimiter:
飞书API速率限制器
def init(self):
self.base_interval = 1.0 # 基础间隔1秒
self.current_interval = 1.0 # 当前间隔
self.max_interval = 10.0 # 最大间隔
self.recent_calls = [] # 近期调用记录
self.window_size = 60 # 统计窗口60秒
self.max_retries = 3 # 最大重试次数
def before_call(self):
调用前等待
time.sleep(self.current_interval)
self.recent_calls.append(time.time())
self.adjustinterval()
def adjustinterval(self):
根据近期调用频率动态调整间隔
now = time.time()
# 统计最近60秒内的调用次数
recent = [t for t in self.recentcalls if now - t < self.windowsize]
self.recent_calls = recent
call_count = len(recent)
# 根据频率调整间隔
if call_count > 50: # 接近限制(100次/分钟)
self.currentinterval = min(self.currentinterval * 1.5, self.max_interval)
elif call_count > 30:
self.currentinterval = min(self.currentinterval * 1.2, self.max_interval)
elif callcount < 10 and self.currentinterval > self.base_interval:
# 调用不频繁时减少间隔
self.currentinterval = max(self.currentinterval * 0.9, self.base_interval)
def onratelimit(self, retry_after=None):
遇到429错误时调用
if retry_after:
waittime = retryafter
else:
waittime = self.currentinterval * 2
self.currentinterval = min(waittime, self.max_interval)
time.sleep(self.current_interval)
def on_success(self):
成功后逐渐恢复正常间隔
if self.currentinterval > self.baseinterval:
self.currentinterval = max(self.currentinterval * 0.8, self.base_interval)
遇到429错误时:
markdown
❌ 错误做法:
一次性写入100条记录
✅ 正确做法:
for batch in chunks(records, 10):
for record in batch:
feishubitablecreate_record(record)
sleep(1) # 每条记录间隔1秒
sleep(5) # 批次间额外等待5秒
markdown
优先级:
markdown
对于频繁查询的数据:
┌─────────────────┐
│ API调用 │
└────────┬────────┘
▼
┌─────────────────┐
│ 检查响应 │
└────────┬────────┘
▼
┌────┴────┐
│ 429? │
└────┬────┘
│
┌────┴────────────────────────────┐
│ 是 │ 否
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ 检查Retry-After │ │ 继续执行 │
└────────┬────────┘ └─────────────────┘
▼
┌─────────────────┐
│ 等待指定时间 │
│ 或间隔×2 │
└────────┬────────┘
▼
┌─────────────────┐
│ 重试次数 < 3? │
└────────┬────────┘
│
┌────┴────┐
│ 是 │ 否
▼ ▼
┌─────────┐ ┌─────────────────┐
│ 重试 │ │ 记录错误,通知 │
└─────────┘ └─────────────────┘
yaml
feishuratelimit:
enabled: true
baseintervalms: 1000 # 基础间隔1秒
maxintervalms: 10000 # 最大间隔10秒
max_retries: 3 # 最大重试次数
windowsizesec: 60 # 统计窗口60秒
warning_threshold: 50 # 警告阈值(次/分钟)
batch_size: 10 # 批次大小
batchdelayms: 5000 # 批次间延迟
markdown
任务:创建200条记录
策略:
实现:
第1批:10条记录 → 等待5秒
第2批:10条记录 → 等待5秒
...
第20批:10条记录 → 完成
markdown
任务:向文档写入50个段落
策略:
注意:
markdown
任务:读取100条记录 + 更新50条记录
策略:
建议:
| 实践 | 描述 |
|---|---|
| 估算调用量 | 在批量操作前计算API调用次数 |
| 分批执行 |
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 feishu-rate-limit-1776153181 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 feishu-rate-limit-1776153181 技能
skillhub install feishu-rate-limit-1776153181
文件大小: 3.9 KB | 发布时间: 2026-4-17 14:49