From 8f7a5aad55a64f0d81b6436a22ffbdfcdcde91e0 Mon Sep 17 00:00:00 2001 From: zhihuang Date: Fri, 13 May 2016 12:22:59 -0700 Subject: [PATCH] Increase the stun ping interval. Writable connections are pinged at a slower rate. The function IsPingable will filter out the writable connections. The interval for slower ping rate by default is WRITABLE_CONNECTION_PING_INTERVAL(2500ms) and can be set with the configuration. BUG=webrtc:1161 Review-Url: https://codereview.webrtc.org/1944003002 Cr-Commit-Position: refs/heads/master@{#12736} --- webrtc/p2p/base/p2ptransportchannel.cc | 31 ++++++++++++------- .../p2p/base/p2ptransportchannel_unittest.cc | 5 +-- webrtc/p2p/base/transport.h | 11 +++---- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/webrtc/p2p/base/p2ptransportchannel.cc b/webrtc/p2p/base/p2ptransportchannel.cc index 2801c44646..5e60b278c1 100644 --- a/webrtc/p2p/base/p2ptransportchannel.cc +++ b/webrtc/p2p/base/p2ptransportchannel.cc @@ -217,10 +217,8 @@ static const int STRONG_PING_INTERVAL = 1000 * PING_PACKET_SIZE / 1000; // writable or not receiving. const int WEAK_PING_INTERVAL = 1000 * PING_PACKET_SIZE / 10000; -// If the current best connection is both writable and receiving, then we will -// also try hard to make sure it is pinged at this rate (a little less than -// 2 * STRONG_PING_INTERVAL). -static const int MAX_CURRENT_STRONG_INTERVAL = 900; // ms +// Writable connections are pinged at a slower rate. +static const int WRITABLE_CONNECTION_PING_INTERVAL = 2500; // ms static const int MIN_CHECK_RECEIVING_INTERVAL = 50; // ms @@ -250,7 +248,7 @@ P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, 0 /* backup_connection_ping_interval */, false /* gather_continually */, false /* prioritize_most_likely_candidate_pairs */, - MAX_CURRENT_STRONG_INTERVAL /* max_strong_interval */) { + WRITABLE_CONNECTION_PING_INTERVAL) { uint32_t weak_ping_interval = ::strtoul( webrtc::field_trial::FindFullName("WebRTC-StunInterPacketDelay").c_str(), nullptr, 10); @@ -433,11 +431,13 @@ void P2PTransportChannel::SetIceConfig(const IceConfig& config) { LOG(LS_INFO) << "Set ping most likely connection to " << config_.prioritize_most_likely_candidate_pairs; - if (config.max_strong_interval >= 0 && - config_.max_strong_interval != config.max_strong_interval) { - config_.max_strong_interval = config.max_strong_interval; - LOG(LS_INFO) << "Set max strong interval to " - << config_.max_strong_interval; + if (config.writable_connection_ping_interval >= 0 && + config_.writable_connection_ping_interval != + config.writable_connection_ping_interval) { + config_.writable_connection_ping_interval = + config.writable_connection_ping_interval; + LOG(LS_INFO) << "Set writable_connection_ping_interval to " + << config_.writable_connection_ping_interval; } } @@ -1307,6 +1307,7 @@ void P2PTransportChannel::OnCheckAndPing() { MarkConnectionPinged(conn); } } + int delay = std::min(ping_interval, check_receiving_interval_); thread()->PostDelayed(delay, this, MSG_CHECK_AND_PING); } @@ -1348,6 +1349,13 @@ bool P2PTransportChannel::IsPingable(Connection* conn, int64_t now) { return (now >= conn->last_ping_response_received() + config_.backup_connection_ping_interval); } + + // Writable connections are pinged at a slower rate. + if (conn->writable()) { + return (now >= + conn->last_ping_sent() + config_.writable_connection_ping_interval); + } + return conn->active(); } @@ -1363,7 +1371,8 @@ Connection* P2PTransportChannel::FindNextPingableConnection() { Connection* conn_to_ping = nullptr; if (best_connection_ && best_connection_->connected() && best_connection_->writable() && - (best_connection_->last_ping_sent() + config_.max_strong_interval <= + (best_connection_->last_ping_sent() + + config_.writable_connection_ping_interval <= now)) { conn_to_ping = best_connection_; } else { diff --git a/webrtc/p2p/base/p2ptransportchannel_unittest.cc b/webrtc/p2p/base/p2ptransportchannel_unittest.cc index 54ab3196cb..51933d4a9e 100644 --- a/webrtc/p2p/base/p2ptransportchannel_unittest.cc +++ b/webrtc/p2p/base/p2ptransportchannel_unittest.cc @@ -2696,13 +2696,14 @@ class P2PTransportChannelMostLikelyToWorkFirstTest cricket::P2PTransportChannel& StartTransportChannel( bool prioritize_most_likely_to_work, - int max_strong_interval) { + int writable_connection_ping_interval) { channel_.reset( new cricket::P2PTransportChannel("checks", 1, nullptr, allocator())); cricket::IceConfig config = channel_->config(); config.prioritize_most_likely_candidate_pairs = prioritize_most_likely_to_work; - config.max_strong_interval = max_strong_interval; + config.writable_connection_ping_interval = + writable_connection_ping_interval; channel_->SetIceConfig(config); PrepareChannel(channel_.get()); channel_->Connect(); diff --git a/webrtc/p2p/base/transport.h b/webrtc/p2p/base/transport.h index e31d37a6f6..68f178a40a 100644 --- a/webrtc/p2p/base/transport.h +++ b/webrtc/p2p/base/transport.h @@ -154,23 +154,22 @@ struct IceConfig { // is writable yet. bool prioritize_most_likely_candidate_pairs = false; - // If the current best connection is both writable and receiving, - // then we will also try hard to make sure it is pinged at this rate - // (Default value is a little less than 2 * STRONG_PING_INTERVAL). - int max_strong_interval = -1; + // Writable connections are pinged at a slower rate. + int writable_connection_ping_interval = -1; IceConfig() {} IceConfig(int receiving_timeout_ms, int backup_connection_ping_interval, bool gather_continually, bool prioritize_most_likely_candidate_pairs, - int max_strong_interval_ms) + int writable_connection_ping_interval_ms) : receiving_timeout(receiving_timeout_ms), backup_connection_ping_interval(backup_connection_ping_interval), gather_continually(gather_continually), prioritize_most_likely_candidate_pairs( prioritize_most_likely_candidate_pairs), - max_strong_interval(max_strong_interval_ms) {} + writable_connection_ping_interval( + writable_connection_ping_interval_ms) {} }; bool BadTransportDescription(const std::string& desc, std::string* err_desc);