[api] Use loop-based reboot timeout check to avoid scheduler heap churn (#12291)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -52,11 +52,6 @@ void APIServer::setup() {
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Schedule reboot if no clients connect within timeout
|
|
||||||
if (this->reboot_timeout_ != 0) {
|
|
||||||
this->schedule_reboot_timeout_();
|
|
||||||
}
|
|
||||||
|
|
||||||
this->socket_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0); // monitored for incoming connections
|
this->socket_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0); // monitored for incoming connections
|
||||||
if (this->socket_ == nullptr) {
|
if (this->socket_ == nullptr) {
|
||||||
ESP_LOGW(TAG, "Could not create socket");
|
ESP_LOGW(TAG, "Could not create socket");
|
||||||
@@ -110,16 +105,13 @@ void APIServer::setup() {
|
|||||||
camera::Camera::instance()->add_listener(this);
|
camera::Camera::instance()->add_listener(this);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
void APIServer::schedule_reboot_timeout_() {
|
// Initialize last_connected_ for reboot timeout tracking
|
||||||
this->status_set_warning();
|
this->last_connected_ = App.get_loop_component_start_time();
|
||||||
this->set_timeout("api_reboot", this->reboot_timeout_, []() {
|
// Set warning status if reboot timeout is enabled
|
||||||
if (!global_api_server->is_connected()) {
|
if (this->reboot_timeout_ != 0) {
|
||||||
ESP_LOGE(TAG, "No clients; rebooting");
|
this->status_set_warning();
|
||||||
App.reboot();
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void APIServer::loop() {
|
void APIServer::loop() {
|
||||||
@@ -147,15 +139,24 @@ void APIServer::loop() {
|
|||||||
this->clients_.emplace_back(conn);
|
this->clients_.emplace_back(conn);
|
||||||
conn->start();
|
conn->start();
|
||||||
|
|
||||||
// Clear warning status and cancel reboot when first client connects
|
// First client connected - clear warning and update timestamp
|
||||||
if (this->clients_.size() == 1 && this->reboot_timeout_ != 0) {
|
if (this->clients_.size() == 1 && this->reboot_timeout_ != 0) {
|
||||||
this->status_clear_warning();
|
this->status_clear_warning();
|
||||||
this->cancel_timeout("api_reboot");
|
this->last_connected_ = App.get_loop_component_start_time();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->clients_.empty()) {
|
if (this->clients_.empty()) {
|
||||||
|
// Check reboot timeout - done in loop to avoid scheduler heap churn
|
||||||
|
// (cancelled scheduler items sit in heap memory until their scheduled time)
|
||||||
|
if (this->reboot_timeout_ != 0) {
|
||||||
|
const uint32_t now = App.get_loop_component_start_time();
|
||||||
|
if (now - this->last_connected_ > this->reboot_timeout_) {
|
||||||
|
ESP_LOGE(TAG, "No clients; rebooting");
|
||||||
|
App.reboot();
|
||||||
|
}
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,9 +195,10 @@ void APIServer::loop() {
|
|||||||
}
|
}
|
||||||
this->clients_.pop_back();
|
this->clients_.pop_back();
|
||||||
|
|
||||||
// Schedule reboot when last client disconnects
|
// Last client disconnected - set warning and start tracking for reboot timeout
|
||||||
if (this->clients_.empty() && this->reboot_timeout_ != 0) {
|
if (this->clients_.empty() && this->reboot_timeout_ != 0) {
|
||||||
this->schedule_reboot_timeout_();
|
this->status_set_warning();
|
||||||
|
this->last_connected_ = App.get_loop_component_start_time();
|
||||||
}
|
}
|
||||||
// Don't increment client_index since we need to process the swapped element
|
// Don't increment client_index since we need to process the swapped element
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -202,7 +202,6 @@ class APIServer : public Component,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void schedule_reboot_timeout_();
|
|
||||||
#ifdef USE_API_NOISE
|
#ifdef USE_API_NOISE
|
||||||
bool update_noise_psk_(const SavedNoisePsk &new_psk, const LogString *save_log_msg, const LogString *fail_log_msg,
|
bool update_noise_psk_(const SavedNoisePsk &new_psk, const LogString *save_log_msg, const LogString *fail_log_msg,
|
||||||
const psk_t &active_psk, bool make_active);
|
const psk_t &active_psk, bool make_active);
|
||||||
@@ -218,6 +217,7 @@ class APIServer : public Component,
|
|||||||
|
|
||||||
// 4-byte aligned types
|
// 4-byte aligned types
|
||||||
uint32_t reboot_timeout_{300000};
|
uint32_t reboot_timeout_{300000};
|
||||||
|
uint32_t last_connected_{0};
|
||||||
|
|
||||||
// Vectors and strings (12 bytes each on 32-bit)
|
// Vectors and strings (12 bytes each on 32-bit)
|
||||||
std::vector<std::unique_ptr<APIConnection>> clients_;
|
std::vector<std::unique_ptr<APIConnection>> clients_;
|
||||||
|
|||||||
Reference in New Issue
Block a user