From 7027f50d28acbb276e5cf3293dd1955af80b0182 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Wed, 17 May 2023 10:27:20 -0700 Subject: [PATCH] Applied thread flag to network layer PiperOrigin-RevId: 532831254 --- internal/network/BUILD | 2 + internal/network/http_client_impl.cc | 67 +++++++++++++------ .../flags/nearby_platform_feature_flags.h | 4 ++ 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/internal/network/BUILD b/internal/network/BUILD index a3cb3d6a..95fcecec 100644 --- a/internal/network/BUILD +++ b/internal/network/BUILD @@ -53,8 +53,10 @@ cc_library( ], deps = [ ":types", + "//internal/flags:nearby_flags", "//internal/platform:logging", "//internal/platform:types", + "//internal/platform/flags:platform_flags", "//internal/platform/implementation:platform", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:flat_hash_map", diff --git a/internal/network/http_client_impl.cc b/internal/network/http_client_impl.cc index a5dbacc1..1e057c86 100644 --- a/internal/network/http_client_impl.cc +++ b/internal/network/http_client_impl.cc @@ -23,7 +23,9 @@ #include #include "absl/status/status.h" +#include "internal/flags/nearby_flags.h" #include "internal/network/debug.h" +#include "internal/platform/flags/nearby_platform_feature_flags.h" #include "internal/platform/logging.h" #include "internal/platform/mutex_lock.h" #include "internal/platform/single_thread_executor.h" @@ -35,31 +37,54 @@ void NearbyHttpClient::StartRequest( const HttpRequest& request, std::function&)> callback) { MutexLock lock(&mutex_); - CleanThreads(); - - std::future http_thread = std::async( - std::launch::async, [=]() { - NEARBY_LOGS(INFO) << __func__ << ": Start async request to url=" + if (NearbyFlags::GetInstance().GetBoolFlag( + platform::config_package_nearby::nearby_platform_feature:: + kEnablePlatformThreadToNetwork)) { + executor_.Execute([request = std::move(request), + callback = std::move(callback)]() { + NEARBY_LOGS(INFO) << __func__ << ": Start async request to url=" + << request.GetUrl().GetUrlPath(); + absl::StatusOr response = InternalGetResponse(request); + if (response.ok()) { + NEARBY_LOGS(INFO) << __func__ << ": Got response from url=" << request.GetUrl().GetUrlPath(); - absl::StatusOr response = InternalGetResponse(request); - if (response.ok()) { - NEARBY_LOGS(INFO) - << __func__ - << ": Got response from url=" << request.GetUrl().GetUrlPath(); - } else { - NEARBY_LOGS(ERROR) << __func__ << ": Failed to get response from url=" - << request.GetUrl().GetUrlPath() << ", status" - << response.status(); - } + } else { + NEARBY_LOGS(ERROR) << __func__ << ": Failed to get response from url=" + << request.GetUrl().GetUrlPath() << ", status" + << response.status(); + } - if (callback) { - callback(response); - } - NEARBY_LOGS(INFO) << __func__ << ": Completed request to url=" + if (callback) { + callback(response); + } + NEARBY_LOGS(INFO) << __func__ << ": Completed request to url=" + << request.GetUrl().GetUrlPath(); + }); + } else { + CleanThreads(); + + std::future http_thread = std::async(std::launch::async, [=]() { + NEARBY_LOGS(INFO) << __func__ << ": Start async request to url=" + << request.GetUrl().GetUrlPath(); + absl::StatusOr response = InternalGetResponse(request); + if (response.ok()) { + NEARBY_LOGS(INFO) << __func__ << ": Got response from url=" << request.GetUrl().GetUrlPath(); - }); + } else { + NEARBY_LOGS(ERROR) << __func__ << ": Failed to get response from url=" + << request.GetUrl().GetUrlPath() << ", status" + << response.status(); + } - http_threads_.push_back(std::move(http_thread)); + if (callback) { + callback(response); + } + NEARBY_LOGS(INFO) << __func__ << ": Completed request to url=" + << request.GetUrl().GetUrlPath(); + }); + + http_threads_.push_back(std::move(http_thread)); + } } void NearbyHttpClient::StartCancellableRequest( diff --git a/internal/platform/flags/nearby_platform_feature_flags.h b/internal/platform/flags/nearby_platform_feature_flags.h index ff899639..32e5d7b3 100644 --- a/internal/platform/flags/nearby_platform_feature_flags.h +++ b/internal/platform/flags/nearby_platform_feature_flags.h @@ -31,6 +31,10 @@ namespace nearby_platform_feature { constexpr auto kEnableHotspotWin32Socket = flags::Flag(kConfigPackage, "45401992", true); +// Apply platform thread to network library. +constexpr auto kEnablePlatformThreadToNetwork = + flags::Flag(kConfigPackage, "45412711", true); + } // namespace nearby_platform_feature } // namespace config_package_nearby } // namespace platform