Public Access
feat: Error rate limiting
Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
2 files changed
+68
-1
No files matched your search
@@ -82,7 +82,9 @@ void NutUpsComponent::update() {
|
||||
update_sensors();
|
||||
} else {
|
||||
consecutive_failures_++;
|
||||
if (should_log_error(protocol_error_limiter_)) {
|
||||
ESP_LOGW(TAG, "Failed to read UPS data (failure #%u)", consecutive_failures_);
|
||||
}
|
||||
|
||||
// Implement limited retry logic with exponential backoff
|
||||
if (consecutive_failures_ >= 3 && consecutive_failures_ <= max_consecutive_failures_) {
|
||||
@@ -152,6 +154,41 @@ bool NutUpsComponent::initialize_usb() {
|
||||
#endif
|
||||
}
|
||||
|
||||
bool NutUpsComponent::should_log_error(ErrorRateLimit& limiter) {
|
||||
uint32_t now = millis();
|
||||
|
||||
// Check if we're past the rate limit window
|
||||
if (now - limiter.last_error_time > ErrorRateLimit::RATE_LIMIT_MS) {
|
||||
// Reset the limiter for new time window
|
||||
if (limiter.suppressed_count > 0) {
|
||||
// Log how many errors we suppressed in the previous window
|
||||
ESP_LOGW(TAG, "Suppressed %u similar error messages in the last %u ms",
|
||||
limiter.suppressed_count, ErrorRateLimit::RATE_LIMIT_MS);
|
||||
limiter.suppressed_count = 0;
|
||||
}
|
||||
limiter.error_count = 0;
|
||||
limiter.last_error_time = now;
|
||||
}
|
||||
|
||||
limiter.error_count++;
|
||||
|
||||
// Allow first few errors in burst, then rate limit
|
||||
if (limiter.error_count <= ErrorRateLimit::MAX_BURST) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Rate limit subsequent errors
|
||||
limiter.suppressed_count++;
|
||||
return false;
|
||||
}
|
||||
|
||||
void NutUpsComponent::log_suppressed_errors(ErrorRateLimit& limiter) {
|
||||
if (limiter.suppressed_count > 0) {
|
||||
ESP_LOGW(TAG, "Suppressed %u similar error messages", limiter.suppressed_count);
|
||||
limiter.suppressed_count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool NutUpsComponent::detect_ups_protocol() {
|
||||
ESP_LOGD(TAG, "Detecting UPS protocol...");
|
||||
|
||||
@@ -371,7 +408,9 @@ bool NutUpsComponent::usb_write(const std::vector<uint8_t> &data) {
|
||||
|
||||
// Take USB mutex for thread safety
|
||||
if (xSemaphoreTake(usb_mutex_, pdMS_TO_TICKS(1000)) != pdTRUE) {
|
||||
if (should_log_error(usb_error_limiter_)) {
|
||||
ESP_LOGW(TAG, "Failed to acquire USB mutex for write");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -387,7 +426,9 @@ bool NutUpsComponent::usb_write(const std::vector<uint8_t> &data) {
|
||||
xSemaphoreGive(usb_mutex_);
|
||||
|
||||
if (ret != ESP_OK) {
|
||||
if (should_log_error(usb_error_limiter_)) {
|
||||
ESP_LOGW(TAG, "USB write failed: %s", esp_err_to_name(ret));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -404,7 +445,9 @@ bool NutUpsComponent::usb_read(std::vector<uint8_t> &data, uint32_t timeout_ms)
|
||||
|
||||
// Take USB mutex for thread safety
|
||||
if (xSemaphoreTake(usb_mutex_, pdMS_TO_TICKS(timeout_ms)) != pdTRUE) {
|
||||
if (should_log_error(usb_error_limiter_)) {
|
||||
ESP_LOGW(TAG, "Failed to acquire USB mutex for read");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -420,7 +463,9 @@ bool NutUpsComponent::usb_read(std::vector<uint8_t> &data, uint32_t timeout_ms)
|
||||
xSemaphoreGive(usb_mutex_);
|
||||
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGV(TAG, "USB read failed: %s", esp_err_to_name(ret));
|
||||
if (should_log_error(usb_error_limiter_)) {
|
||||
ESP_LOGW(TAG, "USB read failed: %s", esp_err_to_name(ret));
|
||||
}
|
||||
data.clear();
|
||||
return false;
|
||||
}
|
||||
@@ -586,10 +631,14 @@ esp_err_t NutUpsComponent::usb_device_enumerate() {
|
||||
device_connected_ = true;
|
||||
return ESP_OK;
|
||||
}
|
||||
if (should_log_error(usb_error_limiter_)) {
|
||||
ESP_LOGW(TAG, "Failed to get USB endpoints: %s", esp_err_to_name(endpoint_ret));
|
||||
}
|
||||
} else {
|
||||
if (should_log_error(usb_error_limiter_)) {
|
||||
ESP_LOGW(TAG, "Failed to claim USB interface: %s", esp_err_to_name(claim_ret));
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up on failure
|
||||
usb_device_.dev_hdl = nullptr;
|
||||
|
||||
@@ -119,6 +119,9 @@ namespace esphome
|
||||
|
||||
~NutUpsComponent() {
|
||||
#ifdef USE_ESP32
|
||||
// Log any remaining suppressed errors before cleanup
|
||||
log_suppressed_errors(usb_error_limiter_);
|
||||
log_suppressed_errors(protocol_error_limiter_);
|
||||
usb_deinit();
|
||||
#endif
|
||||
}
|
||||
@@ -174,6 +177,17 @@ namespace esphome
|
||||
UpsData ups_data_;
|
||||
mutable std::mutex data_mutex_; // Protect ups_data_ access
|
||||
|
||||
// Error rate limiting to prevent log spam
|
||||
struct ErrorRateLimit {
|
||||
uint32_t last_error_time{0};
|
||||
uint32_t error_count{0};
|
||||
uint32_t suppressed_count{0};
|
||||
static constexpr uint32_t RATE_LIMIT_MS = 5000; // 5 seconds between repeated errors
|
||||
static constexpr uint32_t MAX_BURST = 3; // Allow 3 errors before rate limiting
|
||||
};
|
||||
ErrorRateLimit usb_error_limiter_;
|
||||
ErrorRateLimit protocol_error_limiter_;
|
||||
|
||||
std::unique_ptr<UpsProtocolBase> active_protocol_;
|
||||
std::unordered_map<std::string, sensor::Sensor *> sensors_;
|
||||
std::unordered_map<std::string, binary_sensor::BinarySensor *> binary_sensors_;
|
||||
@@ -186,6 +200,10 @@ namespace esphome
|
||||
void update_sensors();
|
||||
void simulate_ups_data();
|
||||
|
||||
// Error rate limiting helpers
|
||||
bool should_log_error(ErrorRateLimit& limiter);
|
||||
void log_suppressed_errors(ErrorRateLimit& limiter);
|
||||
|
||||
public:
|
||||
// USB communication methods (accessible by protocol classes)
|
||||
bool usb_write(const std::vector<uint8_t> &data);
|
||||
|
||||
Reference in new issue
Block a user