1
#pragma once
2

            
3
#include "envoy/common/callback.h"
4
#include "envoy/server/bootstrap_extension_config.h"
5
#include "envoy/server/lifecycle_notifier.h"
6
#include "envoy/stats/store.h"
7

            
8
#include "source/common/common/logger.h"
9
#include "source/extensions/bootstrap/dynamic_modules/extension_config.h"
10

            
11
namespace Envoy {
12
namespace Extensions {
13
namespace Bootstrap {
14
namespace DynamicModules {
15

            
16
/**
17
 * A bootstrap extension that uses a dynamic module.
18
 */
19
class DynamicModuleBootstrapExtension : public Server::BootstrapExtension,
20
                                        public Logger::Loggable<Logger::Id::dynamic_modules> {
21
public:
22
  DynamicModuleBootstrapExtension(DynamicModuleBootstrapExtensionConfigSharedPtr config);
23
  ~DynamicModuleBootstrapExtension() override;
24

            
25
  /**
26
   * Initializes the in-module extension.
27
   */
28
  void initializeInModuleExtension();
29

            
30
  // Server::BootstrapExtension
31
  void onServerInitialized(Server::Instance&) override;
32
  void onWorkerThreadInitialized() override;
33

            
34
  /**
35
   * Check if the extension has been destroyed.
36
   */
37
4
  bool isDestroyed() const { return destroyed_; }
38

            
39
  /**
40
   * Get the extension configuration.
41
   */
42
3
  const DynamicModuleBootstrapExtensionConfig& getExtensionConfig() const { return *config_; }
43

            
44
  /**
45
   * Get the stats store.
46
   */
47
12
  Stats::Store& statsStore() { return config_->stats_store_; }
48

            
49
  /**
50
   * Destroys the in-module extension. This is called by the destructor. It is safe to call
51
   * multiple times.
52
   */
53
  void destroy();
54

            
55
private:
56
  /**
57
   * Helper to get the `this` pointer as a void pointer.
58
   */
59
63
  void* thisAsVoidPtr() { return static_cast<void*>(this); }
60

            
61
  /**
62
   * Registers drain and shutdown lifecycle callbacks with the server.
63
   */
64
  void registerLifecycleCallbacks();
65

            
66
  // The configuration for this extension.
67
  DynamicModuleBootstrapExtensionConfigSharedPtr config_;
68

            
69
  // The in-module extension pointer.
70
  envoy_dynamic_module_type_bootstrap_extension_module_ptr in_module_extension_ = nullptr;
71

            
72
  // Whether the extension has been destroyed.
73
  bool destroyed_ = false;
74

            
75
  // Handle for the drain close callback registration. Dropped on destruction to unregister.
76
  Common::CallbackHandlePtr drain_handle_;
77

            
78
  // Handle for the shutdown lifecycle callback registration.
79
  Server::ServerLifecycleNotifier::HandlePtr shutdown_handle_;
80
};
81

            
82
} // namespace DynamicModules
83
} // namespace Bootstrap
84
} // namespace Extensions
85
} // namespace Envoy