1
#include "source/extensions/bootstrap/dynamic_modules/extension.h"
2

            
3
namespace Envoy {
4
namespace Extensions {
5
namespace Bootstrap {
6
namespace DynamicModules {
7

            
8
DynamicModuleBootstrapExtension::DynamicModuleBootstrapExtension(
9
    DynamicModuleBootstrapExtensionConfigSharedPtr config)
10
23
    : config_(config) {}
11

            
12
23
DynamicModuleBootstrapExtension::~DynamicModuleBootstrapExtension() { destroy(); }
13

            
14
23
void DynamicModuleBootstrapExtension::initializeInModuleExtension() {
15
23
  in_module_extension_ =
16
23
      config_->on_bootstrap_extension_new_(config_->in_module_config_, thisAsVoidPtr());
17
23
}
18

            
19
24
void DynamicModuleBootstrapExtension::destroy() {
20
24
  if (in_module_extension_ != nullptr) {
21
22
    config_->on_bootstrap_extension_destroy_(in_module_extension_);
22
22
    in_module_extension_ = nullptr;
23
22
  }
24
24
  destroyed_ = true;
25
24
}
26

            
27
16
void DynamicModuleBootstrapExtension::onServerInitialized(Server::Instance& server) {
28
16
  if (in_module_extension_ == nullptr) {
29
1
    return;
30
1
  }
31
15
  config_->setListenerManager(server.listenerManager());
32
15
  config_->on_bootstrap_extension_server_initialized_(thisAsVoidPtr(), in_module_extension_);
33
15
  registerLifecycleCallbacks();
34
15
}
35

            
36
13
void DynamicModuleBootstrapExtension::onWorkerThreadInitialized() {
37
13
  if (in_module_extension_ == nullptr) {
38
1
    return;
39
1
  }
40
12
  config_->on_bootstrap_extension_worker_thread_initialized_(thisAsVoidPtr(), in_module_extension_);
41
12
}
42

            
43
15
void DynamicModuleBootstrapExtension::registerLifecycleCallbacks() {
44
  // Register for drain notifications via the server-wide drain manager.
45
15
  drain_handle_ = config_->context_.drainManager().addOnDrainCloseCb(
46
15
      Network::DrainDirection::All, [this](std::chrono::milliseconds) -> absl::Status {
47
1
        if (in_module_extension_ != nullptr) {
48
1
          ENVOY_LOG(debug, "dynamic module bootstrap extension drain started");
49
1
          config_->on_bootstrap_extension_drain_started_(thisAsVoidPtr(), in_module_extension_);
50
1
        }
51
1
        return absl::OkStatus();
52
1
      });
53

            
54
  // Register for shutdown notifications via the server lifecycle notifier.
55
15
  shutdown_handle_ = config_->context_.lifecycleNotifier().registerCallback(
56
15
      Server::ServerLifecycleNotifier::Stage::ShutdownExit, [this](Event::PostCb completion_cb) {
57
13
        if (in_module_extension_ != nullptr) {
58
12
          ENVOY_LOG(debug, "dynamic module bootstrap extension shutdown started");
59
          // Wrap the completion callback in a heap-allocated std::function so it can be passed
60
          // through the C ABI as an opaque context pointer.
61
12
          auto* completion = new Event::PostCb(std::move(completion_cb));
62
12
          config_->on_bootstrap_extension_shutdown_(
63
12
              thisAsVoidPtr(), in_module_extension_,
64
12
              [](void* context) {
65
12
                auto* cb = static_cast<Event::PostCb*>(context);
66
12
                (*cb)();
67
12
                delete cb;
68
12
              },
69
12
              static_cast<void*>(completion));
70
12
        } else {
71
1
          completion_cb();
72
1
        }
73
13
      });
74
15
}
75

            
76
} // namespace DynamicModules
77
} // namespace Bootstrap
78
} // namespace Extensions
79
} // namespace Envoy