forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
grpc: Separate out grpc_init into its own class (envoyproxy#8293)
* Separate out grpc init to a separate class. Signed-off-by: Joshua Marantz <[email protected]>
- Loading branch information
Showing
14 changed files
with
162 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "common/grpc/google_grpc_context.h" | ||
|
||
#include <atomic> | ||
|
||
#include "common/common/assert.h" | ||
#include "common/common/lock_guard.h" | ||
#include "common/common/macros.h" | ||
#include "common/common/thread.h" | ||
|
||
#include "grpcpp/grpcpp.h" | ||
|
||
namespace Envoy { | ||
namespace Grpc { | ||
|
||
GoogleGrpcContext::GoogleGrpcContext() : instance_tracker_(instanceTracker()) { | ||
Thread::LockGuard lock(instance_tracker_.mutex_); | ||
if (++instance_tracker_.live_instances_ == 1) { | ||
#ifdef ENVOY_GOOGLE_GRPC | ||
grpc_init(); | ||
#endif | ||
} | ||
} | ||
|
||
GoogleGrpcContext::~GoogleGrpcContext() { | ||
// Per https://github.com/grpc/grpc/issues/20303 it is OK to call | ||
// grpc_shutdown_blocking() as long as no one can concurrently call | ||
// grpc_init(). We use check_format.py to ensure that this file contains the | ||
// only callers to grpc_init(), and the mutex to then make that guarantee | ||
// across users of this class. | ||
Thread::LockGuard lock(instance_tracker_.mutex_); | ||
ASSERT(instance_tracker_.live_instances_ > 0); | ||
if (--instance_tracker_.live_instances_ == 0) { | ||
#ifdef ENVOY_GOOGLE_GRPC | ||
grpc_shutdown_blocking(); // Waiting for quiescence avoids non-determinism in tests. | ||
#endif | ||
} | ||
} | ||
|
||
GoogleGrpcContext::InstanceTracker& GoogleGrpcContext::instanceTracker() { | ||
MUTABLE_CONSTRUCT_ON_FIRST_USE(InstanceTracker); | ||
} | ||
|
||
} // namespace Grpc | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include "common/common/thread.h" | ||
|
||
namespace Envoy { | ||
namespace Grpc { | ||
|
||
// Captures global grpc initialization and shutdown. Note that grpc | ||
// initialization starts several threads, so it is a little annoying to run them | ||
// alongside unrelated tests, particularly if they are trying to track memory | ||
// usage, or you are exploiting otherwise consistent run-to-run pointer values | ||
// during debug. | ||
// | ||
// Instantiating this class makes it easy to ensure classes that depend on grpc | ||
// libraries get them initialized. | ||
class GoogleGrpcContext { | ||
public: | ||
GoogleGrpcContext(); | ||
~GoogleGrpcContext(); | ||
|
||
private: | ||
struct InstanceTracker { | ||
Thread::MutexBasicLockable mutex_; | ||
uint64_t live_instances_ GUARDED_BY(mutex_) = 0; | ||
}; | ||
|
||
static InstanceTracker& instanceTracker(); | ||
|
||
InstanceTracker& instance_tracker_; | ||
}; | ||
|
||
} // namespace Grpc | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Envoy { | ||
|
||
void foo() { | ||
grpc_init(); | ||
} | ||
|
||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Envoy { | ||
|
||
void foo() { | ||
grpc_shutdown(); | ||
} | ||
|
||
} // namespace Envoy |