mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
internal cleanup
PiperOrigin-RevId: 547888600
This commit is contained in:
committed by
Copybara-Service
parent
770f59b748
commit
33e2fdddf4
@@ -14,18 +14,14 @@
|
||||
|
||||
#include "internal/network/http_client_impl.h"
|
||||
|
||||
#include <chrono> // NOLINT
|
||||
#include <functional>
|
||||
#include <future> // NOLINT
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
|
||||
#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"
|
||||
@@ -37,54 +33,27 @@ void NearbyHttpClient::StartRequest(
|
||||
const HttpRequest& request,
|
||||
std::function<void(const absl::StatusOr<HttpResponse>&)> callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
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<HttpResponse> response = InternalGetResponse(request);
|
||||
if (response.ok()) {
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Got response from url="
|
||||
executor_.Execute(
|
||||
[request = std::move(request), callback = std::move(callback)]() {
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Start async request to url="
|
||||
<< request.GetUrl().GetUrlPath();
|
||||
} else {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Failed to get response from url="
|
||||
<< request.GetUrl().GetUrlPath() << ", status"
|
||||
<< response.status();
|
||||
}
|
||||
absl::StatusOr<HttpResponse> 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();
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
callback(response);
|
||||
}
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Completed request to url="
|
||||
<< request.GetUrl().GetUrlPath();
|
||||
});
|
||||
} else {
|
||||
CleanThreads();
|
||||
|
||||
std::future<void> http_thread = std::async(std::launch::async, [=]() {
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Start async request to url="
|
||||
<< request.GetUrl().GetUrlPath();
|
||||
absl::StatusOr<HttpResponse> response = InternalGetResponse(request);
|
||||
if (response.ok()) {
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Got response from url="
|
||||
if (callback) {
|
||||
callback(response);
|
||||
}
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Completed request to url="
|
||||
<< request.GetUrl().GetUrlPath();
|
||||
} 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="
|
||||
<< request.GetUrl().GetUrlPath();
|
||||
});
|
||||
|
||||
http_threads_.push_back(std::move(http_thread));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void NearbyHttpClient::StartCancellableRequest(
|
||||
@@ -158,20 +127,6 @@ absl::StatusOr<HttpResponse> NearbyHttpClient::GetResponse(
|
||||
return response;
|
||||
}
|
||||
|
||||
void NearbyHttpClient::CleanThreads() {
|
||||
auto it = http_threads_.begin();
|
||||
|
||||
while (it != http_threads_.end()) {
|
||||
// Delete the thread if it is ready
|
||||
auto status = it->wait_for(std::chrono::seconds(0));
|
||||
if (status == std::future_status::ready) {
|
||||
it = http_threads_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
absl::StatusOr<HttpResponse> NearbyHttpClient::InternalGetResponse(
|
||||
const HttpRequest& request) {
|
||||
api::WebRequest web_request;
|
||||
|
||||
Reference in New Issue
Block a user