diff --git a/connections/implementation/client_proxy.cc b/connections/implementation/client_proxy.cc index 06768e72..441d774d 100644 --- a/connections/implementation/client_proxy.cc +++ b/connections/implementation/client_proxy.cc @@ -76,6 +76,9 @@ ClientProxy::ClientProxy(::nearby::analytics::EventLogger* event_logger) local_safe_to_disconnect_version_ = NearbyFlags::GetInstance().GetInt64Flag( config_package_nearby::nearby_connections_feature:: kSafeToDisconnectVersion); + NEARBY_LOGS(INFO) << "[safe-to-disconnect]: Local enabled: " + << supports_safe_to_disconnect_ + << "; Version_: " << local_safe_to_disconnect_version_; } ClientProxy::~ClientProxy() { Reset(); } diff --git a/connections/implementation/endpoint_channel_manager.cc b/connections/implementation/endpoint_channel_manager.cc index cc3b5282..17970452 100644 --- a/connections/implementation/endpoint_channel_manager.cc +++ b/connections/implementation/endpoint_channel_manager.cc @@ -135,8 +135,9 @@ void EndpointChannelManager::MarkEndpointStopWaitToDisconnect( } bool EndpointChannelManager::CreateNewTimeoutDisconnectedState( - const std::string& endpoint_id) { - return channel_state_.CreateNewTimeoutDisconnectedState(endpoint_id); + const std::string& endpoint_id, absl::Duration timeout_millis) { + return channel_state_.CreateNewTimeoutDisconnectedState(endpoint_id, + timeout_millis); } bool EndpointChannelManager::IsSafeToDisconnect( @@ -281,7 +282,7 @@ void EndpointChannelManager::ChannelState::MarkEndpointStopWaitToDisconnect( } bool EndpointChannelManager::ChannelState::CreateNewTimeoutDisconnectedState( - const std::string& endpoint_id) { + const std::string& endpoint_id, absl::Duration timeout_millis) { auto item = endpoints_.find(endpoint_id); if (item == endpoints_.end()) return false; NEARBY_LOGS(INFO) << "[safe-to-disconnect] " @@ -291,9 +292,7 @@ bool EndpointChannelManager::ChannelState::CreateNewTimeoutDisconnectedState( MutexLock lock(&item->second.timeout_to_disconnected_mutex); item->second.timeout_to_disconnected_enabled = true; item->second.timeout_to_disconnected_notified = false; - item->second.timeout_to_disconnected.Wait(FeatureFlags::GetInstance() - .GetFlags() - .safe_to_disconnect_ack_delay_millis); + item->second.timeout_to_disconnected.Wait(timeout_millis); NEARBY_LOGS(INFO) << "[safe-to-disconnect] Wait is done with " << (item->second.timeout_to_disconnected_notified ? "notification" diff --git a/connections/implementation/endpoint_channel_manager.h b/connections/implementation/endpoint_channel_manager.h index 16980164..2f7348aa 100644 --- a/connections/implementation/endpoint_channel_manager.h +++ b/connections/implementation/endpoint_channel_manager.h @@ -22,6 +22,7 @@ #include "securegcm/d2d_connection_context_v1.h" #include "absl/base/thread_annotations.h" #include "absl/container/flat_hash_map.h" +#include "absl/time/time.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" #include "internal/platform/feature_flags.h" @@ -114,7 +115,8 @@ class EndpointChannelManager final { bool is_safe_to_disconnect, bool notify_stop_waiting) ABSL_LOCKS_EXCLUDED(mutex_); - bool CreateNewTimeoutDisconnectedState(const std::string& endpoint_id) + bool CreateNewTimeoutDisconnectedState(const std::string& endpoint_id, + absl::Duration timeout_millis) ABSL_LOCKS_EXCLUDED(mutex_); bool IsSafeToDisconnect(const std::string& endpoint_id) ABSL_LOCKS_EXCLUDED(mutex_); @@ -193,7 +195,8 @@ class EndpointChannelManager final { void MarkEndpointStopWaitToDisconnect(const std::string& endpoint_id, bool is_safe_to_disconnect, bool notify_stop_waiting); - bool CreateNewTimeoutDisconnectedState(const std::string& endpoint_id); + bool CreateNewTimeoutDisconnectedState(const std::string& endpoint_id, + absl::Duration timeout_millis); bool IsSafeToDisconnect(const std::string& endpoint_id); void RemoveTimeoutDisconnectedState(const std::string& endpoint_id); diff --git a/connections/implementation/endpoint_manager.cc b/connections/implementation/endpoint_manager.cc index 1cccfc41..0e61b8c1 100644 --- a/connections/implementation/endpoint_manager.cc +++ b/connections/implementation/endpoint_manager.cc @@ -32,6 +32,7 @@ #include "connections/implementation/service_id_constants.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/exception.h" +#include "internal/platform/feature_flags.h" #include "internal/platform/logging.h" #include "internal/platform/mutex.h" #include "internal/platform/mutex_lock.h" @@ -782,6 +783,10 @@ bool EndpointManager::ApplySafeToDisconnect(const std::string& endpoint_id, << reason; bool is_safe_disconnection = false; bool send_disconnection_frame = true; + absl::Duration timeout_millis = FeatureFlags::GetInstance() + .GetFlags() + .safe_to_disconnect_ack_delay_millis; + bool is_wait_for_ack = true; switch (reason) { case DisconnectionReason::UPGRADED: case DisconnectionReason::SHUTDOWN: @@ -796,6 +801,11 @@ bool EndpointManager::ApplySafeToDisconnect(const std::string& endpoint_id, case DisconnectionReason::REMOTE_DISCONNECTION: is_safe_disconnection = true; send_disconnection_frame = false; + timeout_millis = + FeatureFlags::GetInstance() + .GetFlags() + .safe_to_disconnect_remote_disc_delay_millis; + is_wait_for_ack = false; break; default: is_safe_disconnection = false; @@ -820,8 +830,11 @@ bool EndpointManager::ApplySafeToDisconnect(const std::string& endpoint_id, } } - bool state = - channel_manager_->CreateNewTimeoutDisconnectedState(endpoint_id); + NEARBY_LOGS(WARNING) << "[safe-to-disconnect] Wait for " + << (is_wait_for_ack ? "ack" : "disconnection") + << ", timeout in " << timeout_millis; + bool state = channel_manager_->CreateNewTimeoutDisconnectedState( + endpoint_id, timeout_millis); if (!state) return is_safe_disconnection; return is_safe_disconnection || diff --git a/internal/platform/feature_flags.h b/internal/platform/feature_flags.h index d4202188..736c8015 100644 --- a/internal/platform/feature_flags.h +++ b/internal/platform/feature_flags.h @@ -71,6 +71,8 @@ class FeatureFlags { // initiator will end the connection in 30s. absl::Duration safe_to_disconnect_ack_delay_millis = absl::Milliseconds(30000); + absl::Duration safe_to_disconnect_remote_disc_delay_millis = + absl::Milliseconds(10000); // If the receiver doesn't ack with payload_received_ack frame in 1s, the // sender will timeout the waiting. absl::Duration wait_payload_received_ack_millis = absl::Milliseconds(1000);