Skip to content

Commit faecca1

Browse files
authored
cleanup: rest client style cleanups (#8451)
* cleanup: rest client style cleanups 1. https://github.com/googleapis/google-cloud-cpp/blob/541eb09c32c91c09ea84a55fc0861e916ebec56a/google/cloud/internal/curl_impl.h#L40-L42 declares a static object (i.e., internal linkage) in a header. We want to avoid that because it can lead to ODR issues. See https://abseil.io/tips/140 for more details. 2. https://github.com/googleapis/google-cloud-cpp/blob/541eb09c32c91c09ea84a55fc0861e916ebec56a/google/cloud/internal/rest_client.h#L38-L44 These factory functions should be named "Make..." not "Get...". For functions that create things, we use the verb "Make", which follows the standard's naming (e.g., make_pair).
1 parent 2ba1571 commit faecca1

11 files changed

+32
-35
lines changed

google/cloud/internal/curl_impl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ Status CurlImpl::MakeRequestImpl() {
620620
TRACE_STATE() << "url_ " << url_ << "\n";
621621
// Setting BUFFERSIZE is a request, not an order. libcurl can and will write
622622
// fewer bytes as it wishes.
623-
handle_.SetOption(CURLOPT_BUFFERSIZE, kDefaultCurlWriteBufferSize);
623+
handle_.SetOption(CURLOPT_BUFFERSIZE, spill_.max_size());
624624
handle_.SetOption(CURLOPT_URL, url_.c_str());
625625
handle_.SetOption(CURLOPT_HTTPHEADER, request_headers_.get());
626626
handle_.SetOption(CURLOPT_USERAGENT, user_agent_.c_str());

google/cloud/internal/curl_impl.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ extern "C" std::size_t CurlRequestWrite(char* ptr, size_t size, size_t nmemb,
3737
extern "C" std::size_t CurlRequestHeader(char* contents, std::size_t size,
3838
std::size_t nitems, void* userdata);
3939

40-
// We get better performance using a slightly larger buffer (128KiB) than the
41-
// default buffer size set by libcurl (16KiB).
42-
static auto constexpr kDefaultCurlWriteBufferSize = 128 * 1024L;
43-
4440
// This class encapsulates use of libcurl and manages all the necessary state
4541
// of a request and its associated response.
4642
class CurlImpl {
@@ -159,8 +155,9 @@ class CurlImpl {
159155
// WriteCallback. However, the callback *must* save all the bytes, returning
160156
// fewer bytes read aborts the download. The application may have requested
161157
// fewer bytes in the call to `Read()`, so we need a place to store the
162-
// additional bytes.
163-
std::array<char, kDefaultCurlWriteBufferSize> spill_;
158+
// additional bytes. We get better performance using a slightly larger buffer
159+
// (128KiB) than the default buffer size set by libcurl (16KiB).
160+
std::array<char, 128 * 1024L> spill_;
164161
std::size_t spill_offset_;
165162

166163
Options options_;

google/cloud/internal/curl_rest_client.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,15 @@ StatusOr<std::unique_ptr<RestResponse>> CurlRestClient::Put(
208208
new CurlRestResponse(options_, std::move(*impl)))};
209209
}
210210

211-
std::unique_ptr<RestClient> GetDefaultRestClient(std::string endpoint_address,
212-
Options options) {
211+
std::unique_ptr<RestClient> MakeDefaultRestClient(std::string endpoint_address,
212+
Options options) {
213213
auto factory = GetDefaultCurlHandleFactory(options);
214214
return std::unique_ptr<RestClient>(new CurlRestClient(
215215
std::move(endpoint_address), std::move(factory), std::move(options)));
216216
}
217217

218-
std::unique_ptr<RestClient> GetPooledRestClient(std::string endpoint_address,
219-
Options options) {
218+
std::unique_ptr<RestClient> MakePooledRestClient(std::string endpoint_address,
219+
Options options) {
220220
std::size_t pool_size = kDefaultPooledCurlHandleFactorySize;
221221
if (options.has<ConnectionPoolSizeOption>()) {
222222
pool_size = options.get<ConnectionPoolSizeOption>();

google/cloud/internal/curl_rest_client.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ class CurlRestClient : public RestClient {
6565
std::vector<absl::Span<char const>> const& payload) override;
6666

6767
private:
68-
friend std::unique_ptr<RestClient> GetDefaultRestClient(
68+
friend std::unique_ptr<RestClient> MakeDefaultRestClient(
6969
std::string endpoint_address, Options options);
7070

71-
friend class std::unique_ptr<RestClient> GetPooledRestClient(
71+
friend class std::unique_ptr<RestClient> MakePooledRestClient(
7272
std::string endpoint_address, Options options);
7373

7474
CurlRestClient(std::string endpoint_address,

google/cloud/internal/curl_rest_client_integration_test.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class RestClientIntegrationTest : public ::testing::Test {
133133

134134
TEST_F(RestClientIntegrationTest, Get) {
135135
options_.set<UnifiedCredentialsOption>(MakeInsecureCredentials());
136-
auto client = GetDefaultRestClient(url_, {});
136+
auto client = MakeDefaultRestClient(url_, {});
137137
RestRequest request;
138138
request.SetPath("get");
139139
auto response_status = RetryRestRequest([&] { return client->Get(request); });
@@ -150,7 +150,7 @@ TEST_F(RestClientIntegrationTest, Get) {
150150
TEST_F(RestClientIntegrationTest, Delete) {
151151
options_.set<UnifiedCredentialsOption>(MakeInsecureCredentials());
152152
options_.set<UserIpOption>("127.0.0.1");
153-
auto client = GetDefaultRestClient(url_, options_);
153+
auto client = MakeDefaultRestClient(url_, options_);
154154
RestRequest request;
155155
request.SetPath("delete");
156156
request.AddQueryParameter({"key", "value"});
@@ -177,7 +177,7 @@ TEST_F(RestClientIntegrationTest, PatchJsonContentType) {
177177
"client_email": "[email protected]",
178178
})""";
179179

180-
auto client = GetDefaultRestClient(url_, options_);
180+
auto client = MakeDefaultRestClient(url_, options_);
181181
RestRequest request;
182182
request.SetPath("patch");
183183
request.AddQueryParameter({"type", "service_account"});
@@ -209,7 +209,7 @@ TEST_F(RestClientIntegrationTest, PatchJsonContentType) {
209209

210210
TEST_F(RestClientIntegrationTest, AnythingPostNoContentType) {
211211
options_.set<UnifiedCredentialsOption>(MakeInsecureCredentials());
212-
auto client = GetDefaultRestClient(url_, options_);
212+
auto client = MakeDefaultRestClient(url_, options_);
213213
RestRequest request;
214214
request.SetPath("anything");
215215

@@ -254,7 +254,7 @@ TEST_F(RestClientIntegrationTest, AnythingPostNoContentType) {
254254

255255
TEST_F(RestClientIntegrationTest, AnythingPostJsonContentType) {
256256
options_.set<UnifiedCredentialsOption>(MakeInsecureCredentials());
257-
auto client = GetDefaultRestClient(url_, options_);
257+
auto client = MakeDefaultRestClient(url_, options_);
258258
RestRequest request;
259259
request.SetPath("anything");
260260

@@ -268,7 +268,7 @@ TEST_F(RestClientIntegrationTest, AnythingPostJsonContentType) {
268268

269269
TEST_F(RestClientIntegrationTest, AnythingPutJsonContentTypeSingleSpan) {
270270
options_.set<UnifiedCredentialsOption>(MakeInsecureCredentials());
271-
auto client = GetDefaultRestClient(url_, options_);
271+
auto client = MakeDefaultRestClient(url_, options_);
272272
RestRequest request;
273273
request.SetPath("anything");
274274

@@ -282,7 +282,7 @@ TEST_F(RestClientIntegrationTest, AnythingPutJsonContentTypeSingleSpan) {
282282

283283
TEST_F(RestClientIntegrationTest, AnythingPutJsonContentTypeTwoSpans) {
284284
options_.set<UnifiedCredentialsOption>(MakeInsecureCredentials());
285-
auto client = GetDefaultRestClient(url_, options_);
285+
auto client = MakeDefaultRestClient(url_, options_);
286286
RestRequest request;
287287
request.SetPath("anything");
288288

@@ -306,7 +306,7 @@ TEST_F(RestClientIntegrationTest, AnythingPutJsonContentTypeTwoSpans) {
306306

307307
TEST_F(RestClientIntegrationTest, AnythingPutJsonContentTypeEmptyMiddleSpan) {
308308
options_.set<UnifiedCredentialsOption>(MakeInsecureCredentials());
309-
auto client = GetDefaultRestClient(url_, options_);
309+
auto client = MakeDefaultRestClient(url_, options_);
310310
RestRequest request;
311311
request.SetPath("anything");
312312

@@ -328,7 +328,7 @@ TEST_F(RestClientIntegrationTest, AnythingPutJsonContentTypeEmptyMiddleSpan) {
328328

329329
TEST_F(RestClientIntegrationTest, AnythingPutJsonContentTypeEmptyFirstSpan) {
330330
options_.set<UnifiedCredentialsOption>(MakeInsecureCredentials());
331-
auto client = GetDefaultRestClient(url_, options_);
331+
auto client = MakeDefaultRestClient(url_, options_);
332332
RestRequest request;
333333
request.SetPath("anything");
334334

@@ -356,7 +356,7 @@ TEST_F(RestClientIntegrationTest, ResponseBodyLargerThanSpillBuffer) {
356356
auto large_json_payload = json.dump();
357357
options_.set<UnifiedCredentialsOption>(MakeInsecureCredentials());
358358
options_.set<ConnectionPoolSizeOption>(4);
359-
auto client = GetPooledRestClient(url_, options_);
359+
auto client = MakePooledRestClient(url_, options_);
360360
RestRequest request;
361361
request.SetPath("anything");
362362
request.AddHeader("content-type", "application/json");
@@ -370,7 +370,7 @@ TEST_F(RestClientIntegrationTest, ResponseBodyLargerThanSpillBuffer) {
370370

371371
TEST_F(RestClientIntegrationTest, PostFormData) {
372372
options_.set<UnifiedCredentialsOption>(MakeInsecureCredentials());
373-
auto client = GetDefaultRestClient(url_, options_);
373+
auto client = MakeDefaultRestClient(url_, options_);
374374
RestRequest request;
375375
request.SetPath("anything");
376376

google/cloud/internal/oauth2_authorized_user_credentials.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ AuthorizedUserCredentials::AuthorizedUserCredentials(
9696
rest_client_(std::move(rest_client)) {
9797
if (!rest_client_) {
9898
rest_client_ =
99-
rest_internal::GetDefaultRestClient(info_.token_uri, options_);
99+
rest_internal::MakeDefaultRestClient(info_.token_uri, options_);
100100
}
101101
}
102102

google/cloud/internal/oauth2_compute_engine_credentials.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ ComputeEngineCredentials::ComputeEngineCredentials(
9393
options_(std::move(options)) {
9494
if (!rest_client_) {
9595
options_.set<rest_internal::CurlFollowLocationOption>(true);
96-
rest_client_ = rest_internal::GetDefaultRestClient(
96+
rest_client_ = rest_internal::MakeDefaultRestClient(
9797
"http://" + google::cloud::internal::GceMetadataHostname(), options_);
9898
}
9999
}

google/cloud/internal/oauth2_minimal_iam_credentials_rest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ MinimalIamCredentialsRestStub::MinimalIamCredentialsRestStub(
4040
rest_client_(std::move(rest_client)),
4141
options_(std::move(options)) {
4242
if (!rest_client_) {
43-
rest_client_ = rest_internal::GetDefaultRestClient(endpoint_, options_);
43+
rest_client_ = rest_internal::MakeDefaultRestClient(endpoint_, options_);
4444
}
4545
}
4646

google/cloud/internal/oauth2_service_account_credentials.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ ServiceAccountCredentials::ServiceAccountCredentials(
173173
options_(std::move(options)) {
174174
if (!rest_client_) {
175175
if (options_.has<ServiceAccountCredentialsTokenUriOption>()) {
176-
rest_client_ = rest_internal::GetDefaultRestClient(
176+
rest_client_ = rest_internal::MakeDefaultRestClient(
177177
options_.get<ServiceAccountCredentialsTokenUriOption>(), options_);
178178
} else {
179179
rest_client_ =
180-
rest_internal::GetDefaultRestClient(info_.token_uri, options_);
180+
rest_internal::MakeDefaultRestClient(info_.token_uri, options_);
181181
}
182182
}
183183
}

google/cloud/internal/rest_client.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3232

3333
// Provides factory function to create a CurlRestClient that does not manage a
3434
// pool of connections.
35-
std::unique_ptr<RestClient> GetDefaultRestClient(std::string endpoint_address,
36-
Options options);
35+
std::unique_ptr<RestClient> MakeDefaultRestClient(std::string endpoint_address,
36+
Options options);
3737

3838
// Provides factory function to create a CurlRestClient that manages a pool of
3939
// connections which are reused in order to minimize costs of setup and
4040
// teardown.
41-
std::unique_ptr<RestClient> GetPooledRestClient(std::string endpoint_address,
42-
Options options);
41+
std::unique_ptr<RestClient> MakePooledRestClient(std::string endpoint_address,
42+
Options options);
4343

4444
// Provides methods corresponding to HTTP verbs to make requests to RESTful
4545
// services.

0 commit comments

Comments
 (0)