InFlightRequests
InFlightRequests缓存了已经发送但是还没有收到响应的ClientRequset,主要字段有:
final class InFlightRequests {
// 每个连接最多缓存ClientRequset的个数
private final int maxInFlightRequestsPerConnection;
// key是nodeID,value是clientRequest
private final Map<String, Deque<ClientRequest>> requests = new HashMap<String, Deque<ClientRequest>>();
}
canSendMore方法判断是否可以向指定的node发送请求
final class InFlightRequests {
/**
* Can we send more requests to this node?
*
* @param node Node in question
* @return true iff we have no requests still being sent to the given node
*/
public boolean canSendMore(String node) {
Deque<ClientRequest> queue = requests.get(node);
//queue.peekFirst().request().completed()表示当前队头的请求已经发送完成,如果迟迟发送不出去,则不能向这个node发送消息。
//队头的消息和KafkaChannel.send字段指向的是同一个消息,避免覆盖send也不会指向新的请求。
//再判断是否积压请求。
return queue == null || queue.isEmpty() ||
(queue.peekFirst().request().completed() && queue.size() < this.maxInFlightRequestsPerConnection);
}
}