|
18 | 18 |
|
19 | 19 | #include "absl/container/flat_hash_set.h"
|
20 | 20 | #include "absl/status/statusor.h"
|
| 21 | +#include "common/backoff.h" |
21 | 22 | #include "common/kms_client.h"
|
22 | 23 | #include "common/status_macros.h"
|
23 | 24 | #include "kmsp11/object_loader.h"
|
@@ -88,7 +89,20 @@ absl::StatusOr<std::unique_ptr<Token>> Token::New(CK_SLOT_ID slot_id,
|
88 | 89 | std::unique_ptr<ObjectLoader> loader,
|
89 | 90 | ObjectLoader::New(token_config.key_ring(),
|
90 | 91 | 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); |
92 | 106 | ASSIGN_OR_RETURN(std::unique_ptr<ObjectStore> store, ObjectStore::New(state));
|
93 | 107 |
|
94 | 108 | // using `new` to invoke a private constructor
|
|
0 commit comments