Skip initialisation if not running in own thread

Summary:
One design goal of sonar is to never cause the host app to crash or hang.
For this reason, we do all heavy work in a background thread.
If we detect that it's not running in it's own thread, just return so we don't hold up the caller.

Reviewed By: danielbuechele

Differential Revision: D8767288

fbshipit-source-id: e146cc2cfe5c3e62d12f527ff79f24c74873d4ff
This commit is contained in:
John Knox
2018-07-09 07:46:09 -07:00
committed by Facebook Github Bot
parent 85e6bf6d51
commit d0ecb46d64
2 changed files with 11 additions and 0 deletions

View File

@@ -33,6 +33,8 @@
#define SONAR_CA_FILE_NAME "sonarCA.crt"
#define CLIENT_CERT_FILE_NAME "device.crt"
#define PRIVATE_KEY_FILE "privateKey.pem"
#define WRONG_THREAD_EXIT_MSG \
"ERROR: Aborting sonar initialization because it's not running in the sonar thread."
static constexpr int reconnectIntervalSeconds = 2;
static constexpr int connectionKeepaliveSeconds = 10;
@@ -104,6 +106,10 @@ void SonarWebSocketImpl::start() {
}
void SonarWebSocketImpl::startSync() {
if (!isRunningInOwnThread()) {
SONAR_LOG(WRONG_THREAD_EXIT_MSG);
return;
}
if (isOpen()) {
SONAR_LOG("Already connected");
return;
@@ -289,6 +295,10 @@ bool SonarWebSocketImpl::ensureSonarDirExists() {
}
}
bool SonarWebSocketImpl::isRunningInOwnThread() {
return sonarEventBase_->isInEventBaseThread();
}
bool fileExists(std::string fileName) {
struct stat buffer;
return stat(fileName.c_str(), &buffer) == 0;

View File

@@ -62,6 +62,7 @@ class SonarWebSocketImpl : public SonarWebSocket {
bool isCertificateExchangeNeeded();
void requestSignedCertFromSonar();
bool ensureSonarDirExists();
bool isRunningInOwnThread();
};
} // namespace sonar