AsyncHttpClient ClientStats源码流程解读

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

本文主要研究一下AsyncHttpClient的ClientStats

ClientStats

org/asynchttpclient/ClientStats.java

/**
 * A record class representing the state of an (@link org.asynchttpclient.AsyncHttpClient).
 */
public class ClientStats {
  private final Map<String, HostStats> statsPerHost;
  public ClientStats(Map<String, HostStats> statsPerHost) {
    this.statsPerHost = Collections.unmodifiableMap(statsPerHost);
  }
  /**
   * @return A map from hostname to statistics on that host's connections.
   * The returned map is unmodifiable.
   */
  public Map<String, HostStats> getStatsPerHost() {
    return statsPerHost;
  }
  /**
   * @return The sum of {@link #getTotalActiveConnectionCount()} and {@link #getTotalIdleConnectionCount()},
   * a long representing the total number of connections in the connection pool.
   */
  public long getTotalConnectionCount() {
    return statsPerHost
            .values()
            .stream()
            .mapToLong(HostStats::getHostConnectionCount)
            .sum();
  }
  /**
   * @return A long representing the number of active connections in the connection pool.
   */
  public long getTotalActiveConnectionCount() {
    return statsPerHost
            .values()
            .stream()
            .mapToLong(HostStats::getHostActiveConnectionCount)
            .sum();
  }
  /**
   * @return A long representing the number of idle connections in the connection pool.
   */
  public long getTotalIdleConnectionCount() {
    return statsPerHost
            .values()
            .stream()
            .mapToLong(HostStats::getHostIdleConnectionCount)
            .sum();
  }
  @Override
  public String toString() {
    return "There are " + getTotalConnectionCount() +
            " total connections, " + getTotalActiveConnectionCount() +
            " are active and " + getTotalIdleConnectionCount() + " are idle.";
  }
  @Override
  public boolean equals(final Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    final ClientStats that = (ClientStats) o;
    return Objects.equals(statsPerHost, that.statsPerHost);
  }
  @Override
  public int hashCode() {
    return Objects.hashCode(statsPerHost);
  }
}
ClientStats通过Map<String, HostStats>维护了每个host对应的统计;它提供了getStatsPerHost、getTotalConnectionCount、getTotalActiveConnectionCount、getTotalIdleConnectionCount方法

HostStats

org/asynchttpclient/HostStats.java

/**
 * A record class representing the status of connections to some host.
 */
public class HostStats {
  private final long activeConnectionCount;
  private final long idleConnectionCount;
  public HostStats(long activeConnectionCount,
                   long idleConnectionCount) {
    this.activeConnectionCount = activeConnectionCount;
    this.idleConnectionCount = idleConnectionCount;
  }
  /**
   * @return The sum of {@link #getHostActiveConnectionCount()} and {@link #getHostIdleConnectionCount()},
   * a long representing the total number of connections to this host.
   */
  public long getHostConnectionCount() {
    return activeConnectionCount + idleConnectionCount;
  }
  /**
   * @return A long representing the number of active connections to the host.
   */
  public long getHostActiveConnectionCount() {
    return activeConnectionCount;
  }
  /**
   * @return A long representing the number of idle connections in the connection pool.
   */
  public long getHostIdleConnectionCount() {
    return idleConnectionCount;
  }
  @Override
  public String toString() {
    return "There are " + getHostConnectionCount() +
            " total connections, " + getHostActiveConnectionCount() +
            " are active and " + getHostIdleConnectionCount() + " are idle.";
  }
  @Override
  public boolean equals(final Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    final HostStats hostStats = (HostStats) o;
    return activeConnectionCount == hostStats.activeConnectionCount &&
            idleConnectionCount == hostStats.idleConnectionCount;
  }
  @Override
  public int hashCode() {
    return Objects.hash(activeConnectionCount, idleConnectionCount);
  }
}
HostStats定义了activeConnectionCount、idleConnectionCount属性

getClientStats

org/asynchttpclient/netty/channel/ChannelManager.java

public ClientStats getClientStats() {
    Map<String, Long> totalConnectionsPerHost = openChannels.stream().map(Channel::remoteAddress).filter(a -> a.getClass() == InetSocketAddress.class)
            .map(a -> (InetSocketAddress) a).map(InetSocketAddress::getHostName).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    Map<String, Long> idleConnectionsPerHost = channelPool.getIdleChannelCountPerHost();
    Map<String, HostStats> statsPerHost = totalConnectionsPerHost.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> {
      final long totalConnectionCount = entry.getValue();
      final long idleConnectionCount = idleConnectionsPerHost.getOrDefault(entry.getKey(), 0L);
      final long activeConnectionCount = totalConnectionCount - idleConnectionCount;
      return new HostStats(activeConnectionCount, idleConnectionCount);
    }));
    return new ClientStats(statsPerHost);
  }
ChannelManager提供了getClientStats方法,它从openChannels获取totalConnectionsPerHost,从channelPool.getIdleChannelCountPerHost()获取idleConnectionCount,然后创建HostStats,最后返回ClientStats

小结

AsyncHttpClient提供了ClientStats用于获取连接的统计信息,可按host维度,统计activeConnectionCount、idleConnectionCount,也可汇总查看totalConnectionCount、totalActiveConnectionCount、totalIdleConnectionCount。它可以从ChannelManager获取。对于使用micrometer的可以使用这个作为数据源进行适配。

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

相关文章

  • java实现后台数据显示在前端

    java实现后台数据显示在前端

    这篇文章主要为大家详细介绍了java实现后台数据显示在前端,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • 8张图带你全面了解Java kafka的核心机制

    8张图带你全面了解Java kafka的核心机制

    kafka是目前企业中很常用的消息队列产品,可以用于削峰、解耦、异步通信,本文就通过几张图带大家全面认识一下kafka,现在我们不妨带入kafka设计者的角度去思考该如何设计,它的架构是怎么样的、都有哪些组件组成、如何进行扩展等等,需要的朋友可以参考下
    2023-05-05
  • SpringSecurity拦截器链的使用详解

    SpringSecurity拦截器链的使用详解

    这篇文章主要介绍了SpringSecurity拦截器链的使用详解,webSecurity的build方法最终调用的是doBuild方法,doBuild方法调用的是webSecurity的performBuild方法,webSecurity完成所有过滤器的插件,最终返回的是过滤器链代理类filterChainProxy,需要的朋友可以参考下
    2023-11-11
  • Apache Shrio安全框架实现原理及实例详解

    Apache Shrio安全框架实现原理及实例详解

    这篇文章主要介绍了Apache Shrio安全框架实现原理及实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • 一文详解JavaWeb过滤器(Filter)

    一文详解JavaWeb过滤器(Filter)

    本文主要介绍了一文详解JavaWeb过滤器(Filter),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • springmvc后台基于@ModelAttribute获取表单提交的数据

    springmvc后台基于@ModelAttribute获取表单提交的数据

    这篇文章主要介绍了springmvc后台基于@ModelAttribute获取表单提交的数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Java桥接模式实例详解【简单版与升级版】

    Java桥接模式实例详解【简单版与升级版】

    这篇文章主要介绍了Java桥接模式,结合实例形式分析了java桥接模式简单版与升级版两种实现技巧,需要的朋友可以参考下
    2019-07-07
  • Spring Boot 优雅停机原理详解

    Spring Boot 优雅停机原理详解

    这篇文章主要为大家介绍了Spring Boot 优雅停机原理详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • 浅谈SpringBoot实现异步调用的几种方式

    浅谈SpringBoot实现异步调用的几种方式

    本文主要介绍了浅谈SpringBoot实现异步调用的几种方式,主要包括CompletableFuture异步任务,基于@Async异步任务, TaskExecutor异步任务,感兴趣的可以了解一下
    2023-11-11
  • SpringBoot使用前缀树过滤敏感词的方法实例

    SpringBoot使用前缀树过滤敏感词的方法实例

    Trie也叫做字典树、前缀树(Prefix Tree)、单词查找树,特点:查找效率高,消耗内存大,这篇文章主要给大家介绍了关于SpringBoot使用前缀树过滤敏感词的相关资料,需要的朋友可以参考下
    2022-01-01

最新评论