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 
18 #include "google/cloud/spanner/retry_policy.h"
19 #include "google/cloud/spanner/version.h"
20 #include "google/cloud/backoff_policy.h"
21 #include "google/cloud/polling_policy.h"
22 #include "google/cloud/status.h"
23 
24 namespace google {
25 namespace cloud {
26 namespace spanner {
27 inline namespace SPANNER_CLIENT_NS {
28 
29 using PollingPolicy = ::google::cloud::PollingPolicy;
30 
31 /**
32  * Combine a RetryPolicy and a BackoffPolicy to create simple polling policies.
33  */
34 template <typename Retry = LimitedTimeRetryPolicy,
35           typename Backoff = ExponentialBackoffPolicy>
36 class GenericPollingPolicy : public PollingPolicy {
37  public:
GenericPollingPolicy(Retry retry_policy,Backoff backoff_policy)38   GenericPollingPolicy(Retry retry_policy, Backoff backoff_policy)
39       : retry_policy_(std::move(retry_policy)),
40         backoff_policy_(std::move(backoff_policy)) {}
41 
42   //@{
clone()43   std::unique_ptr<PollingPolicy> clone() const override {
44     return std::unique_ptr<PollingPolicy>(new GenericPollingPolicy(*this));
45   }
46 
OnFailure(google::cloud::Status const & status)47   bool OnFailure(google::cloud::Status const& status) override {
48     return retry_policy_.OnFailure(status);
49   }
50 
WaitPeriod()51   std::chrono::milliseconds WaitPeriod() override {
52     return backoff_policy_.OnCompletion();
53   }
54   //@}
55 
56  private:
57   Retry retry_policy_;
58   Backoff backoff_policy_;
59 };
60 
61 }  // namespace SPANNER_CLIENT_NS
62 }  // namespace spanner
63 }  // namespace cloud
64 }  // namespace google
65 
66 #endif  // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_POLLING_POLICY_H
67