Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::integration::ThreadPoolBridge Class Reference

Bridge for thread pool integration implementing INetworkBridge. More...

#include <thread_pool_bridge.h>

Inheritance diagram for kcenon::network::integration::ThreadPoolBridge:
Inheritance graph
Collaboration diagram for kcenon::network::integration::ThreadPoolBridge:
Collaboration graph

Public Types

enum class  BackendType { ThreadSystem , CommonSystem , Custom }
 Type of thread pool backend. More...
 

Public Member Functions

 ThreadPoolBridge (std::shared_ptr< thread_pool_interface > pool, BackendType backend_type=BackendType::Custom)
 Construct bridge with custom thread pool.
 
 ~ThreadPoolBridge () override
 Destructor.
 
VoidResult initialize (const BridgeConfig &config) override
 Initialize the bridge with configuration.
 
VoidResult shutdown () override
 Shutdown the bridge.
 
bool is_initialized () const override
 Check if the bridge is initialized.
 
BridgeMetrics get_metrics () const override
 Get current metrics.
 
std::shared_ptr< thread_pool_interfaceget_thread_pool () const
 Get the underlying thread pool.
 
BackendType get_backend_type () const
 Get the backend type.
 
- Public Member Functions inherited from kcenon::network::integration::INetworkBridge
virtual ~INetworkBridge ()=default
 

Static Public Member Functions

static std::shared_ptr< ThreadPoolBridgefrom_thread_system (const std::string &pool_name="network_pool")
 Create bridge from thread_system.
 

Private Attributes

std::shared_ptr< thread_pool_interfacepool_
 
BackendType backend_type_
 
std::atomic< bool > initialized_ {false}
 
std::mutex metrics_mutex_
 
BridgeMetrics cached_metrics_
 

Detailed Description

Bridge for thread pool integration implementing INetworkBridge.

This class consolidates thread_system and common_system thread pool integrations into a single, unified bridge. It provides factory methods for creating bridges from different backend types.

Backend Types:

  • ThreadSystem: Uses thread_system's thread pool directly
  • CommonSystem: Adapts common_system's IExecutor
  • Custom: Uses user-provided thread_pool_interface

Lifecycle:

  1. Create using factory method (from_thread_system, from_common_system) or direct constructor
  2. Call initialize() with configuration
  3. Use get_thread_pool() to access the underlying pool
  4. Call shutdown() before destruction

Thread Safety:

Definition at line 81 of file thread_pool_bridge.h.

Member Enumeration Documentation

◆ BackendType

Type of thread pool backend.

Enumerator
ThreadSystem 

Uses thread_system's thread pool.

CommonSystem 

Uses common_system's IExecutor.

Custom 

Uses custom thread_pool_interface.

Definition at line 87 of file thread_pool_bridge.h.

87 {
90 Custom
91 };

Constructor & Destructor Documentation

◆ ThreadPoolBridge()

kcenon::network::integration::ThreadPoolBridge::ThreadPoolBridge ( std::shared_ptr< thread_pool_interface > pool,
BackendType backend_type = BackendType::Custom )
explicit

Construct bridge with custom thread pool.

Parameters
poolThread pool implementation
backend_typeType of backend (default: Custom)
Exceptions
std::invalid_argumentif pool is nullptr

Example:

auto pool = std::make_shared<basic_thread_pool>(8);
auto bridge = std::make_shared<ThreadPoolBridge>(pool);

Definition at line 15 of file thread_pool_bridge.cpp.

18 : pool_(std::move(pool)), backend_type_(backend_type) {
19 if (!pool_) {
20 throw std::invalid_argument("ThreadPoolBridge requires non-null thread pool");
21 }
22}
std::shared_ptr< thread_pool_interface > pool_

References pool_.

◆ ~ThreadPoolBridge()

kcenon::network::integration::ThreadPoolBridge::~ThreadPoolBridge ( )
override

Destructor.

Automatically calls shutdown() if initialized

Definition at line 24 of file thread_pool_bridge.cpp.

24 {
25 if (initialized_.load()) {
26 shutdown();
27 }
28}
VoidResult shutdown() override
Shutdown the bridge.

References initialized_, and shutdown().

Here is the call graph for this function:

Member Function Documentation

◆ from_thread_system()

std::shared_ptr< ThreadPoolBridge > kcenon::network::integration::ThreadPoolBridge::from_thread_system ( const std::string & pool_name = "network_pool")
static

Create bridge from thread_system.

Parameters
pool_nameThread pool name (default: "network_pool")
Returns
Shared pointer to ThreadPoolBridge

Creates a bridge using thread_system's thread pool via thread_integration_manager.

Example:

config.integration_name = "thread_system";
bridge->initialize(config);
static std::shared_ptr< ThreadPoolBridge > from_thread_system(const std::string &pool_name="network_pool")
Create bridge from thread_system.
tracing_config config
Definition exporters.cpp:29
Configuration for bridge initialization.

Definition at line 125 of file thread_pool_bridge.cpp.

126 {
127 (void)pool_name; // Pool name is informational only in current implementation
128
130 if (!pool) {
131 throw std::runtime_error("Failed to get thread pool from thread_integration_manager");
132 }
133
134 return std::make_shared<ThreadPoolBridge>(pool, BackendType::ThreadSystem);
135}
static thread_integration_manager & instance()
Get the singleton instance.
std::shared_ptr< thread_pool_interface > get_thread_pool()
Get the current thread pool.

References kcenon::network::integration::thread_integration_manager::get_thread_pool(), kcenon::network::integration::thread_integration_manager::instance(), and ThreadSystem.

Referenced by kcenon::network::integration::NetworkSystemBridge::create_default(), and kcenon::network::integration::NetworkSystemBridge::with_thread_system().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_backend_type()

ThreadPoolBridge::BackendType kcenon::network::integration::ThreadPoolBridge::get_backend_type ( ) const

Get the backend type.

Returns
Type of backend this bridge uses

Definition at line 121 of file thread_pool_bridge.cpp.

121 {
122 return backend_type_;
123}

References backend_type_.

◆ get_metrics()

BridgeMetrics kcenon::network::integration::ThreadPoolBridge::get_metrics ( ) const
overridevirtual

Get current metrics.

Returns
Bridge metrics including thread pool statistics

Custom Metrics:

  • "worker_threads": Number of worker threads
  • "pending_tasks": Number of queued tasks
  • "backend_type": Backend type (0=ThreadSystem, 1=CommonSystem, 2=Custom)

Thread Safety: Safe to call concurrently

Implements kcenon::network::integration::INetworkBridge.

Definition at line 93 of file thread_pool_bridge.cpp.

93 {
94 std::lock_guard<std::mutex> lock(metrics_mutex_);
95
96 if (!initialized_.load() || !pool_) {
97 BridgeMetrics metrics;
98 metrics.is_healthy = false;
99 metrics.last_activity = cached_metrics_.last_activity;
100 return metrics;
101 }
102
103 // Update metrics with current thread pool state
104 BridgeMetrics metrics = cached_metrics_;
105 metrics.is_healthy = pool_->is_running();
106 metrics.last_activity = std::chrono::steady_clock::now();
107 metrics.custom_metrics["worker_threads"] = static_cast<double>(pool_->worker_count());
108 metrics.custom_metrics["pending_tasks"] = static_cast<double>(pool_->pending_tasks());
109 metrics.custom_metrics["backend_type"] = static_cast<double>(backend_type_);
110
111 // Update cached metrics for next call
112 cached_metrics_ = metrics;
113
114 return metrics;
115}
std::chrono::steady_clock::time_point last_activity
Timestamp of last activity or health check.
bool is_healthy
Overall health status of the bridge.

References backend_type_, cached_metrics_, kcenon::network::integration::BridgeMetrics::custom_metrics, initialized_, kcenon::network::integration::BridgeMetrics::is_healthy, kcenon::network::integration::BridgeMetrics::last_activity, metrics_mutex_, and pool_.

◆ get_thread_pool()

std::shared_ptr< thread_pool_interface > kcenon::network::integration::ThreadPoolBridge::get_thread_pool ( ) const

Get the underlying thread pool.

Returns
Shared pointer to thread pool, or nullptr if not initialized

Thread Safety: Safe to call after initialization

Example:

if (auto pool = bridge->get_thread_pool()) {
pool->submit([]{ // task implementation here });
}

Definition at line 117 of file thread_pool_bridge.cpp.

117 {
118 return pool_;
119}

References pool_.

◆ initialize()

VoidResult kcenon::network::integration::ThreadPoolBridge::initialize ( const BridgeConfig & config)
overridevirtual

Initialize the bridge with configuration.

Parameters
configConfiguration parameters
Returns
ok() on success, error_info on failure

Configuration Properties:

  • "enabled": "true" or "false" (default: "true")
  • "worker_count": Number of worker threads (informational)
  • "pool_name": Thread pool identifier (informational)

Error Conditions:

  • Already initialized
  • Underlying thread pool not running
  • Invalid configuration

Example:

config.integration_name = "thread_system";
config.properties["pool_name"] = "network_pool";
config.properties["worker_count"] = "8";
auto result = bridge->initialize(config);
if (result.is_err()) {
std::cerr << "Init failed: " << result.error().message << std::endl;
}

Implements kcenon::network::integration::INetworkBridge.

Definition at line 30 of file thread_pool_bridge.cpp.

30 {
31 if (initialized_.load()) {
32 return error_void(
34 "ThreadPoolBridge already initialized",
35 "ThreadPoolBridge::initialize");
36 }
37
38 if (!pool_) {
39 return error_void(
41 "Thread pool is null",
42 "ThreadPoolBridge::initialize");
43 }
44
45 if (!pool_->is_running()) {
46 return error_void(
48 "Thread pool is not running",
49 "ThreadPoolBridge::initialize");
50 }
51
52 // Check if bridge is enabled (default: true)
53 auto enabled_it = config.properties.find("enabled");
54 if (enabled_it != config.properties.end() && enabled_it->second == "false") {
55 return error_void(
57 "Bridge is disabled in configuration",
58 "ThreadPoolBridge::initialize");
59 }
60
61 // Initialize metrics
62 std::lock_guard<std::mutex> lock(metrics_mutex_);
64 cached_metrics_.last_activity = std::chrono::steady_clock::now();
65 cached_metrics_.custom_metrics["worker_threads"] = static_cast<double>(pool_->worker_count());
66 cached_metrics_.custom_metrics["pending_tasks"] = static_cast<double>(pool_->pending_tasks());
67 cached_metrics_.custom_metrics["backend_type"] = static_cast<double>(backend_type_);
68
69 initialized_.store(true);
70 return ok();
71}
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
VoidResult ok()
std::map< std::string, double > custom_metrics
Bridge-specific custom metrics.

References kcenon::network::error_codes::common_errors::already_exists, backend_type_, cached_metrics_, config, kcenon::network::integration::BridgeMetrics::custom_metrics, kcenon::network::error_void(), initialized_, kcenon::network::error_codes::common_errors::invalid_argument, kcenon::network::integration::BridgeMetrics::is_healthy, kcenon::network::integration::BridgeMetrics::last_activity, metrics_mutex_, kcenon::network::error_codes::common_errors::not_initialized, kcenon::network::ok(), and pool_.

Here is the call graph for this function:

◆ is_initialized()

bool kcenon::network::integration::ThreadPoolBridge::is_initialized ( ) const
overridevirtual

Check if the bridge is initialized.

Returns
true if initialized and ready, false otherwise

Implements kcenon::network::integration::INetworkBridge.

Definition at line 89 of file thread_pool_bridge.cpp.

89 {
90 return initialized_.load() && pool_ && pool_->is_running();
91}

References initialized_, and pool_.

◆ shutdown()

VoidResult kcenon::network::integration::ThreadPoolBridge::shutdown ( )
overridevirtual

Shutdown the bridge.

Returns
ok() on success, error_info on failure

Shuts down the bridge but does not shut down the underlying thread pool. Thread pool lifecycle is managed externally.

This method is idempotent - multiple calls are safe.

Implements kcenon::network::integration::INetworkBridge.

Definition at line 73 of file thread_pool_bridge.cpp.

73 {
74 if (!initialized_.load()) {
75 return ok(); // Idempotent: already shut down
76 }
77
78 // Update metrics to reflect shutdown state
79 {
80 std::lock_guard<std::mutex> lock(metrics_mutex_);
82 cached_metrics_.last_activity = std::chrono::steady_clock::now();
83 }
84
85 initialized_.store(false);
86 return ok();
87}

References cached_metrics_, initialized_, kcenon::network::integration::BridgeMetrics::is_healthy, kcenon::network::integration::BridgeMetrics::last_activity, metrics_mutex_, and kcenon::network::ok().

Referenced by ~ThreadPoolBridge().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ backend_type_

BackendType kcenon::network::integration::ThreadPoolBridge::backend_type_
private

Definition at line 247 of file thread_pool_bridge.h.

Referenced by get_backend_type(), get_metrics(), and initialize().

◆ cached_metrics_

BridgeMetrics kcenon::network::integration::ThreadPoolBridge::cached_metrics_
mutableprivate

Definition at line 250 of file thread_pool_bridge.h.

Referenced by get_metrics(), initialize(), and shutdown().

◆ initialized_

std::atomic<bool> kcenon::network::integration::ThreadPoolBridge::initialized_ {false}
private

Definition at line 248 of file thread_pool_bridge.h.

248{false};

Referenced by get_metrics(), initialize(), is_initialized(), shutdown(), and ~ThreadPoolBridge().

◆ metrics_mutex_

std::mutex kcenon::network::integration::ThreadPoolBridge::metrics_mutex_
mutableprivate

Definition at line 249 of file thread_pool_bridge.h.

Referenced by get_metrics(), initialize(), and shutdown().

◆ pool_

std::shared_ptr<thread_pool_interface> kcenon::network::integration::ThreadPoolBridge::pool_
private

The documentation for this class was generated from the following files: