mirror of
https://github.com/oatpp/oatpp.git
synced 2025-04-18 19:00:23 +08:00
Api docs. ConnectionMonitor.
This commit is contained in:
parent
42bbaf3224
commit
46a2f98758
@ -29,12 +29,21 @@
|
||||
|
||||
namespace oatpp { namespace network { namespace monitor {
|
||||
|
||||
/**
|
||||
* ConnectionInactivityChecker - checks if a connection is inactive (has no read/writes) and whether it should be closed.
|
||||
* Extends - &id:oatpp::network::monitor::MetricsChecker;.
|
||||
*/
|
||||
class ConnectionInactivityChecker : public MetricsChecker {
|
||||
private:
|
||||
std::chrono::duration<v_int64, std::micro> m_lastReadTimeout;
|
||||
std::chrono::duration<v_int64, std::micro> m_lastWriteTimeout;
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param lastReadTimeout - how long can live connection without reads.
|
||||
* @param lastWriteTimeout - how long can live connection without writes.
|
||||
*/
|
||||
ConnectionInactivityChecker(const std::chrono::duration<v_int64, std::micro>& lastReadTimeout,
|
||||
const std::chrono::duration<v_int64, std::micro>& lastWriteTimeout);
|
||||
|
||||
|
@ -29,11 +29,19 @@
|
||||
|
||||
namespace oatpp { namespace network { namespace monitor {
|
||||
|
||||
/**
|
||||
* ConnectionMaxAgeChecker - checks if connection is too old and should be closed.
|
||||
* Extends - &id:oatpp::network::monitor::MetricsChecker;.
|
||||
*/
|
||||
class ConnectionMaxAgeChecker : public MetricsChecker {
|
||||
private:
|
||||
std::chrono::duration<v_int64, std::micro> m_maxAge;
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param maxAge - how long should connection live.
|
||||
*/
|
||||
ConnectionMaxAgeChecker(const std::chrono::duration<v_int64, std::micro>& maxAge);
|
||||
|
||||
std::vector<oatpp::String> getMetricsList();
|
||||
|
@ -35,6 +35,10 @@
|
||||
|
||||
namespace oatpp { namespace network { namespace monitor {
|
||||
|
||||
/**
|
||||
* ConnectionMonitor is a middleman who's able to manage provided connections
|
||||
* and close those ones that are not satisfy selected rules.
|
||||
*/
|
||||
class ConnectionMonitor : public ClientConnectionProvider, public ServerConnectionProvider {
|
||||
private:
|
||||
|
||||
@ -118,6 +122,10 @@ private:
|
||||
std::shared_ptr<ConnectionProvider> m_connectionProvider;
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param connectionProvider - underlying connection provider.
|
||||
*/
|
||||
ConnectionMonitor(const std::shared_ptr<ConnectionProvider>& connectionProvider);
|
||||
|
||||
std::shared_ptr<data::stream::IOStream> get() override;
|
||||
@ -126,6 +134,10 @@ public:
|
||||
|
||||
void addStatCollector(const std::shared_ptr<StatCollector>& collector);
|
||||
|
||||
/**
|
||||
* Add metrics checker.
|
||||
* @param checker - &id:oatpp::network::monitor::MetricsChecker;.
|
||||
*/
|
||||
void addMetricsChecker(const std::shared_ptr<MetricsChecker>& checker);
|
||||
|
||||
void invalidate(const std::shared_ptr<data::stream::IOStream>& connection) override;
|
||||
|
@ -29,13 +29,39 @@
|
||||
|
||||
namespace oatpp { namespace network { namespace monitor {
|
||||
|
||||
/**
|
||||
* MetricsChecker checks &id:oatpp::network::monitor::ConnectionStats; if those are satisfy the rule.
|
||||
*/
|
||||
class MetricsChecker : public oatpp::base::Countable {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Default virtual destructor.
|
||||
*/
|
||||
virtual ~MetricsChecker() = default;
|
||||
|
||||
/**
|
||||
* Get list of metrics names that are checked by this MetricsChecker.
|
||||
* @return
|
||||
*/
|
||||
virtual std::vector<oatpp::String> getMetricsList() = 0;
|
||||
|
||||
/**
|
||||
* Create &id:oatpp::network::monitor::StatCollector; for given `metricName`.
|
||||
* This method will be called by &id:oatpp::network::monitor::ConnectionMonitor; only if there is
|
||||
* no such `StatCollector` registered in the `ConnectionMonitor` yet.
|
||||
* @param metricName - name of the metric.
|
||||
* @return - &id:oatpp::network::monitor::StatCollector;.
|
||||
*/
|
||||
virtual std::shared_ptr<StatCollector> createStatCollector(const oatpp::String& metricName) = 0;
|
||||
|
||||
/**
|
||||
* Called by &id:oatpp::network::monitor::ConnectionMonitor; on each
|
||||
* time interval to check if connection satisfies the rule.
|
||||
* @param stats - &id:oatpp::network::monitor::ConnectionStats;.
|
||||
* @param currMicroTime - current time microseconds.
|
||||
* @return - `true` if connection satisfies the rule. `false` if connection should be closed.
|
||||
*/
|
||||
virtual bool check(const ConnectionStats& stats, v_int64 currMicroTime) = 0;
|
||||
|
||||
};
|
||||
|
@ -30,31 +30,99 @@
|
||||
|
||||
namespace oatpp { namespace network { namespace monitor {
|
||||
|
||||
/**
|
||||
* ConnectionStats.
|
||||
*/
|
||||
struct ConnectionStats {
|
||||
|
||||
/**
|
||||
* Timestamp created microseconds.
|
||||
* When connection was created.
|
||||
*/
|
||||
v_int64 timestampCreated = 0;
|
||||
|
||||
/**
|
||||
* Total bytes read from the connection.
|
||||
* Logs all bytes when the `read` method is called.
|
||||
*/
|
||||
v_io_size totalRead = 0;
|
||||
|
||||
/**
|
||||
* Total bytes written to the connection.
|
||||
* Logs all bytes when the `write` method is called.
|
||||
*/
|
||||
v_io_size totalWrite = 0;
|
||||
|
||||
/**
|
||||
* Timestamp microseconds when the last successful read was performed on the connection.
|
||||
*/
|
||||
v_int64 timestampLastRead = 0;
|
||||
|
||||
/**
|
||||
* Timestamp microseconds when the last successful write was performed on the connection.
|
||||
*/
|
||||
v_int64 timestampLastWrite = 0;
|
||||
|
||||
/**
|
||||
* Amount of bytes read during the last successful read.
|
||||
*/
|
||||
v_io_size lastReadSize = 0;
|
||||
|
||||
/**
|
||||
* Amount of bytes written during the last successful write.
|
||||
*/
|
||||
v_io_size lastWriteSize = 0;
|
||||
|
||||
/**
|
||||
* Data collected by stat-collectors - &l:StatCollector;
|
||||
*/
|
||||
std::unordered_map<oatpp::String, void*> metricsData;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* StatCollector collects metrics data of the connection.
|
||||
*/
|
||||
class StatCollector : public oatpp::base::Countable {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Default virtual destructor.
|
||||
*/
|
||||
virtual ~StatCollector() = default;
|
||||
|
||||
/**
|
||||
* Unique metric name that is collected by this `StatCollector`.
|
||||
* @return - metricName. &id:oatpp::String;.
|
||||
*/
|
||||
virtual oatpp::String metricName() = 0;
|
||||
|
||||
/**
|
||||
* Metric data constructor.
|
||||
* @return
|
||||
*/
|
||||
virtual void* createMetricData() = 0;
|
||||
|
||||
/**
|
||||
* Metric data destructor.
|
||||
* @param metricData
|
||||
*/
|
||||
virtual void deleteMetricData(void* metricData) = 0;
|
||||
|
||||
/**
|
||||
* On connection read event.
|
||||
* @param metricData - metric data of the given connection.- the one created in the `createMetricData` method.
|
||||
* @param readResult - result of the connection read method.
|
||||
* @param timestamp - timestamp microseconds when the connection `read` method was called.
|
||||
*/
|
||||
virtual void onRead(void* metricData, v_io_size readResult, v_int64 timestamp) = 0;
|
||||
|
||||
/**
|
||||
* On connection write event.
|
||||
* @param metricData - metric data of the given connection.- the one created in the `createMetricData` method.
|
||||
* @param writeResult - result of the connection write method.
|
||||
* @param timestamp - timestamp microseconds when the connection `write` method was called.
|
||||
*/
|
||||
virtual void onWrite(void* metricData, v_io_size writeResult, v_int64 timestamp) = 0;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user