mirror of
https://github.com/esphome/esphome.git
synced 2026-05-28 21:59:59 +08:00
[mdns] Throttle MDNS.update() polling on ESP8266 and RP2040 (#13917)
This commit is contained in:
@@ -45,9 +45,28 @@ class MDNSComponent : public Component {
|
|||||||
void setup() override;
|
void setup() override;
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
|
|
||||||
#if (defined(USE_ESP8266) || defined(USE_RP2040)) && defined(USE_ARDUINO)
|
// Polling interval for MDNS.update() on platforms that require it (ESP8266, RP2040).
|
||||||
void loop() override;
|
//
|
||||||
#endif
|
// On these platforms, MDNS.update() calls _process(true) which only manages timer-driven
|
||||||
|
// state machines (probe/announce timeouts and service query cache TTLs). Incoming mDNS
|
||||||
|
// packets are handled independently via the lwIP onRx UDP callback and are NOT affected
|
||||||
|
// by how often update() is called.
|
||||||
|
//
|
||||||
|
// The shortest internal timer is the 250ms probe interval (RFC 6762 Section 8.1).
|
||||||
|
// Announcement intervals are 1000ms and cache TTL checks are on the order of seconds
|
||||||
|
// to minutes. A 50ms polling interval provides sufficient resolution for all timers
|
||||||
|
// while completely removing mDNS from the per-iteration loop list.
|
||||||
|
//
|
||||||
|
// In steady state (after the ~8 second boot probe/announce phase completes), update()
|
||||||
|
// checks timers that are set to never expire, making every call pure overhead.
|
||||||
|
//
|
||||||
|
// Tasmota uses a 50ms main loop cycle with mDNS working correctly, confirming this
|
||||||
|
// interval is safe in production.
|
||||||
|
//
|
||||||
|
// By using set_interval() instead of overriding loop(), the component is excluded from
|
||||||
|
// the main loop list via has_overridden_loop(), eliminating all per-iteration overhead
|
||||||
|
// including virtual dispatch.
|
||||||
|
static constexpr uint32_t MDNS_UPDATE_INTERVAL_MS = 50;
|
||||||
float get_setup_priority() const override { return setup_priority::AFTER_CONNECTION; }
|
float get_setup_priority() const override { return setup_priority::AFTER_CONNECTION; }
|
||||||
|
|
||||||
#ifdef USE_MDNS_EXTRA_SERVICES
|
#ifdef USE_MDNS_EXTRA_SERVICES
|
||||||
|
|||||||
@@ -36,9 +36,14 @@ static void register_esp8266(MDNSComponent *, StaticVector<MDNSService, MDNS_SER
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_esp8266); }
|
void MDNSComponent::setup() {
|
||||||
|
this->setup_buffers_and_register_(register_esp8266);
|
||||||
void MDNSComponent::loop() { MDNS.update(); }
|
// Schedule MDNS.update() via set_interval() instead of overriding loop().
|
||||||
|
// This removes the component from the per-iteration loop list entirely,
|
||||||
|
// eliminating virtual dispatch overhead on every main loop cycle.
|
||||||
|
// See MDNS_UPDATE_INTERVAL_MS comment in mdns_component.h for safety analysis.
|
||||||
|
this->set_interval(MDNS_UPDATE_INTERVAL_MS, []() { MDNS.update(); });
|
||||||
|
}
|
||||||
|
|
||||||
void MDNSComponent::on_shutdown() {
|
void MDNSComponent::on_shutdown() {
|
||||||
MDNS.close();
|
MDNS.close();
|
||||||
|
|||||||
@@ -35,9 +35,14 @@ static void register_rp2040(MDNSComponent *, StaticVector<MDNSService, MDNS_SERV
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_rp2040); }
|
void MDNSComponent::setup() {
|
||||||
|
this->setup_buffers_and_register_(register_rp2040);
|
||||||
void MDNSComponent::loop() { MDNS.update(); }
|
// Schedule MDNS.update() via set_interval() instead of overriding loop().
|
||||||
|
// This removes the component from the per-iteration loop list entirely,
|
||||||
|
// eliminating virtual dispatch overhead on every main loop cycle.
|
||||||
|
// See MDNS_UPDATE_INTERVAL_MS comment in mdns_component.h for safety analysis.
|
||||||
|
this->set_interval(MDNS_UPDATE_INTERVAL_MS, []() { MDNS.update(); });
|
||||||
|
}
|
||||||
|
|
||||||
void MDNSComponent::on_shutdown() {
|
void MDNSComponent::on_shutdown() {
|
||||||
MDNS.close();
|
MDNS.close();
|
||||||
|
|||||||
Reference in New Issue
Block a user