This shows how to override the retry policies for tpu_v1::TpuClient:
auto options =
google::cloud::Options{}
.set<google::cloud::tpu_v1::TpuConnectionIdempotencyPolicyOption>(
CustomIdempotencyPolicy().clone())
.set<google::cloud::tpu_v1::TpuRetryPolicyOption>(
google::cloud::tpu_v1::TpuLimitedErrorCountRetryPolicy(3).clone())
.set<google::cloud::tpu_v1::TpuBackoffPolicyOption>(
google::cloud::ExponentialBackoffPolicy(
/*initial_delay=*/std::chrono::milliseconds(200),
/*maximum_delay=*/std::chrono::seconds(45),
/*scaling=*/2.0)
.clone());
auto connection = google::cloud::tpu_v1::MakeTpuConnection(options);
// c1 and c2 share the same retry policies
auto c1 = google::cloud::tpu_v1::TpuClient(connection);
auto c2 = google::cloud::tpu_v1::TpuClient(connection);
// You can override any of the policies in a new client. This new client
// will share the policies from c1 (or c2) *except* for the retry policy.
auto c3 = google::cloud::tpu_v1::TpuClient(
connection,
google::cloud::Options{}.set<google::cloud::tpu_v1::TpuRetryPolicyOption>(
google::cloud::tpu_v1::TpuLimitedTimeRetryPolicy(
std::chrono::minutes(5))
.clone()));
// You can also override the policies in a single call:
// c3.SomeRpc(..., google::cloud::Options{}
// .set<google::cloud::tpu_v1::TpuRetryPolicyOption>(
// google::cloud::tpu_v1::TpuLimitedErrorCountRetryPolicy(10).clone()));
Assuming you have created a custom idempotency policy. Such as:
class CustomIdempotencyPolicy
: public google::cloud::tpu_v1::TpuConnectionIdempotencyPolicy {
public:
~CustomIdempotencyPolicy() override = default;
std::unique_ptr<google::cloud::tpu_v1::TpuConnectionIdempotencyPolicy> clone()
const override {
return std::make_unique<CustomIdempotencyPolicy>(*this);
}
// Override inherited functions to define as needed.
};
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-14 UTC."],[[["\u003cp\u003eThis document provides information on how to override retry policies for \u003ccode\u003etpu_v1::TpuClient\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe latest available version is \u003ccode\u003e2.37.0-rc\u003c/code\u003e, and there are numerous older versions listed, starting from \u003ccode\u003e2.36.0\u003c/code\u003e down to \u003ccode\u003e2.11.0\u003c/code\u003e, with \u003ccode\u003e2.20.0\u003c/code\u003e being the initial starting version displayed.\u003c/p\u003e\n"],["\u003cp\u003eYou can set custom retry policies, idempotency, and backoff policies using \u003ccode\u003egoogle::cloud::Options\u003c/code\u003e and associated settings like \u003ccode\u003eTpuRetryPolicyOption\u003c/code\u003e, \u003ccode\u003eTpuBackoffPolicyOption\u003c/code\u003e, and \u003ccode\u003eTpuConnectionIdempotencyPolicyOption\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eNew clients can be created to share existing retry policies, or they can override specific policies such as retry policies while keeping others, as shown by the example of \u003ccode\u003ec3\u003c/code\u003e using \u003ccode\u003ec1\u003c/code\u003e and \u003ccode\u003ec2\u003c/code\u003e as reference.\u003c/p\u003e\n"],["\u003cp\u003eRetry policies can also be overridden on a single call using \u003ccode\u003egoogle::cloud::Options\u003c/code\u003e with the \u003ccode\u003eset\u003c/code\u003e function, and you can create custom idempotency policies by creating a class that inherits from \u003ccode\u003eTpuConnectionIdempotencyPolicy\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,[]]