Skip to content

Commit f164e35

Browse files
committed
Add exponential backoff retries during initialization.
Integrators are seeing transient failures, maybe this helps. We also conveniently already have a backoff utility. Bug: b/411131610 Change-Id: I75360835686702837aa0c190b6adad80045de534
1 parent d0fe4d6 commit f164e35

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

kmsp11/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ cc_library(
286286
":object_loader",
287287
":object_store",
288288
":object_store_state_cc_proto",
289+
"//common:backoff",
289290
"//common:kms_client",
290291
"//common:status_macros",
291292
"//kmsp11/config:config_cc_proto",

kmsp11/token.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "absl/container/flat_hash_set.h"
2020
#include "absl/status/statusor.h"
21+
#include "common/backoff.h"
2122
#include "common/kms_client.h"
2223
#include "common/status_macros.h"
2324
#include "kmsp11/object_loader.h"
@@ -88,7 +89,20 @@ absl::StatusOr<std::unique_ptr<Token>> Token::New(CK_SLOT_ID slot_id,
8889
std::unique_ptr<ObjectLoader> loader,
8990
ObjectLoader::New(token_config.key_ring(),
9091
token_config.certs(), generate_certs, allow_software_keys));
91-
ASSIGN_OR_RETURN(ObjectStoreState state, loader->BuildState(*kms_client));
92+
absl::StatusOr<ObjectStoreState> state_resp = loader->BuildState(*kms_client);
93+
94+
// Exponential backoff to reduce errors at library initialization.
95+
constexpr absl::Duration kMinDelay = absl::Milliseconds(10);
96+
constexpr absl::Duration kMaxDelay = absl::Seconds(1);
97+
int retries = 0;
98+
while (retries < 10 &&
99+
(state_resp.status().code() == absl::StatusCode::kDeadlineExceeded ||
100+
state_resp.status().code() == absl::StatusCode::kUnavailable)) {
101+
absl::SleepFor(ComputeBackoff(kMinDelay, kMaxDelay, retries++));
102+
state_resp = loader->BuildState(*kms_client);
103+
}
104+
105+
ASSIGN_OR_RETURN(ObjectStoreState state, state_resp);
92106
ASSIGN_OR_RETURN(std::unique_ptr<ObjectStore> store, ObjectStore::New(state));
93107

94108
// using `new` to invoke a private constructor

0 commit comments

Comments
 (0)