Hyperswitch API 返回 429 时如何区分速率限制与 API 对象锁定
【免费下载链接】hyperswitchOpen source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation项目地址: https://gitcode.com/GitHub_Trending/hy/hyperswitch
调用 Hyperswitch API 时,如果你收到 HTTP 429 响应,它并不一定意味着你的请求量超过了速率限制。Hyperswitch 的 429 有两种成因:一种是 API 速率限制(rate limit)被触发,另一种是 API 对象锁定(API locking)——某个对象正在被其他 API 请求或 Hyperswitch 进程占用,系统主动限制了访问。两者的处理策略不同,区分方法在于查看响应中的错误消息文本。
确认当前的速率限制基线
根据 速率限制文档,所有 Hyperswitch API 的默认速率限制为每秒 80 个请求。如果你的业务在短时间内连续发送大量请求,就可能遇到 429 错误。如果业务需要更高的限制,文档给出的途径是联系 biz@hyperswitch.io。
也就是说,判断是否属于速率限制的第一个依据:你的请求量是否接近或超过 80 req/s 这个默认值。
识别 API 对象锁定:看错误消息原文
如果 429 响应体中的错误消息是下面这段原文,那么它是由 API 对象锁定引起的,不是速率限制:
At this moment, access to this object is restricted due to ongoing utilization by another API request or an ongoing Hyperswitch process. Retry after some time if this error persists.这是文档中明确给出的锁定错误消息。API 对象锁定是 Hyperswitch 的一项保护机制:在部分操作中对对象加锁,防止并发工作负载导致结果不一致。锁定触发后,系统会暂停对该对象的访问,直到占用结束。
判断流程很简单,两步:
- 收到 429 后,先读取响应体中的错误消息;
- 错误消息与上面这段锁定提示一致 → 对象锁定,稍后重试;消息不同且请求量接近每秒 80 个 → 按速率限制处理。
另外,在 错误码文档 中,RATE_LIMIT错误码的说明是 "API rate limit exceeded",归类为 "Issue with integration"(UE_4000),用户侧提示为 "Something went wrong. Try another payment method or contact your bank"。这可以作为区分"速率限制"这一类错误的参照。
两种情况的处理策略
速率限制触发的 429
文档给出的处理建议:
- 实现重试机制,且重试间隔逐步递增(progressively increasing intervals),避免用重复请求继续压垮系统;
- 借助监控工具跟踪用量模式,必要时据此调整速率限制。
对象锁定触发的 429
错误消息本身已给出处理方向:稍后重试(Retry after some time if this error persists)。锁定是暂时性的,由并发操作引起,对象被释放后即可正常访问;这种情况下单纯提高重试频率没有意义,等待占用结束才是关键。
小结
遇到 429 时先读错误消息:消息是 "At this moment, access to this object is restricted due to ongoing utilization by another API request or an ongoing Hyperswitch process." 就按对象锁定处理,稍后重试;否则对照默认每秒 80 个请求的限制,用递增间隔的重试加用量监控来处理。更多细节见 api-reference/essentials/rate_limit.mdx 与 api-reference/essentials/error_codes.mdx。
【免费下载链接】hyperswitchOpen source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation项目地址: https://gitcode.com/GitHub_Trending/hy/hyperswitch
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考