AsyncHttpClient RequestFilter请求筛选源码解读

 更新时间:2023年12月13日 09:57:13   作者:codecraft  
这篇文章主要为大家介绍了AsyncHttpClient RequestFilter请求筛选源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下AsyncHttpClient的RequestFilter

RequestFilter

org/asynchttpclient/filter/RequestFilter.java

/**
 * A Filter interface that gets invoked before making an actual request.
 */
public interface RequestFilter {
  /**
   * An {@link org.asynchttpclient.AsyncHttpClient} will invoke {@link RequestFilter#filter} and will use the
   * returned {@link FilterContext#getRequest()} and {@link FilterContext#getAsyncHandler()} to continue the request
   * processing.
   *
   * @param ctx a {@link FilterContext}
   * @param <T> the handler result type
   * @return {@link FilterContext}. The {@link FilterContext} instance may not the same as the original one.
   * @throws FilterException to interrupt the filter processing.
   */
  <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException;
}
RequestFilter定义了filter方法

ThrottleRequestFilter

org/asynchttpclient/filter/ThrottleRequestFilter.java

/**
 * A {@link org.asynchttpclient.filter.RequestFilter} throttles requests and block when the number of permits is reached,
 * waiting for the response to arrives before executing the next request.
 */
public class ThrottleRequestFilter implements RequestFilter {
  private static final Logger logger = LoggerFactory.getLogger(ThrottleRequestFilter.class);
  private final Semaphore available;
  private final int maxWait;
  public ThrottleRequestFilter(int maxConnections) {
    this(maxConnections, Integer.MAX_VALUE);
  }
  public ThrottleRequestFilter(int maxConnections, int maxWait) {
    this(maxConnections, maxWait, false);
  }
  public ThrottleRequestFilter(int maxConnections, int maxWait, boolean fair) {
    this.maxWait = maxWait;
    available = new Semaphore(maxConnections, fair);
  }
  /**
   * {@inheritDoc}
   */
  @Override
  public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException {
    try {
      if (logger.isDebugEnabled()) {
        logger.debug("Current Throttling Status {}", available.availablePermits());
      }
      if (!available.tryAcquire(maxWait, TimeUnit.MILLISECONDS)) {
        throw new FilterException(String.format("No slot available for processing Request %s with AsyncHandler %s",
                ctx.getRequest(), ctx.getAsyncHandler()));
      }
    } catch (InterruptedException e) {
      throw new FilterException(String.format("Interrupted Request %s with AsyncHandler %s",
              ctx.getRequest(), ctx.getAsyncHandler()));
    }
    return new FilterContext.FilterContextBuilder<>(ctx)
            .asyncHandler(ReleasePermitOnComplete.wrap(ctx.getAsyncHandler(), available))
            .build();
  }
}
ThrottleRequestFilter实现了RequestFilter接口,它使用Semaphore来对request进行限流,限流不通过抛出FilterException,若通过则通过ReleasePermitOnComplete.wrap(ctx.getAsyncHandler(), available)包装一下asyncHandler以释放信号量ReleasePermitOnComplete

ReleasePermitOnComplete

org/asynchttpclient/filter/ReleasePermitOnComplete.java

/**
 * Wrapper for {@link AsyncHandler}s to release a permit on {@link AsyncHandler#onCompleted()}. This is done via a dynamic proxy to preserve all interfaces of the wrapped handler.
 */
public class ReleasePermitOnComplete {
  /**
   * Wrap handler to release the permit of the semaphore on {@link AsyncHandler#onCompleted()}.
   *
   * @param handler   the handler to be wrapped
   * @param available the Semaphore to be released when the wrapped handler is completed
   * @param <T>       the handler result type
   * @return the wrapped handler
   */
  @SuppressWarnings("unchecked")
  public static <T> AsyncHandler<T> wrap(final AsyncHandler<T> handler, final Semaphore available) {
    Class<?> handlerClass = handler.getClass();
    ClassLoader classLoader = handlerClass.getClassLoader();
    Class<?>[] interfaces = allInterfaces(handlerClass);
    return (AsyncHandler<T>) Proxy.newProxyInstance(classLoader, interfaces, (proxy, method, args) -> {
        try {
          return method.invoke(handler, args);
        } finally {
          switch (method.getName()) {
            case "onCompleted":
            case "onThrowable":
              available.release();
            default:
          }
        }
    });
  }
  //......
}
ReleasePermitOnComplete的wrap对原来的handler进行代理,在finally里头执行available.release()

preProcessRequest

org/asynchttpclient/DefaultAsyncHttpClient.java

/**
   * Configure and execute the associated {@link RequestFilter}. This class
   * may decorate the {@link Request} and {@link AsyncHandler}
   *
   * @param fc {@link FilterContext}
   * @return {@link FilterContext}
   */
  private <T> FilterContext<T> preProcessRequest(FilterContext<T> fc) throws FilterException {
    for (RequestFilter asyncFilter : config.getRequestFilters()) {
      fc = asyncFilter.filter(fc);
      assertNotNull(fc, "filterContext");
    }
    Request request = fc.getRequest();
    if (fc.getAsyncHandler() instanceof ResumableAsyncHandler) {
      request = ResumableAsyncHandler.class.cast(fc.getAsyncHandler()).adjustRequestRange(request);
    }
    if (request.getRangeOffset() != 0) {
      RequestBuilder builder = new RequestBuilder(request);
      builder.setHeader("Range", "bytes=" + request.getRangeOffset() + "-");
      request = builder.build();
    }
    fc = new FilterContext.FilterContextBuilder<>(fc).request(request).build();
    return fc;
  }
DefaultAsyncHttpClient的preProcessRequest方法遍历config.getRequestFilters(),挨个执行asyncFilter.filter(fc)

executeRequest

org/asynchttpclient/DefaultAsyncHttpClient.java

public <T> ListenableFuture<T> executeRequest(Request request, AsyncHandler<T> handler) {
    if (config.getCookieStore() != null) {
      try {
        List<Cookie> cookies = config.getCookieStore().get(request.getUri());
        if (!cookies.isEmpty()) {
          RequestBuilder requestBuilder = new RequestBuilder(request);
          for (Cookie cookie : cookies) {
            requestBuilder.addOrReplaceCookie(cookie);
          }
          request = requestBuilder.build();
        }
      } catch (Exception e) {
        handler.onThrowable(e);
        return new ListenableFuture.CompletedFailure<>("Failed to set cookies of request", e);
      }
    }

    if (noRequestFilters) {
      return execute(request, handler);
    } else {
      FilterContext<T> fc = new FilterContext.FilterContextBuilder<T>().asyncHandler(handler).request(request).build();
      try {
        fc = preProcessRequest(fc);
      } catch (Exception e) {
        handler.onThrowable(e);
        return new ListenableFuture.CompletedFailure<>("preProcessRequest failed", e);
      }

      return execute(fc.getRequest(), fc.getAsyncHandler());
    }
  }
executeRequest方法对于noRequestFilters为false会执行preProcessRequest

小结

AsyncHttpClient的RequestFilter定义了filter方法,它有一个实现类为ThrottleRequestFilter,使用信号量用于对请求进行限流;

DefaultAsyncHttpClient的executeRequest方法对于noRequestFilters为false会执行preProcessRequest,而preProcessRequest方法遍历config.getRequestFilters(),挨个执行asyncFilter.filter(fc)。

以上就是AsyncHttpClient RequestFilter请求筛选源码解读的详细内容,更多关于AsyncHttpClient RequestFilter的资料请关注脚本之家其它相关文章!

相关文章

  • Spring实战之获取其他Bean的属性值操作示例

    Spring实战之获取其他Bean的属性值操作示例

    这篇文章主要介绍了Spring实战之获取其他Bean的属性值操作,结合实例形式分析了Spring操作Bean属性值的相关配置与实现技巧,需要的朋友可以参考下
    2019-12-12
  • 老生常谈java路径中的反斜杠和斜杠的区别

    老生常谈java路径中的反斜杠和斜杠的区别

    下面小编就为大家带来一篇老生常谈java路径中的反斜杠和斜杠的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • SpringBoot实现文件上传接口

    SpringBoot实现文件上传接口

    这篇文章主要为大家详细介绍了SpringBoot实现文件上传接口,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • Spring AOP定义AfterReturning增加实例分析

    Spring AOP定义AfterReturning增加实例分析

    这篇文章主要介绍了Spring AOP定义AfterReturning增加,结合实例形式分析了Spring面相切面AOP定义AfterReturning增加相关操作技巧与使用注意事项,需要的朋友可以参考下
    2020-01-01
  • java中的前++和后++的区别示例代码详解

    java中的前++和后++的区别示例代码详解

    这篇文章主要介绍了java中的前++和后++的区别示例代码详解,其实大家只要记住一句话就可以了,前++是先自加再使用而后++是先使用再自加,本文通过代码给大家详细解说,感兴趣的朋友跟随小编一起看看吧
    2020-06-06
  • Spring Boot 参数校验的具体实现方式

    Spring Boot 参数校验的具体实现方式

    这篇文章主要介绍了Spring Boot 参数校验的具体实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-06-06
  • Nacos客户端配置中心缓存动态更新实现源码

    Nacos客户端配置中心缓存动态更新实现源码

    这篇文章主要为大家介绍了Nacos客户端配置中心缓存动态更新实现源码,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-03-03
  • Java中的序列化(Serializable)和反序列化

    Java中的序列化(Serializable)和反序列化

    这篇文章主要介绍了Java中的序列化(Serializable)和反序列化, JAVA序列化与反序列化就是JAVA对象与一串字节流之间的相互转换, 我们在程序中创建的JAVA对象只存在于JVM中,需要的朋友可以参考下
    2023-09-09
  • Java格式化输出详细讲解(printf、print、println、format等)

    Java格式化输出详细讲解(printf、print、println、format等)

    Java的格式化输出等同于String.Format,与C有很大的相似,下面这篇文章主要给大家介绍了关于Java格式化输出(printf、print、println、format等)的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-03-03
  • 关于注解式的分布式Elasticsearch的封装案例

    关于注解式的分布式Elasticsearch的封装案例

    这篇文章主要介绍了关于注解式的分布式Elasticsearch的封装案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01

最新评论