1、调用流程图
2、关键方法分析
主要方法是Connection::UpdateState(),该方法
- 更新了connection的状态
- 实现了哪些connection要从可用集合中删除
代码(port.cc 1042L):
void Connection::UpdateState(int64_t now) {
int rtt = ConservativeRTTEstimate(rtt_);
if (LOG_CHECK_LEVEL(LS_VERBOSE)) {
std::string pings;
PrintPingsSinceLastResponse(&pings, 5);
LOG_J(LS_VERBOSE, this) << "UpdateState()"
<< ", ms since last received response="
<< now - last_ping_response_received_
<< ", ms since last received data="
<< now - last_data_received_
<< ", rtt=" << rtt
<< ", pings_since_last_response=" << pings;
}
// Check the writable state. (The order of these checks is important.)
//
// Before becoming unwritable, we allow for a fixed number of pings to fail
// (i.e., receive no response). We also have to give the response time to
// get back, so we include a conservative estimate of this.
//
// Before timing out writability, we give a fixed amount of time. This is to
// allow for changes in network conditions.
if ((write_state_ == STATE_WRITABLE) &&
TooManyFailures(pings_since_last_response_,
CONNECTION_WRITE_CONNECT_FAILURES,
rtt,
now) &&
TooLongWithoutResponse(pings_since_last_response_,
CONNECTION_WRITE_CONNECT_TIMEOUT,
now)) {
uint32_t max_pings = CONNECTION_WRITE_CONNECT_FAILURES;
LOG_J(LS_INFO, this) << "Unwritable after " << max_pings
<< " ping failures and "
<< now - pings_since_last_response_[0].sent_time
<< " ms without a response,"
<< " ms since last received ping="
<< now - last_ping_received_
<< " ms since last received data="
<< now - last_data_received_
<< " rtt=" << rtt;
set_write_state(STATE_WRITE_UNRELIABLE);
}
if ((write_state_ == STATE_WRITE_UNRELIABLE ||
write_state_ == STATE_WRITE_INIT) &&
TooLongWithoutResponse(pings_since_last_response_,
CONNECTION_WRITE_TIMEOUT,
now)) {
LOG_J(LS_INFO, this) << "Timed out after "
<< now - pings_since_last_response_[0].sent_time
<< " ms without a response"
<< ", rtt=" << rtt;
set_write_state(STATE_WRITE_TIMEOUT);
}
// Check the receiving state.
int64_t last_recv_time = last_received();
bool receiving = now <= last_recv_time + receiving_timeout_;
set_receiving(receiving);
if (dead(now)) {
Destroy();
}
}
从上述删除中可以看出connection的删除条件如下图: