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

相关文章

  • spring retry方法调用失败重试机制示例解析

    spring retry方法调用失败重试机制示例解析

    这篇文章主要为大家介绍了spring retry方法调用失败重试机制的示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-03-03
  • springboot全局异常处理详解

    springboot全局异常处理详解

    本篇文章主要介绍了springboot全局异常处理详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • SpringBoot配置文件的优先级顺序、加载顺序、bootstrap.yml与application.yml区别及说明

    SpringBoot配置文件的优先级顺序、加载顺序、bootstrap.yml与application.yml区别及说明

    在SpringBoot中,配置文件的优先级顺序是:application-{profile}.yml或.properties > application.yml或.properties > bootstrap.yml或.properties,{profile}代表不同环境,如dev、test、prod,加载顺序是先加载bootstrap文件
    2024-09-09
  • java中关于命令执行汇总

    java中关于命令执行汇总

    这篇文章主要介绍了java中关于命令执行汇总,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • Java中Calendar类用法实例详解

    Java中Calendar类用法实例详解

    这篇文章主要给大家介绍了关于Java中Calendar类用法的相关资料,Calendar类是Java.util包中提供的一个抽象类,该类从JDK1.1开始出现,作为Date类的替代方案,Calendar类中包含了对不同国家地区日历的处理,需要的朋友可以参考下
    2023-09-09
  • SpringBoot的application.yml不生效问题及解决

    SpringBoot的application.yml不生效问题及解决

    这篇文章主要介绍了SpringBoot的application.yml不生效问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • idea如何保存当前已修改的文件|恢复到未修改状态

    idea如何保存当前已修改的文件|恢复到未修改状态

    文章主要介绍了如何在Git中将修改暂存起来以及如何恢复这些暂存的修改,详细步骤包括提交修改、保存修改、查看和恢复修改等过程
    2026-04-04
  • Java调用Redis集群代码及问题解决

    Java调用Redis集群代码及问题解决

    这篇文章主要介绍了Java调用Redis集群代码及问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • SpringBoot 优雅停机实现原理与最佳实践指南

    SpringBoot 优雅停机实现原理与最佳实践指南

    文章介绍了Spring Boot在2.3版本中引入的优雅停机机制,详细解释了优雅停机的概念、核心组件与层级关系、Spring Boot 2.3前后的区别,以及在生产环境中的最佳实践,强调了启用优雅停机的重要性,以提高系统的高可用性和稳定性
    2026-04-04
  • Java SpringBoot在RequestBody中高效的使用枚举参数原理案例详解

    Java SpringBoot在RequestBody中高效的使用枚举参数原理案例详解

    这篇文章主要介绍了Java SpringBoot在RequestBody中高效的使用枚举参数原理案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09

最新评论