Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for SQL-ODBC AWS Init and Shutdown Behaviour #163

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions sql-odbc/src/sqlodbc/opensearch_communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
// clang-format off
#include "opensearch_odbc.h"
#include "mylog.h"
#include <atomic>
#include <mutex>
#include <aws/core/utils/StringUtils.h>
#include <aws/core/client/RetryStrategy.h>
#include <aws/core/client/AWSClient.h>
Expand Down Expand Up @@ -121,6 +123,40 @@ static const std::string ERROR_RESPONSE_SCHEMA = R"EOF(
}
)EOF";

namespace {
/**
* A helper class to initialize/shutdown AWS API once per DLL load/unload.
*/
class AwsSdkHelper {
public:
AwsSdkHelper() :
m_reference_count(0) {
}

AwsSdkHelper& operator++() {
if (1 == ++m_reference_count) {
std::scoped_lock lock(m_mutex);
Aws::InitAPI(m_sdk_options);
}
return *this;
}

AwsSdkHelper& operator--() {
if (0 == --m_reference_count) {
std::scoped_lock lock(m_mutex);
Aws::ShutdownAPI(m_sdk_options);
}
return *this;
}

Aws::SDKOptions m_sdk_options;
std::atomic<int> m_reference_count;
std::mutex m_mutex;
};

AwsSdkHelper AWS_SDK_HELPER;
}

void OpenSearchCommunication::AwsHttpResponseToString(
std::shared_ptr< Aws::Http::HttpResponse > response, std::string& output) {
// This function has some unconventional stream operations because we need
Expand Down Expand Up @@ -237,13 +273,11 @@ OpenSearchCommunication::OpenSearchCommunication()
#pragma clang diagnostic pop
#endif // __APPLE__
{
LogMsg(OPENSEARCH_ALL, "Initializing Aws API.");
Aws::InitAPI(m_options);
++AWS_SDK_HELPER;
}

OpenSearchCommunication::~OpenSearchCommunication() {
LogMsg(OPENSEARCH_ALL, "Shutting down Aws API.");
Aws::ShutdownAPI(m_options);
--AWS_SDK_HELPER;
}

std::string OpenSearchCommunication::GetErrorMessage() {
Expand Down
1 change: 0 additions & 1 deletion sql-odbc/src/sqlodbc/opensearch_communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class OpenSearchCommunication {
OpenSearchResultQueue m_result_queue;
runtime_options m_rt_opts;
std::string m_client_encoding;
Aws::SDKOptions m_options;
std::string m_response_str;
std::shared_ptr< Aws::Http::HttpClient > m_http_client;
std::string m_error_message_to_user;
Expand Down