Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(generator): create retry policy samples #11930

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// source: generator/integration_tests/test.proto

#include "generator/integration_tests/golden/v1/golden_kitchen_sink_client.h"
#include "generator/integration_tests/golden/v1/golden_kitchen_sink_connection_idempotency_policy.h"
#include "generator/integration_tests/golden/v1/golden_kitchen_sink_options.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
Expand Down Expand Up @@ -45,6 +47,52 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
//! [set-client-endpoint]
}

//! [custom-idempotency-policy]
class CustomIdempotencyPolicy
: public google::cloud::golden_v1::GoldenKitchenSinkConnectionIdempotencyPolicy {
public:
~CustomIdempotencyPolicy() override = default;
std::unique_ptr<google::cloud::golden_v1::GoldenKitchenSinkConnectionIdempotencyPolicy> clone() const override {
return std::make_unique<CustomIdempotencyPolicy>(*this);
}
// Override inherited functions to define as needed.
};
//! [custom-idempotency-policy]

void SetRetryPolicy(std::vector<std::string> const& argv) {
if (!argv.empty()) {
throw google::cloud::testing_util::Usage{"set-client-retry-policy"};
}
//! [set-retry-policy]
auto options = google::cloud::Options{}
.set<google::cloud::golden_v1::GoldenKitchenSinkConnectionIdempotencyPolicyOption>(
CustomIdempotencyPolicy().clone())
.set<google::cloud::golden_v1::GoldenKitchenSinkRetryPolicyOption>(
google::cloud::golden_v1::GoldenKitchenSinkLimitedErrorCountRetryPolicy(3).clone())
.set<google::cloud::golden_v1::GoldenKitchenSinkBackoffPolicyOption>(
google::cloud::ExponentialBackoffPolicy(
/*initial_delay=*/std::chrono::milliseconds(200),
/*maximum_delay=*/std::chrono::seconds(45),
/*scaling=*/2.0).clone());
auto connection = google::cloud::golden_v1::MakeGoldenKitchenSinkConnection(options);

// c1 and c2 share the same retry policies
auto c1 = google::cloud::golden_v1::GoldenKitchenSinkClient(connection);
auto c2 = google::cloud::golden_v1::GoldenKitchenSinkClient(connection);

// You can override any of the policies in a new client. This new client
// will share the policies from c1 (or c2) *except* from the retry policy.
auto c3 = google::cloud::golden_v1::GoldenKitchenSinkClient(
connection, google::cloud::Options{}.set<google::cloud::golden_v1::GoldenKitchenSinkRetryPolicyOption>(
google::cloud::golden_v1::GoldenKitchenSinkLimitedTimeRetryPolicy(std::chrono::minutes(5)).clone()));

// You can also override the policies in a single call:
// c3.SomeRpc(..., google::cloud::Options{}
// .set<google::cloud::golden_v1::GoldenKitchenSinkRetryPolicyOption>(
// google::cloud::golden_v1::GoldenKitchenSinkLimitedErrorCountRetryPolicy(10).clone()));
//! [set-client-client-retry-policy]
}

void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
Expand Down Expand Up @@ -77,6 +125,9 @@ void AutoRun(std::vector<std::string> const& argv) {
std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning SetRetryPolicy() example" << std::endl;
SetRetryPolicy({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}
Expand All @@ -86,6 +137,7 @@ void AutoRun(std::vector<std::string> const& argv) {
int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"set-retry-policy", SetRetryPolicy},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// source: generator/integration_tests/test2.proto

#include "generator/integration_tests/golden/v1/golden_rest_only_client.h"
#include "generator/integration_tests/golden/v1/golden_rest_only_connection_idempotency_policy.h"
#include "generator/integration_tests/golden/v1/golden_rest_only_options.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
Expand Down Expand Up @@ -45,6 +47,52 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
//! [set-client-endpoint]
}

//! [custom-idempotency-policy]
class CustomIdempotencyPolicy
: public google::cloud::golden_v1::GoldenRestOnlyConnectionIdempotencyPolicy {
public:
~CustomIdempotencyPolicy() override = default;
std::unique_ptr<google::cloud::golden_v1::GoldenRestOnlyConnectionIdempotencyPolicy> clone() const override {
return std::make_unique<CustomIdempotencyPolicy>(*this);
}
// Override inherited functions to define as needed.
};
//! [custom-idempotency-policy]

void SetRetryPolicy(std::vector<std::string> const& argv) {
if (!argv.empty()) {
throw google::cloud::testing_util::Usage{"set-client-retry-policy"};
}
//! [set-retry-policy]
auto options = google::cloud::Options{}
.set<google::cloud::golden_v1::GoldenRestOnlyConnectionIdempotencyPolicyOption>(
CustomIdempotencyPolicy().clone())
.set<google::cloud::golden_v1::GoldenRestOnlyRetryPolicyOption>(
google::cloud::golden_v1::GoldenRestOnlyLimitedErrorCountRetryPolicy(3).clone())
.set<google::cloud::golden_v1::GoldenRestOnlyBackoffPolicyOption>(
google::cloud::ExponentialBackoffPolicy(
/*initial_delay=*/std::chrono::milliseconds(200),
/*maximum_delay=*/std::chrono::seconds(45),
/*scaling=*/2.0).clone());
auto connection = google::cloud::golden_v1::MakeGoldenRestOnlyConnectionRest(options);

// c1 and c2 share the same retry policies
auto c1 = google::cloud::golden_v1::GoldenRestOnlyClient(connection);
auto c2 = google::cloud::golden_v1::GoldenRestOnlyClient(connection);

// You can override any of the policies in a new client. This new client
// will share the policies from c1 (or c2) *except* from the retry policy.
auto c3 = google::cloud::golden_v1::GoldenRestOnlyClient(
connection, google::cloud::Options{}.set<google::cloud::golden_v1::GoldenRestOnlyRetryPolicyOption>(
google::cloud::golden_v1::GoldenRestOnlyLimitedTimeRetryPolicy(std::chrono::minutes(5)).clone()));

// You can also override the policies in a single call:
// c3.SomeRpc(..., google::cloud::Options{}
// .set<google::cloud::golden_v1::GoldenRestOnlyRetryPolicyOption>(
// google::cloud::golden_v1::GoldenRestOnlyLimitedErrorCountRetryPolicy(10).clone()));
//! [set-client-client-retry-policy]
}

void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
Expand Down Expand Up @@ -77,6 +125,9 @@ void AutoRun(std::vector<std::string> const& argv) {
std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning SetRetryPolicy() example" << std::endl;
SetRetryPolicy({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}
Expand All @@ -86,6 +137,7 @@ void AutoRun(std::vector<std::string> const& argv) {
int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"set-retry-policy", SetRetryPolicy},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// source: generator/integration_tests/test.proto

#include "generator/integration_tests/golden/v1/golden_thing_admin_client.h"
#include "generator/integration_tests/golden/v1/golden_thing_admin_connection_idempotency_policy.h"
#include "generator/integration_tests/golden/v1/golden_thing_admin_options.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
Expand Down Expand Up @@ -45,6 +47,52 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
//! [set-client-endpoint]
}

//! [custom-idempotency-policy]
class CustomIdempotencyPolicy
: public google::cloud::golden_v1::GoldenThingAdminConnectionIdempotencyPolicy {
public:
~CustomIdempotencyPolicy() override = default;
std::unique_ptr<google::cloud::golden_v1::GoldenThingAdminConnectionIdempotencyPolicy> clone() const override {
return std::make_unique<CustomIdempotencyPolicy>(*this);
}
// Override inherited functions to define as needed.
};
//! [custom-idempotency-policy]

void SetRetryPolicy(std::vector<std::string> const& argv) {
if (!argv.empty()) {
throw google::cloud::testing_util::Usage{"set-client-retry-policy"};
}
//! [set-retry-policy]
auto options = google::cloud::Options{}
.set<google::cloud::golden_v1::GoldenThingAdminConnectionIdempotencyPolicyOption>(
CustomIdempotencyPolicy().clone())
.set<google::cloud::golden_v1::GoldenThingAdminRetryPolicyOption>(
google::cloud::golden_v1::GoldenThingAdminLimitedErrorCountRetryPolicy(3).clone())
.set<google::cloud::golden_v1::GoldenThingAdminBackoffPolicyOption>(
google::cloud::ExponentialBackoffPolicy(
/*initial_delay=*/std::chrono::milliseconds(200),
/*maximum_delay=*/std::chrono::seconds(45),
/*scaling=*/2.0).clone());
auto connection = google::cloud::golden_v1::MakeGoldenThingAdminConnection(options);

// c1 and c2 share the same retry policies
auto c1 = google::cloud::golden_v1::GoldenThingAdminClient(connection);
auto c2 = google::cloud::golden_v1::GoldenThingAdminClient(connection);

// You can override any of the policies in a new client. This new client
// will share the policies from c1 (or c2) *except* from the retry policy.
auto c3 = google::cloud::golden_v1::GoldenThingAdminClient(
connection, google::cloud::Options{}.set<google::cloud::golden_v1::GoldenThingAdminRetryPolicyOption>(
google::cloud::golden_v1::GoldenThingAdminLimitedTimeRetryPolicy(std::chrono::minutes(5)).clone()));

// You can also override the policies in a single call:
// c3.SomeRpc(..., google::cloud::Options{}
// .set<google::cloud::golden_v1::GoldenThingAdminRetryPolicyOption>(
// google::cloud::golden_v1::GoldenThingAdminLimitedErrorCountRetryPolicy(10).clone()));
//! [set-client-client-retry-policy]
}

void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
Expand Down Expand Up @@ -77,6 +125,9 @@ void AutoRun(std::vector<std::string> const& argv) {
std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning SetRetryPolicy() example" << std::endl;
SetRetryPolicy({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}
Expand All @@ -86,6 +137,7 @@ void AutoRun(std::vector<std::string> const& argv) {
int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"set-retry-policy", SetRetryPolicy},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
Expand Down
95 changes: 95 additions & 0 deletions generator/internal/sample_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Status SampleGenerator::GenerateHeader() {

HeaderLocalIncludes({
vars("client_header_path"),
vars("options_header_path"),
vars("idempotency_policy_header_path"),
"google/cloud/common_options.h",
"google/cloud/credentials.h",
"google/cloud/internal/getenv.h",
Expand Down Expand Up @@ -87,6 +89,95 @@ void SetClientEndpoint(std::vector<std::string> const& argv) {
//! [set-client-endpoint]
}

//! [custom-idempotency-policy]
class CustomIdempotencyPolicy
: public google::cloud::$product_namespace$::$idempotency_class_name$ {
public:
~CustomIdempotencyPolicy() override = default;
std::unique_ptr<google::cloud::$product_namespace$::$idempotency_class_name$> clone() const override {
return std::make_unique<CustomIdempotencyPolicy>(*this);
}
// Override inherited functions to define as needed.
};
//! [custom-idempotency-policy]

void SetRetryPolicy(std::vector<std::string> const& argv) {
if (!argv.empty()) {
throw google::cloud::testing_util::Usage{"set-client-retry-policy"};
}
//! [set-retry-policy]
auto options = google::cloud::Options{}
.set<google::cloud::$product_namespace$::$idempotency_class_name$Option>(
CustomIdempotencyPolicy().clone())
.set<google::cloud::$product_namespace$::$retry_policy_name$Option>(
google::cloud::$product_namespace$::$limited_error_count_retry_policy_name$(3).clone())
.set<google::cloud::$product_namespace$::$service_name$BackoffPolicyOption>(
google::cloud::ExponentialBackoffPolicy(
/*initial_delay=*/std::chrono::milliseconds(200),
/*maximum_delay=*/std::chrono::seconds(45),
/*scaling=*/2.0).clone());)""");
if (HasGenerateGrpcTransport()) {
HeaderPrint(R"""(
auto connection = google::cloud::$product_namespace$::Make$connection_class_name$()""");
} else {
HeaderPrint(R"""(
auto connection = google::cloud::$product_namespace$::Make$connection_class_name$Rest()""");
}
if (IsExperimental()) HeaderPrint("google::cloud::ExperimentalTag{}, ");
switch (endpoint_location_style) {
case ServiceConfiguration::LOCATION_DEPENDENT:
HeaderPrint(R"""("location-unused-in-this-example", )""");
break;
case ServiceConfiguration::LOCATION_DEPENDENT_COMPAT:
default:
break;
}
HeaderPrint(R"""(options);)""");
if (IsExperimental()) {
HeaderPrint(R"""(

// c1 and c2 share the same retry policies
auto c1 = google::cloud::$product_namespace$::$client_class_name$(
google::cloud::ExperimentalTag{}, connection);
auto c2 = google::cloud::$product_namespace$::$client_class_name$(
google::cloud::ExperimentalTag{}, connection);

// You can override any of the policies in a new client. This new client
// will share the policies from c1 (or c2) *except* from the retry policy.
devbww marked this conversation as resolved.
Show resolved Hide resolved
auto c3 = google::cloud::$product_namespace$::$client_class_name$(
google::cloud::ExperimentalTag{}, connection,
google::cloud::Options{}.set<google::cloud::$product_namespace$::$retry_policy_name$Option>(
google::cloud::$product_namespace$::$limited_time_retry_policy_name$(std::chrono::minutes(5)).clone()));

// You can also override the policies in a single call:
// c3.SomeRpc(..., google::cloud::Options{}
// .set<google::cloud::$product_namespace$::$retry_policy_name$Option>(
// google::cloud::$product_namespace$::$limited_error_count_retry_policy_name$(10).clone()));
//! [set-client-client-retry-policy]
coryan marked this conversation as resolved.
Show resolved Hide resolved
}
)""");
} else
HeaderPrint(R"""(

// c1 and c2 share the same retry policies
auto c1 = google::cloud::$product_namespace$::$client_class_name$(connection);
auto c2 = google::cloud::$product_namespace$::$client_class_name$(connection);

// You can override any of the policies in a new client. This new client
// will share the policies from c1 (or c2) *except* from the retry policy.
auto c3 = google::cloud::$product_namespace$::$client_class_name$(
connection, google::cloud::Options{}.set<google::cloud::$product_namespace$::$retry_policy_name$Option>(
google::cloud::$product_namespace$::$limited_time_retry_policy_name$(std::chrono::minutes(5)).clone()));

// You can also override the policies in a single call:
// c3.SomeRpc(..., google::cloud::Options{}
// .set<google::cloud::$product_namespace$::$retry_policy_name$Option>(
// google::cloud::$product_namespace$::$limited_error_count_retry_policy_name$(10).clone()));
//! [set-client-client-retry-policy]
}
)""");

HeaderPrint(R"""(
void WithServiceAccount(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw google::cloud::testing_util::Usage{"with-service-account <keyfile>"};
Expand Down Expand Up @@ -136,6 +227,9 @@ void AutoRun(std::vector<std::string> const& argv) {
std::cout << "\nRunning SetClientEndpoint() example" << std::endl;
SetClientEndpoint({});

std::cout << "\nRunning SetRetryPolicy() example" << std::endl;
SetRetryPolicy({});

std::cout << "\nRunning WithServiceAccount() example" << std::endl;
WithServiceAccount({keyfile});
}
Expand All @@ -145,6 +239,7 @@ void AutoRun(std::vector<std::string> const& argv) {
int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"set-client-endpoint", SetClientEndpoint},
{"set-retry-policy", SetRetryPolicy},
{"with-service-account", WithServiceAccount},
{"auto", AutoRun},
});
Expand Down
Loading