Refactor BaseDataCollector to integrate ConnectionManager for connection handling

- Extracted connection management logic into a new `ConnectionManager` class, promoting separation of concerns and enhancing modularity.
- Updated `BaseDataCollector` to utilize the `ConnectionManager` for connection, disconnection, and reconnection processes, improving code clarity and maintainability.
- Refactored connection-related methods and attributes, ensuring consistent error handling and logging practices.
- Enhanced the `OKXCollector` to implement the new connection management approach, streamlining its connection logic.
- Added unit tests for the `ConnectionManager` to validate its functionality and ensure robust error handling.

These changes improve the architecture of the data collector, aligning with project standards for maintainability and performance.
This commit is contained in:
Vasily.onl
2025-06-09 17:42:06 +08:00
parent 60434afd5d
commit 41f0e8e6b6
8 changed files with 434 additions and 77 deletions

View File

@@ -109,7 +109,7 @@ class OKXCollector(BaseDataCollector):
symbol,
config=candle_config or CandleProcessingConfig(timeframes=self.timeframes), # Use provided config or create new one
component_name=f"{component_name}_processor",
logger=logger
logger=self.logger
)
# Add callbacks for processed data
@@ -140,6 +140,21 @@ class OKXCollector(BaseDataCollector):
"""
Establish connection to OKX WebSocket API.
Returns:
True if connection successful, False otherwise
"""
return await self._connection_manager.connect(self._actual_connect)
async def disconnect(self) -> None:
"""
Disconnect from OKX WebSocket API.
"""
await self._connection_manager.disconnect(self._actual_disconnect)
async def _actual_connect(self) -> bool:
"""
Implement the actual connection logic for OKX WebSocket API.
Returns:
True if connection successful, False otherwise
"""
@@ -157,7 +172,7 @@ class OKXCollector(BaseDataCollector):
pong_timeout=10.0,
max_reconnect_attempts=5,
reconnect_delay=5.0,
logger=self.logger # Pass the logger to enable ping/pong logging
logger=self.logger
)
# Add message callback
@@ -175,9 +190,9 @@ class OKXCollector(BaseDataCollector):
self._log_error(f"Error connecting OKX collector for {self.symbol}: {e}")
return False
async def disconnect(self) -> None:
async def _actual_disconnect(self) -> None:
"""
Disconnect from OKX WebSocket API.
Implement the actual disconnection logic for OKX WebSocket API.
"""
try:
self._log_info(f"Disconnecting OKX collector for {self.symbol}")