Update 更新文档
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
#ifndef FAULT_TOLERANCE_MANAGER_H
|
||||
#define FAULT_TOLERANCE_MANAGER_H
|
||||
#include <QObject>
|
||||
#include <QTimer>
|
||||
#include <QElapsedTimer>
|
||||
#include <QMutex>
|
||||
#include <QMap>
|
||||
#include <QSet>
|
||||
#include "../common_structures/TradingStructures.h"
|
||||
struct FaultToleranceConfig {
|
||||
int heartbeatIntervalMs = 5000;
|
||||
int heartbeatTimeoutMs = 15000;
|
||||
int maxReconnectAttempts = 3;
|
||||
int reconnectDelayMs = 2000;
|
||||
bool autoReconnectEnabled = true;
|
||||
bool failoverEnabled = true;
|
||||
QStringList backupServers;
|
||||
};
|
||||
struct ConnectionStats {
|
||||
qint64 totalConnections = 0;
|
||||
qint64 failedConnections = 0;
|
||||
qint64 successfulConnections = 0;
|
||||
qint64 totalReconnects = 0;
|
||||
double availabilityRate = 0.0;
|
||||
QDateTime lastConnectionTime;
|
||||
QDateTime lastFailureTime;
|
||||
void updateStats(bool success) {
|
||||
totalConnections++;
|
||||
if (success) {
|
||||
successfulConnections++;
|
||||
lastConnectionTime = QDateTime::currentDateTime();
|
||||
} else {
|
||||
failedConnections++;
|
||||
lastFailureTime = QDateTime::currentDateTime();
|
||||
}
|
||||
availabilityRate = totalConnections > 0 ?
|
||||
static_cast<double>(successfulConnections) / totalConnections : 0.0;
|
||||
}
|
||||
};
|
||||
class FaultToleranceManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static FaultToleranceManager* instance();
|
||||
void setConfig(const FaultToleranceConfig& config);
|
||||
FaultToleranceConfig getConfig() const;
|
||||
bool connectToServer(const QString& serverAddress, int port);
|
||||
void disconnectFromServer();
|
||||
void reconnect();
|
||||
Trading::ConnectionState getConnectionState() const;
|
||||
bool isConnected() const;
|
||||
bool isDegraded() const;
|
||||
ConnectionStats getStatistics() const;
|
||||
void startHeartbeat();
|
||||
void stopHeartbeat();
|
||||
void sendHeartbeat();
|
||||
void enableFailover(bool enabled);
|
||||
void addBackupServer(const QString& serverAddress, int port);
|
||||
void removeBackupServer(const QString& serverAddress);
|
||||
QStringList getBackupServers() const;
|
||||
void triggerFailover();
|
||||
void resetConnection();
|
||||
void forceReconnect();
|
||||
signals:
|
||||
void connectionStateChanged(Trading::ConnectionState newState, Trading::ConnectionState oldState);
|
||||
void connectionEstablished();
|
||||
void connectionLost();
|
||||
void connectionDegraded();
|
||||
void heartbeatTimeout();
|
||||
void failoverTriggered(const QString& newServer);
|
||||
void reconnectAttempt(int attempt, int maxAttempts);
|
||||
void connectionStatsUpdated(const ConnectionStats& stats);
|
||||
private:
|
||||
explicit FaultToleranceManager(QObject *parent = nullptr);
|
||||
~FaultToleranceManager();
|
||||
void initialize();
|
||||
void cleanup();
|
||||
void updateConnectionState(Trading::ConnectionState newState);
|
||||
void attemptReconnect();
|
||||
void performFailover();
|
||||
void updateHeartbeat();
|
||||
void checkHeartbeatTimeout();
|
||||
Trading::ConnectionState m_currentState = Trading::ConnectionState::Disconnected;
|
||||
QString m_currentServer;
|
||||
int m_currentPort = 0;
|
||||
int m_reconnectAttempts = 0;
|
||||
FaultToleranceConfig m_config;
|
||||
ConnectionStats m_stats;
|
||||
QTimer* m_heartbeatTimer;
|
||||
QTimer* m_reconnectTimer;
|
||||
QElapsedTimer m_lastHeartbeatTime;
|
||||
QMap<QString, int> m_backupServers;
|
||||
mutable QMutex m_mutex;
|
||||
};
|
||||
#endif
|
||||
Reference in New Issue
Block a user