TensorFlow Serving C++ API Documentation
retrier.cc
1 /* Copyright 2017 Google Inc. All Rights Reserved.
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 
16 #include "tensorflow_serving/util/retrier.h"
17 
18 #include <functional>
19 
20 #include "absl/status/status.h"
21 #include "tensorflow/core/platform/env.h"
22 #include "tensorflow/core/platform/logging.h"
23 
24 namespace tensorflow {
25 namespace serving {
26 
27 absl::Status Retry(const string& description, uint32 max_num_retries,
28  int64_t retry_interval_micros,
29  const std::function<absl::Status()>& retried_fn,
30  const std::function<bool(absl::Status)>& should_retry) {
31  absl::Status status;
32  int num_tries = 0;
33  do {
34  if (num_tries > 0) {
35  Env::Default()->SleepForMicroseconds(retry_interval_micros);
36  LOG(INFO) << "Retrying of " << description << " retry: " << num_tries;
37  }
38  status = retried_fn();
39  if (!status.ok()) {
40  LOG(ERROR) << description << " failed: " << status;
41  }
42  ++num_tries;
43  } while (!status.ok() && num_tries < max_num_retries + 1 &&
44  should_retry(status));
45 
46  if (!should_retry(status)) {
47  LOG(INFO) << "Retrying of " << description << " was cancelled.";
48  }
49  if (num_tries == max_num_retries + 1) {
50  LOG(INFO) << "Retrying of " << description
51  << " exhausted max_num_retries: " << max_num_retries;
52  }
53  return status;
54 }
55 
56 } // namespace serving
57 } // namespace tensorflow