【网络核心层篇】NetworkClient—检查连接

本文深入探讨了Kafka的NetworkClient如何检查Node连接状态,包括isReady()方法的判断逻辑,以及元数据更新和消息发送的条件。通过分析源码,阐述了连接就绪、元数据更新需求及发送消息的能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sender 线程在发送消息的的sendProducerData()方法中,会对Kafka的的每个node 进行检测是否可以发送消息,将没有就绪的node节点移除,这个时候就会调用NetworkClient的ready方法对指定的node 进行检测。

1.流程图:

在这里插入图片描述

2.源码分析:
1.检测node的连接状态是否就绪

可用则为true,不可用则返回false,并会初始化一个连接

/**
 * Begin connecting to the given node, return true if we are already connected and ready to send to that node.
 *
 * 检测指定node是否可以发送请求
 * 如果可以发送,那么就返回true,否则就初始化一个连接,
 * @param node The node to check
 * @param now  The current timestamp
 * @return True if we are ready to send to the given node
 */
@Override
public boolean ready(Node node, long now) {
   
   
    if (node.isEmpty())
        throw new IllegalArgumentException("Cannot connect to empty node " + node);

    //是否已经准备好发送请求 [2]
    if (isReady(node, now))
        return true;

    // 检测该节点现在是否能够连接
    if (connectionStates.canConnect(node.idString(), now))
        // if we are interested in sending to a node and we don't have a connection to it, initiate one
      //初始化一个连接
        initiateConnect(node, now)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值