content: /*
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
   ~                                                                               ~
   ~ The MIT License (MIT)                                                         ~
   ~                                                                               ~
   ~ Copyright (c) 2015-2025 miaixz.org and other contributors.                    ~
   ~                                                                               ~
   ~ Permission is hereby granted, free of charge, to any person obtaining a copy  ~
   ~ of this software and associated documentation files (the "Software"), to deal ~
   ~ in the Software without restriction, including without limitation the rights  ~
   ~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     ~
   ~ copies of the Software, and to permit persons to whom the Software is         ~
   ~ furnished to do so, subject to the following conditions:                      ~
   ~                                                                               ~
   ~ The above copyright notice and this permission notice shall be included in    ~
   ~ all copies or substantial portions of the Software.                           ~
   ~                                                                               ~
   ~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    ~
   ~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      ~
   ~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   ~
   ~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        ~
   ~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ~
   ~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN     ~
   ~ THE SOFTWARE.                                                                 ~
   ~                                                                               ~
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  */
  package org.miaixz.bus.vortex.filter;
  
  import java.util.HashSet;
  import java.util.Set;
  import org.miaixz.bus.vortex.Context;
  import org.miaixz.bus.vortex.Format;
  import org.miaixz.bus.vortex.magic.Limiter;
  import org.miaixz.bus.vortex.registry.LimiterRegistry;
  import org.springframework.core.Ordered;
  import org.springframework.core.annotation.Order;
  import org.springframework.web.server.ServerWebExchange;
  import org.springframework.web.server.WebFilterChain;
  import reactor.core.publisher.Mono;
  
  /**
   * 限流过滤器，基于令牌桶算法对请求进行流量限制
   *
   * @author Justubborn
   * @since Java 17+
   */
  @Order(Ordered.HIGHEST_PRECEDENCE + 3)
  public class LimitFilter extends AbstractFilter {
  
      private final LimiterRegistry registry;
  
      /**
       * 构造器，初始化限流注册表
       *
       * @param registry 限流注册表
       */
      public LimitFilter(LimiterRegistry registry) {
          this.registry = registry;
      }
  
      /**
       * 内部过滤方法，执行限流逻辑
       *
       * @param exchange 当前的 ServerWebExchange 对象
       * @param chain    过滤器链
       * @param context  请求上下文
       * @return {@link Mono<Void>} 表示异步处理完成
       */
      @Override
      protected Mono<Void> doFilter(ServerWebExchange exchange, WebFilterChain chain, Context context) {
          String ip = getRequestMap(context).get("x-remote_ip");
          String methodVersion = getAssets(context).getMethod() + getAssets(context).getVersion();
          Set<Limiter> cfgList = getLimiter(methodVersion, ip);
          for (Limiter cfg : cfgList) {
              cfg.acquire();
          }
          Format.info(
                  exchange,
                  "RATE_LIMIT_APPLIED",
                  "Path: " + exchange.getRequest().getURI().getPath() + ", Method: " + methodVersion);
          return chain.filter(exchange);
      }
  
      /**
       * 获取适用的限流配置
       *
       * @param methodVersion 方法名和版本号的组合
       * @param ip            请求的 IP 地址
       * @return 适用的限流配置集合
       */
      private Set<Limiter> getLimiter(String methodVersion, String ip) {
          String[] limitKeys = new String[] { methodVersion, ip + methodVersion };
          Set<Limiter> limitCfgList = new HashSet<>();
          for (String limitKey : limitKeys) {
              Limiter limitCfg = registry.get(limitKey);
              if (null != limitCfg) {
                  limitCfgList.add(limitCfg);
              }
          }
          return limitCfgList;
      }
  
  }
  