Skip to content

Commit 596bae3

Browse files
authored
Fix slow empty credentials issue (#3544)
Optimize credential retrieval performance when credentials are empty by improving EC2 instance profile configuration loading and HTTP resource client behavior.
1 parent a07b837 commit 596bae3

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

src/aws-cpp-sdk-core/include/aws/core/internal/AWSHttpResourceClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ namespace Aws
160160
};
161161

162162
void AWS_CORE_API InitEC2MetadataClient();
163+
void AWS_CORE_API InitEC2MetadataClient(const Aws::Client::ClientConfiguration::CredentialProviderConfiguration& credentialConfig);
163164
void AWS_CORE_API CleanupEC2MetadataClient();
164165
std::shared_ptr<EC2MetadataClient> AWS_CORE_API GetEC2MetadataClient();
165166

src/aws-cpp-sdk-core/source/config/EC2InstanceProfileConfigLoader.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ namespace Aws
4040
}
4141

4242
EC2InstanceProfileConfigLoader::EC2InstanceProfileConfigLoader(const Aws::Client::ClientConfiguration::CredentialProviderConfiguration& credentialConfig)
43-
: m_ec2metadataClient(Aws::MakeShared<Aws::Internal::EC2MetadataClient>(EC2_INSTANCE_PROFILE_LOG_TAG, credentialConfig))
44-
{}
43+
{
44+
Aws::Internal::InitEC2MetadataClient(credentialConfig);
45+
m_ec2metadataClient = Aws::Internal::GetEC2MetadataClient();
46+
}
4547

4648
bool EC2InstanceProfileConfigLoader::LoadInternal()
4749
{

src/aws-cpp-sdk-core/source/internal/AWSHttpResourceClient.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -432,21 +432,8 @@ namespace Aws
432432
return Aws::String(m_endpoint);
433433
}
434434

435-
#ifdef _MSC_VER
436-
// VS2015 compiler's bug, warning s_ec2metadataClient: symbol will be dynamically initialized (implementation limitation)
437-
AWS_SUPPRESS_WARNING(4592,
438-
static std::shared_ptr<EC2MetadataClient> s_ec2metadataClient(nullptr);
439-
)
440-
#else
441-
static std::shared_ptr<EC2MetadataClient> s_ec2metadataClient(nullptr);
442-
#endif
443-
444-
void InitEC2MetadataClient()
435+
static Aws::String CalculateEC2MetadataServiceEndpoint()
445436
{
446-
if (s_ec2metadataClient)
447-
{
448-
return;
449-
}
450437
Aws::String ec2MetadataServiceEndpoint = Aws::Environment::GetEnv("AWS_EC2_METADATA_SERVICE_ENDPOINT");
451438
if (ec2MetadataServiceEndpoint.empty())
452439
{
@@ -478,6 +465,36 @@ namespace Aws
478465
}
479466
}
480467
}
468+
return ec2MetadataServiceEndpoint;
469+
}
470+
471+
#ifdef _MSC_VER
472+
// VS2015 compiler's bug, warning s_ec2metadataClient: symbol will be dynamically initialized (implementation limitation)
473+
AWS_SUPPRESS_WARNING(4592,
474+
static std::shared_ptr<EC2MetadataClient> s_ec2metadataClient(nullptr);
475+
)
476+
#else
477+
static std::shared_ptr<EC2MetadataClient> s_ec2metadataClient(nullptr);
478+
#endif
479+
480+
void InitEC2MetadataClient(const Aws::Client::ClientConfiguration::CredentialProviderConfiguration& credentialConfig)
481+
{
482+
if (s_ec2metadataClient)
483+
{
484+
return;
485+
}
486+
Aws::String ec2MetadataServiceEndpoint = CalculateEC2MetadataServiceEndpoint();
487+
AWS_LOGSTREAM_INFO(EC2_METADATA_CLIENT_LOG_TAG, "Using IMDS endpoint: " << ec2MetadataServiceEndpoint);
488+
s_ec2metadataClient = Aws::MakeShared<EC2MetadataClient>(EC2_METADATA_CLIENT_LOG_TAG, credentialConfig, ec2MetadataServiceEndpoint.c_str());
489+
}
490+
491+
void InitEC2MetadataClient()
492+
{
493+
if (s_ec2metadataClient)
494+
{
495+
return;
496+
}
497+
Aws::String ec2MetadataServiceEndpoint = CalculateEC2MetadataServiceEndpoint();
481498
AWS_LOGSTREAM_INFO(EC2_METADATA_CLIENT_LOG_TAG, "Using IMDS endpoint: " << ec2MetadataServiceEndpoint);
482499
s_ec2metadataClient = Aws::MakeShared<EC2MetadataClient>(EC2_METADATA_CLIENT_LOG_TAG, ec2MetadataServiceEndpoint.c_str());
483500
}

0 commit comments

Comments
 (0)