forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_wide.cc
64 lines (53 loc) · 2.43 KB
/
process_wide.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "source/exe/process_wide.h"
#include "envoy/network/dns_resolver.h"
#include "source/common/common/assert.h"
#include "source/common/event/libevent.h"
#include "source/common/http/http2/nghttp2.h"
#include "source/server/proto_descriptors.h"
namespace Envoy {
namespace {
struct InitData {
uint32_t count_ ABSL_GUARDED_BY(mutex_){};
absl::Mutex mutex_;
};
// Static variable to count initialization pairs. For tests like
// main_common_test, we need to count to avoid double initialization or
// shutdown.
InitData& processWideInitData() { MUTABLE_CONSTRUCT_ON_FIRST_USE(InitData); };
} // namespace
ProcessWide::ProcessWide() {
// Note that the following lock has the dual use of making sure that initialization is complete
// before a second caller can enter and leave this function.
auto& init_data = processWideInitData();
absl::MutexLock lock(&init_data.mutex_);
if (init_data.count_++ == 0) {
// TODO(mattklein123): Audit the following as not all of these have to be re-initialized in the
// edge case where something does init/destroy/init/destroy.
Event::Libevent::Global::initialize();
Envoy::Server::validateProtoDescriptors();
Http::Http2::initializeNghttp2Logging();
// We do not initialize Google gRPC here -- we instead instantiate
// Grpc::GoogleGrpcContext in MainCommon immediately after instantiating
// ProcessWide. This is because ProcessWide is instantiated in the unit-test
// flow in test/test_runner.h, and grpc_init() instantiates threads which
// allocate memory asynchronous to running tests, making it hard to
// accurately measure memory consumption, and making unit-test debugging
// non-deterministic. See https://github.com/envoyproxy/envoy/issues/8282
// for details. Of course we also need grpc_init called in unit-tests that
// test Google gRPC, and the relevant classes must also instantiate
// Grpc::GoogleGrpcContext, which allows for nested instantiation.
//
// It appears that grpc_init() started instantiating threads in grpc 1.22.1,
// which was integrated in https://github.com/envoyproxy/envoy/pull/8196,
// around the time the flakes in #8282 started being reported.
}
}
ProcessWide::~ProcessWide() {
auto& init_data = processWideInitData();
absl::MutexLock lock(&init_data.mutex_);
ASSERT(init_data.count_ > 0);
if (--init_data.count_ == 0) {
Network::DnsResolverFactory::terminateFactories();
}
}
} // namespace Envoy