summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSara Golemon <sara.golemon@mongodb.com>2018-09-12 20:58:55 +0000
committerSara Golemon <sara.golemon@mongodb.com>2018-09-13 15:54:06 +0000
commit0aae246c5dc62e4f6541b3afe66ba04eb2d02799 (patch)
tree69d9f89af84e82cc1479e6f5ed41cedb17f7f396
parentcd198ae20230746491022a3d33330a3af2807832 (diff)
downloadmongo-0aae246c5dc62e4f6541b3afe66ba04eb2d02799.tar.gz
SERVER-37104 Split async helpers off of http_client
-rw-r--r--src/mongo/db/free_mon/free_mon_mongod.cpp87
-rw-r--r--src/mongo/util/net/SConscript8
-rw-r--r--src/mongo/util/net/http_client.cpp86
-rw-r--r--src/mongo/util/net/http_client.h15
-rw-r--r--src/mongo/util/net/http_client_winhttp.cpp1
-rw-r--r--src/mongo/util/options_parser/options_parser.cpp1
6 files changed, 55 insertions, 143 deletions
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<std::vector<std::uint8_t>>(
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<Validated<BSONObj>>();
- uassertStatusOK(swDoc.getStatus());
+ auto blobSize = blob.size();
+ auto blobData = blob.release();
+ ConstDataRange cdr(blobData.get(), blobSize);
+ auto swDoc = cdr.read<Validated<BSONObj>>();
+ 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<FreeMonMetricsResponse> sendMetricsAsync(const FreeMonMetricsRequest& req) override {
@@ -153,29 +151,50 @@ public:
auto data = std::make_shared<std::vector<std::uint8_t>>(
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<Validated<BSONObj>>();
+ uassertStatusOK(swDoc.getStatus());
- auto swDoc = cdr.read<Validated<BSONObj>>();
- 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<DataBuilder> post(StringData path,
+ std::shared_ptr<std::vector<std::uint8_t>> data) const {
+ auto pf = makePromiseFuture<DataBuilder>();
+ 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<char*>(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 <http://www.gnu.org/licenses/>.
- *
- * 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 <string>
-#include <vector>
-
-#include "mongo/base/data_range.h"
-#include "mongo/base/string_data.h"
-#include "mongo/util/assert_util.h"
-
-namespace mongo {
-
-Future<DataBuilder> HttpClient::postAsync(executor::ThreadPoolTaskExecutor* executor,
- StringData url,
- std::shared_ptr<std::vector<std::uint8_t>> data) const {
- auto pf = makePromiseFuture<DataBuilder>();
- 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<char*>(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<DataBuilder> HttpClient::getAsync(executor::ThreadPoolTaskExecutor* executor,
- StringData url) const {
- auto pf = makePromiseFuture<DataBuilder>();
- 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 {
@@ -81,23 +78,11 @@ public:
virtual DataBuilder post(StringData url, ConstDataRange data) const = 0;
/**
- * Futurized helper for HttpClient::post().
- */
- Future<DataBuilder> postAsync(executor::ThreadPoolTaskExecutor* executor,
- StringData url,
- std::shared_ptr<std::vector<std::uint8_t>> data) const;
-
- /**
* Perform a GET request from the specified URL.
*/
virtual DataBuilder get(StringData url) const = 0;
/**
- * Futurized helpr for HttpClient::get().
- */
- Future<DataBuilder> getAsync(executor::ThreadPoolTaskExecutor* executor, StringData url) const;
-
- /**
* Factory method provided by client implementation.
*/
static std::unique_ptr<HttpClient> create();
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"