Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
polling_policy.h
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_POLLING_POLICY_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_POLLING_POLICY_H
17 
20 #include "google/cloud/status.h"
21 
22 namespace google {
23 namespace cloud {
24 namespace spanner {
25 inline namespace SPANNER_CLIENT_NS {
26 
47  public:
48  virtual ~PollingPolicy() = default;
49 
57  virtual std::unique_ptr<PollingPolicy> clone() const = 0;
58 
68  virtual bool OnFailure(google::cloud::Status const& status) = 0;
69 
73  virtual std::chrono::milliseconds WaitPeriod() = 0;
74 };
75 
79 template <typename Retry = LimitedTimeRetryPolicy,
80  typename Backoff = ExponentialBackoffPolicy>
82  public:
83  GenericPollingPolicy(Retry retry_policy, Backoff backoff_policy)
84  : retry_policy_(std::move(retry_policy)),
85  backoff_policy_(std::move(backoff_policy)) {}
86 
88  std::unique_ptr<PollingPolicy> clone() const override {
89  return std::unique_ptr<PollingPolicy>(new GenericPollingPolicy(*this));
90  }
91 
92  bool OnFailure(google::cloud::Status const& status) override {
93  return retry_policy_.OnFailure(status);
94  }
95 
96  std::chrono::milliseconds WaitPeriod() override {
97  return backoff_policy_.OnCompletion();
98  }
100 
101  private:
102  Retry retry_policy_;
103  Backoff backoff_policy_;
104 };
105 
106 } // namespace SPANNER_CLIENT_NS
107 } // namespace spanner
108 } // namespace cloud
109 } // namespace google
110 
111 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_POLLING_POLICY_H
GenericPollingPolicy(Retry retry_policy, Backoff backoff_policy)
std::unique_ptr< PollingPolicy > clone() const override
Return a copy of the current policy.
Control the Cloud Spanner C++ client library behavior with respect to polling on long running operati...
Contains all the Cloud Spanner C++ client types and functions.
Combine a RetryPolicy and a BackoffPolicy to create simple polling policies.
google::cloud::internal::LimitedTimeRetryPolicy< google::cloud::Status, internal::SafeGrpcRetry > LimitedTimeRetryPolicy
A retry policy that limits based on time.
Definition: retry_policy.h:65
google::cloud::internal::ExponentialBackoffPolicy ExponentialBackoffPolicy
A truncated exponential backoff policy with randomized periods.
bool OnFailure(google::cloud::Status const &status) override
A callback to indicate that a polling attempt failed.
#define SPANNER_CLIENT_NS
Definition: version.h:22
std::chrono::milliseconds WaitPeriod() override
How long should the polling loop wait before trying again.