From 0aae246c5dc62e4f6541b3afe66ba04eb2d02799 Mon Sep 17 00:00:00 2001 From: Sara Golemon Date: Wed, 12 Sep 2018 20:58:55 +0000 Subject: SERVER-37104 Split async helpers off of http_client --- src/mongo/db/free_mon/free_mon_mongod.cpp | 87 +++++++++++++++--------- src/mongo/util/net/SConscript | 8 --- src/mongo/util/net/http_client.cpp | 86 ----------------------- src/mongo/util/net/http_client.h | 15 ---- src/mongo/util/net/http_client_winhttp.cpp | 1 + src/mongo/util/options_parser/options_parser.cpp | 1 + 6 files changed, 55 insertions(+), 143 deletions(-) delete mode 100644 src/mongo/util/net/http_client.cpp diff --git a/src/mongo/db/free_mon/free_mon_mongod.cpp b/src/mongo/db/free_mon/free_mon_mongod.cpp index f4a9c67cadb..ad9573ab46e 100644 --- a/src/mongo/db/free_mon/free_mon_mongod.cpp +++ b/src/mongo/db/free_mon/free_mon_mongod.cpp @@ -59,6 +59,7 @@ #include "mongo/db/server_parameters.h" #include "mongo/db/service_context.h" #include "mongo/executor/network_interface_factory.h" +#include "mongo/executor/thread_pool_task_executor.h" #include "mongo/rpc/object_check.h" #include "mongo/util/assert_util.h" #include "mongo/util/concurrency/thread_pool.h" @@ -124,28 +125,25 @@ public: auto data = std::make_shared>( reqObj.objdata(), reqObj.objdata() + reqObj.objsize()); - return _client - ->postAsync( - _executor.get(), exportedExportedFreeMonEndpointURL.getLocked() + "/register", data) - .then([](DataBuilder&& blob) { + return post("/register", data).then([](DataBuilder&& blob) { - if (!blob.size()) { - uasserted(ErrorCodes::FreeMonHttpTemporaryFailure, "Empty response received"); - } + if (!blob.size()) { + uasserted(ErrorCodes::FreeMonHttpTemporaryFailure, "Empty response received"); + } - auto blobSize = blob.size(); - auto blobData = blob.release(); - ConstDataRange cdr(blobData.get(), blobSize); - auto swDoc = cdr.read>(); - uassertStatusOK(swDoc.getStatus()); + auto blobSize = blob.size(); + auto blobData = blob.release(); + ConstDataRange cdr(blobData.get(), blobSize); + auto swDoc = cdr.read>(); + uassertStatusOK(swDoc.getStatus()); - BSONObj respObj(swDoc.getValue()); + BSONObj respObj(swDoc.getValue()); - auto resp = - FreeMonRegistrationResponse::parse(IDLParserErrorContext("response"), respObj); + auto resp = + FreeMonRegistrationResponse::parse(IDLParserErrorContext("response"), respObj); - return resp; - }); + return resp; + }); } Future sendMetricsAsync(const FreeMonMetricsRequest& req) override { @@ -153,29 +151,50 @@ public: auto data = std::make_shared>( reqObj.objdata(), reqObj.objdata() + reqObj.objsize()); - return _client - ->postAsync( - _executor.get(), exportedExportedFreeMonEndpointURL.getLocked() + "/metrics", data) - .then([](DataBuilder&& blob) { + return post("/metrics", data).then([](DataBuilder&& blob) { - if (!blob.size()) { - uasserted(ErrorCodes::FreeMonHttpTemporaryFailure, "Empty response received"); - } + if (!blob.size()) { + uasserted(ErrorCodes::FreeMonHttpTemporaryFailure, "Empty response received"); + } + + auto blobSize = blob.size(); + auto blobData = blob.release(); + ConstDataRange cdr(blobData.get(), blobSize); - auto blobSize = blob.size(); - auto blobData = blob.release(); - ConstDataRange cdr(blobData.get(), blobSize); + auto swDoc = cdr.read>(); + uassertStatusOK(swDoc.getStatus()); - auto swDoc = cdr.read>(); - uassertStatusOK(swDoc.getStatus()); + BSONObj respObj(swDoc.getValue()); - BSONObj respObj(swDoc.getValue()); + auto resp = FreeMonMetricsResponse::parse(IDLParserErrorContext("response"), respObj); - auto resp = - FreeMonMetricsResponse::parse(IDLParserErrorContext("response"), respObj); + return resp; + }); + } + +private: + Future post(StringData path, + std::shared_ptr> data) const { + auto pf = makePromiseFuture(); + std::string url(exportedExportedFreeMonEndpointURL.getLocked() + path.toString()); + + auto status = _executor->scheduleWork([ + shared_promise = pf.promise.share(), + url = std::move(url), + data = std::move(data), + this + ](const executor::TaskExecutor::CallbackArgs& cbArgs) mutable { + ConstDataRange cdr(reinterpret_cast(data->data()), data->size()); + try { + auto result = this->_client->post(url, cdr); + shared_promise.emplaceValue(std::move(result)); + } catch (...) { + shared_promise.setError(exceptionToStatus()); + } + }); - return resp; - }); + uassertStatusOK(status); + return std::move(pf.future); } private: diff --git a/src/mongo/util/net/SConscript b/src/mongo/util/net/SConscript index f89065ca5b5..95a167f88c3 100644 --- a/src/mongo/util/net/SConscript +++ b/src/mongo/util/net/SConscript @@ -156,29 +156,21 @@ if http_client == "off": env.Library( target='http_client', source=[ - 'http_client.cpp', 'http_client_none.cpp', ], LIBDEPS=[ '$BUILD_DIR/mongo/base', ], - LIBDEPS_PRIVATE=[ - '$BUILD_DIR/mongo/executor/thread_pool_task_executor', - ], ) else: env.Library( target='http_client', source=[ - 'http_client.cpp', 'http_client_winhttp.cpp' if env.TargetOSIs('windows') else 'http_client_curl.cpp', ], LIBDEPS=[ '$BUILD_DIR/mongo/base', ], - LIBDEPS_PRIVATE=[ - '$BUILD_DIR/mongo/executor/thread_pool_task_executor', - ], SYSLIBDEPS=[ 'winhttp' if env.TargetOSIs('windows') else 'curl', ], diff --git a/src/mongo/util/net/http_client.cpp b/src/mongo/util/net/http_client.cpp deleted file mode 100644 index 6c5bc0f0d2a..00000000000 --- a/src/mongo/util/net/http_client.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/** - * Copyright (C) 2018 MongoDB Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * As a special exception, the copyright holders give permission to link the - * code of portions of this program with the OpenSSL library under certain - * conditions as described in each individual source file and distribute - * linked combinations including the program with the OpenSSL library. You - * must comply with the GNU Affero General Public License in all respects - * for all of the code used other than as permitted herein. If you modify - * file(s) with this exception, you may extend this exception to your - * version of the file(s), but you are not obligated to do so. If you do not - * wish to do so, delete this exception statement from your version. If you - * delete this exception statement from all source files in the program, - * then also delete it in the license file. - */ - -#include "mongo/platform/basic.h" - -#include "mongo/util/net/http_client.h" - -#include -#include - -#include "mongo/base/data_range.h" -#include "mongo/base/string_data.h" -#include "mongo/util/assert_util.h" - -namespace mongo { - -Future HttpClient::postAsync(executor::ThreadPoolTaskExecutor* executor, - StringData url, - std::shared_ptr> data) const { - auto pf = makePromiseFuture(); - std::string urlString(url.toString()); - - auto status = executor->scheduleWork([ - shared_promise = pf.promise.share(), - urlString = std::move(urlString), - data = std::move(data), - this - ](const executor::TaskExecutor::CallbackArgs& cbArgs) mutable { - ConstDataRange cdr(reinterpret_cast(data->data()), data->size()); - try { - auto result = this->post(urlString, cdr); - shared_promise.emplaceValue(std::move(result)); - } catch (...) { - shared_promise.setError(exceptionToStatus()); - } - }); - - uassertStatusOK(status); - return std::move(pf.future); -} - -Future HttpClient::getAsync(executor::ThreadPoolTaskExecutor* executor, - StringData url) const { - auto pf = makePromiseFuture(); - std::string urlString(url.toString()); - - auto status = executor->scheduleWork([ shared_promise = pf.promise.share(), urlString, this ]( - const executor::TaskExecutor::CallbackArgs& cbArgs) mutable { - try { - auto result = this->get(urlString); - shared_promise.emplaceValue(std::move(result)); - } catch (...) { - shared_promise.setError(exceptionToStatus()); - } - }); - - uassertStatusOK(status); - return std::move(pf.future); -} - -} // namespace mongo diff --git a/src/mongo/util/net/http_client.h b/src/mongo/util/net/http_client.h index ae082de13f8..574d7cd031b 100644 --- a/src/mongo/util/net/http_client.h +++ b/src/mongo/util/net/http_client.h @@ -35,10 +35,7 @@ #include "mongo/base/data_builder.h" #include "mongo/base/data_range.h" #include "mongo/base/string_data.h" -#include "mongo/executor/thread_pool_task_executor.h" -#include "mongo/util/concurrency/thread_pool.h" #include "mongo/util/duration.h" -#include "mongo/util/future.h" namespace mongo { @@ -80,23 +77,11 @@ public: */ virtual DataBuilder post(StringData url, ConstDataRange data) const = 0; - /** - * Futurized helper for HttpClient::post(). - */ - Future postAsync(executor::ThreadPoolTaskExecutor* executor, - StringData url, - std::shared_ptr> data) const; - /** * Perform a GET request from the specified URL. */ virtual DataBuilder get(StringData url) const = 0; - /** - * Futurized helpr for HttpClient::get(). - */ - Future getAsync(executor::ThreadPoolTaskExecutor* executor, StringData url) const; - /** * Factory method provided by client implementation. */ diff --git a/src/mongo/util/net/http_client_winhttp.cpp b/src/mongo/util/net/http_client_winhttp.cpp index cdae871784e..9c4ccea5acc 100644 --- a/src/mongo/util/net/http_client_winhttp.cpp +++ b/src/mongo/util/net/http_client_winhttp.cpp @@ -50,6 +50,7 @@ #include "mongo/util/log.h" #include "mongo/util/mongoutils/str.h" #include "mongo/util/net/http_client.h" +#include "mongo/util/scopeguard.h" #include "mongo/util/text.h" #include "mongo/util/winutil.h" diff --git a/src/mongo/util/options_parser/options_parser.cpp b/src/mongo/util/options_parser/options_parser.cpp index bddc9300a48..af8465de28c 100644 --- a/src/mongo/util/options_parser/options_parser.cpp +++ b/src/mongo/util/options_parser/options_parser.cpp @@ -44,6 +44,7 @@ #include "mongo/util/assert_util.h" #include "mongo/util/log.h" #include "mongo/util/mongoutils/str.h" +#include "mongo/util/net/hostandport.h" #include "mongo/util/net/http_client.h" #include "mongo/util/options_parser/constraints.h" #include "mongo/util/options_parser/environment.h" -- cgit v1.2.1