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的资料请关注脚本之家其它相关文章!

相关文章

  • 手把手带你实现第一个Mybatis程序

    手把手带你实现第一个Mybatis程序

    这篇文章主要介绍了mybatis实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2021-07-07
  • Java中的Vector和ArrayList区别及比较

    Java中的Vector和ArrayList区别及比较

    这篇文章主要介绍了Java中的Vector和ArrayList区别及比较,本文从API、同步、数据增长、使用模式4个方面总结了它们之间的不同之处,需要的朋友可以参考下
    2015-03-03
  • Java高性能序列化工具Kryo详情

    Java高性能序列化工具Kryo详情

    这篇文章主要介绍了Java高性能序列化工具Kryo详情,Kryo 是一个快速序列化/反序列化工具,依赖于字节码生成机制,更多相关内容感兴趣的朋友可以参考一下下面文章内容
    2022-06-06
  • java int转byte和long转byte的方法

    java int转byte和long转byte的方法

    下面小编就为大家带来一篇java int转byte和long转byte的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-10-10
  • Springboot使用jsp具体案例解析

    Springboot使用jsp具体案例解析

    这篇文章主要介绍了Springboot使用jsp具体案例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • SpringBoot jwt的token如何刷新

    SpringBoot jwt的token如何刷新

    这篇文章主要给大家介绍了关于SpringBoot jwt的token如何刷新的相关资料,Json web token(JWT)是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准,需要的朋友可以参考下
    2023-07-07
  • Springboot3整合Mybatis3的完整步骤记录

    Springboot3整合Mybatis3的完整步骤记录

    这篇文章主要给大家介绍了关于Springboot3整合Mybatis3的完整步骤,Spring Boot和MyBatis分别是两个功能强大的框架,它们的协同使用可以极大地简化数据访问层的开发,提高整体的开发效率,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • java控制台实现学生信息管理系统

    java控制台实现学生信息管理系统

    这篇文章主要为大家详细介绍了java控制台实现学生信息管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • SpringBoot参数校验之@Validated的使用详解

    SpringBoot参数校验之@Validated的使用详解

    这篇文章主要通过示例为大家详细介绍一下介绍了SpringBoot参数校验中@Validated的使用方法,文中的示例代码讲解详细,需要的可以参考一下
    2022-06-06
  • 如何发布jar包到maven中央仓库

    如何发布jar包到maven中央仓库

    这篇文章主要介绍了发布jar包到maven中央仓库的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2023-12-12

最新评论