From 1ee0baffeff8c24c26c72264ec7fe4cf7ccb7b0e Mon Sep 17 00:00:00 2001 From: bullshit Date: Thu, 7 Aug 2025 20:55:36 +0200 Subject: [PATCH] feat: Error rate limiting Signed-off-by: bullshit --- components/nut_ups/nut_ups.cpp | 63 ++++++++++++++++++++++++++++++---- components/nut_ups/nut_ups.h | 18 ++++++++++ 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/components/nut_ups/nut_ups.cpp b/components/nut_ups/nut_ups.cpp index fe18ec9..11d905c 100644 --- a/components/nut_ups/nut_ups.cpp +++ b/components/nut_ups/nut_ups.cpp @@ -82,7 +82,9 @@ void NutUpsComponent::update() { update_sensors(); } else { consecutive_failures_++; - ESP_LOGW(TAG, "Failed to read UPS data (failure #%u)", 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 &data) { // Take USB mutex for thread safety if (xSemaphoreTake(usb_mutex_, pdMS_TO_TICKS(1000)) != pdTRUE) { - ESP_LOGW(TAG, "Failed to acquire USB mutex for write"); + 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 &data) { xSemaphoreGive(usb_mutex_); if (ret != ESP_OK) { - ESP_LOGW(TAG, "USB write failed: %s", esp_err_to_name(ret)); + 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 &data, uint32_t timeout_ms) // Take USB mutex for thread safety if (xSemaphoreTake(usb_mutex_, pdMS_TO_TICKS(timeout_ms)) != pdTRUE) { - ESP_LOGW(TAG, "Failed to acquire USB mutex for read"); + 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 &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,9 +631,13 @@ esp_err_t NutUpsComponent::usb_device_enumerate() { device_connected_ = true; return ESP_OK; } - ESP_LOGW(TAG, "Failed to get USB endpoints: %s", esp_err_to_name(endpoint_ret)); + if (should_log_error(usb_error_limiter_)) { + ESP_LOGW(TAG, "Failed to get USB endpoints: %s", esp_err_to_name(endpoint_ret)); + } } else { - ESP_LOGW(TAG, "Failed to claim USB interface: %s", esp_err_to_name(claim_ret)); + 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 diff --git a/components/nut_ups/nut_ups.h b/components/nut_ups/nut_ups.h index 9e3e529..eeadd99 100644 --- a/components/nut_ups/nut_ups.h +++ b/components/nut_ups/nut_ups.h @@ -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 } @@ -173,6 +176,17 @@ namespace esphome uint32_t max_consecutive_failures_{5}; // Limit re-detection attempts 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 active_protocol_; std::unordered_map sensors_; @@ -185,6 +199,10 @@ namespace esphome bool read_ups_data(); 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)