Lines
100 %
Functions
#include "source/common/grpc/google_grpc_context.h"
#include <atomic>
#include "source/common/common/assert.h"
#include "source/common/common/lock_guard.h"
#include "source/common/common/macros.h"
#include "source/common/common/thread.h"
#ifdef ENVOY_GOOGLE_GRPC
#include "grpcpp/grpcpp.h"
#endif
namespace Envoy {
namespace Grpc {
GoogleGrpcContext::GoogleGrpcContext() : instance_tracker_(instanceTracker()) {
Thread::LockGuard lock(instance_tracker_.mutex_);
if (++instance_tracker_.live_instances_ == 1) {
grpc_init();
}
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.
ASSERT(instance_tracker_.live_instances_ > 0);
if (--instance_tracker_.live_instances_ == 0) {
grpc_shutdown_blocking(); // Waiting for quiescence avoids non-determinism in tests.
GoogleGrpcContext::InstanceTracker& GoogleGrpcContext::instanceTracker() {
MUTABLE_CONSTRUCT_ON_FIRST_USE(InstanceTracker);
} // namespace Grpc
} // namespace Envoy