You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

APIM策略:基于请求参数限流的自定义错误消息异常排查

基于请求体username的APIM限流+自定义倒计时错误消息实现方案

核心问题定位

你遇到的异常是因为自定义错误响应的逻辑干扰了APIM限流策略的状态跟踪机制,或是on-error段处理未正确关联限流剩余时间参数,同时默认限流错误输出被后续高优先级逻辑覆盖。

正确实现步骤

1. 提取请求体中的username参数

在入站策略中解析请求体并提取username,注意保留请求体避免影响后续流程:

<inbound>
    <base />
    <!-- 仅针对带请求体的方法解析参数 -->
    <choose>
        <when condition="@(context.Request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase) || context.Request.Method.Equals("PUT", StringComparison.OrdinalIgnoreCase))">
            <set-variable name="requestBody" value="@(context.Request.Body.As<string>(preserveContent: true))" />
            <set-variable name="username" value="@(Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(context.Variables.GetValueOrDefault<string>("requestBody")).username)" />
        </when>
    </choose>
</inbound>

2. 配置基于username的限流策略

使用rate-limit-by-key策略,指定限流key为提取的username,同时存储剩余周期时间:

<inbound>
    <!-- 接续上述请求体解析逻辑 -->
    <rate-limit-by-key calls="1" renewal-period="60" counter-key="@(context.Variables.GetValueOrDefault<string>("username", "default-user"))" remaining-variable-name="remainingTime" />
</inbound>
- `calls="1"`:每分钟允许1次调用(可按需调整)
- `renewal-period="60"`:限流周期为60秒
- `remaining-variable-name="remainingTime"`:存储当前周期剩余秒数

3. 在on-error段处理自定义429响应

仅针对限流触发的429错误做自定义处理,利用remainingTime返回倒计时:

<on-error>
    <choose>
        <!-- 只处理限流触发的429错误 -->
        <when condition="@(context.Response.StatusCode == 429 && context.LastError.Reason == "RateLimitExceeded")">
            <set-status code="429" reason="Too Many Requests" />
            <set-header name="Retry-After" exists-action="override" value="@(context.Variables.GetValueOrDefault<int>("remainingTime").ToString())" />
            <set-body>@{
                var remainingSeconds = context.Variables.GetValueOrDefault<int>("remainingTime");
                return $"{{\"error\": \"请求过于频繁,请{remainingSeconds}秒后重试\"}}";
            }</set-body>
            <set-header name="Content-Type" exists-action="override" value="application/json" />
        </when>
        <!-- 其他错误保持默认处理逻辑 -->
        <otherwise>
            <base />
        </otherwise>
    </choose>
</on-error>

4. 规避常见坑点

  • 不要在<rate-limit-by-key>内部直接修改响应,会破坏APIM的限流状态跟踪
  • 必须设置preserveContent: true保留请求体,防止后续策略无法读取
  • 给username变量设置默认值(如"default-user"),避免请求体无该参数时限流失效
  • on-error段必须加条件判断,只处理限流触发的429,避免干扰其他错误的正常返回

验证逻辑

  • 首次请求:返回200,限流计数+1
  • 10秒内二次请求:触发限流,返回带50秒倒计时的自定义JSON错误
  • 周期内后续请求:持续返回自定义429,倒计时随时间递减
  • 周期结束后:限流计数重置,可正常请求返回200

内容的提问来源于stack exchange,提问作者Dark S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 03:22:39