Skip to content

Commit 91da5f3

Browse files
authored
feat(common): support asynchronous RPCs in GrpcAuthenticationStrategy (#6391)
Supporting asynchronous gRPC requests requires that fetching any `grpc::CallCredentials` [1] asynchronously too, or the application would experience blocking while starting the asynchronous RPC. As an optimization I will introduce an optional `${service}Auth` layer, analogous to `${service}Logging` or `${service}Metadata`, this is to save the relatively expensive creation of a future when the type of credentials do not require them. This change does not include any integration tests, those will come later as they require even more infrastructure. **[1]** typically access tokens, but maybe other types too?
1 parent d85d7b1 commit 91da5f3

5 files changed

Lines changed: 30 additions & 4 deletions

google/cloud/internal/grpc_access_token_authentication.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,23 @@ std::shared_ptr<grpc::Channel> GrpcAccessTokenAuthentication::CreateChannel(
2626
return grpc::CreateCustomChannel(endpoint, credentials, arguments);
2727
}
2828

29+
bool GrpcAccessTokenAuthentication::RequiresConfigureContext() const {
30+
return true;
31+
}
32+
2933
Status GrpcAccessTokenAuthentication::ConfigureContext(
3034
grpc::ClientContext& context) {
3135
context.set_credentials(credentials_);
3236
return Status{};
3337
}
3438

39+
future<StatusOr<std::unique_ptr<grpc::ClientContext>>>
40+
GrpcAccessTokenAuthentication::AsyncConfigureContext(
41+
std::unique_ptr<grpc::ClientContext> context) {
42+
context->set_credentials(credentials_);
43+
return make_ready_future(make_status_or(std::move(context)));
44+
}
45+
3546
} // namespace internal
3647
} // namespace GOOGLE_CLOUD_CPP_NS
3748
} // namespace cloud

google/cloud/internal/grpc_access_token_authentication.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ class GrpcAccessTokenAuthentication : public GrpcAuthenticationStrategy {
3333
std::shared_ptr<grpc::Channel> CreateChannel(
3434
std::string const& endpoint,
3535
grpc::ChannelArguments const& arguments) override;
36+
bool RequiresConfigureContext() const override;
3637
Status ConfigureContext(grpc::ClientContext&) override;
38+
future<StatusOr<std::unique_ptr<grpc::ClientContext>>> AsyncConfigureContext(
39+
std::unique_ptr<grpc::ClientContext> context) override;
3740

3841
private:
3942
std::shared_ptr<grpc::CallCredentials> credentials_;

google/cloud/internal/grpc_channel_credentials_authentication.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,21 @@ GrpcChannelCredentialsAuthentication::CreateChannel(
2525
return grpc::CreateCustomChannel(endpoint, credentials_, arguments);
2626
}
2727

28+
bool GrpcChannelCredentialsAuthentication::RequiresConfigureContext() const {
29+
return false;
30+
}
31+
2832
Status GrpcChannelCredentialsAuthentication::ConfigureContext(
2933
grpc::ClientContext&) {
3034
return Status{};
3135
}
3236

37+
future<StatusOr<std::unique_ptr<grpc::ClientContext>>>
38+
GrpcChannelCredentialsAuthentication::AsyncConfigureContext(
39+
std::unique_ptr<grpc::ClientContext> context) {
40+
return make_ready_future(make_status_or(std::move(context)));
41+
}
42+
3343
} // namespace internal
3444
} // namespace GOOGLE_CLOUD_CPP_NS
3545
} // namespace cloud

google/cloud/internal/grpc_channel_credentials_authentication.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ class GrpcChannelCredentialsAuthentication : public GrpcAuthenticationStrategy {
3434
std::shared_ptr<grpc::Channel> CreateChannel(
3535
std::string const& endpoint,
3636
grpc::ChannelArguments const& arguments) override;
37+
bool RequiresConfigureContext() const override;
3738
Status ConfigureContext(grpc::ClientContext&) override;
39+
future<StatusOr<std::unique_ptr<grpc::ClientContext>>> AsyncConfigureContext(
40+
std::unique_ptr<grpc::ClientContext> context) override;
3841

3942
private:
4043
std::shared_ptr<grpc::ChannelCredentials> credentials_;

google/cloud/internal/unified_grpc_credentials.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ class GrpcAuthenticationStrategy {
3434

3535
virtual std::shared_ptr<grpc::Channel> CreateChannel(
3636
std::string const& endpoint, grpc::ChannelArguments const& arguments) = 0;
37+
virtual bool RequiresConfigureContext() const = 0;
3738
virtual Status ConfigureContext(grpc::ClientContext& context) = 0;
38-
// TODO(#6310) - support asynchronous refresh
39-
// virtual future<Status>
40-
// SetupAsync(std::unique_ptr<grpc::ClientContext>, CompletionQueue& cq) =
41-
// 0;
39+
virtual future<StatusOr<std::unique_ptr<grpc::ClientContext>>>
40+
AsyncConfigureContext(std::unique_ptr<grpc::ClientContext> context) = 0;
4241
};
4342

4443
std::unique_ptr<GrpcAuthenticationStrategy> CreateAuthenticationStrategy(

0 commit comments

Comments
 (0)