arduino / mdns-discovery

mdns (bonjour) pluggable discovery
GNU General Public License v3.0
17 stars 6 forks source link

Refactors `mdns-discovery` to better handle `add` and `remove` events #14

Closed silvanocerza closed 2 years ago

silvanocerza commented 2 years ago

Right now mdns-discovery has some issues caused by the mDNS library in use and by technical limitations of the mDNS protocol.

Because of the way the mDNS protocol works we have no way of knowing if a device has been disconnected from the network, only when it's connected/discovered.

The best way to handle this is to use TTLs and send a remove event when the TTL is reached. But the library we're using github.com/brutella/dnssd doesn't handle TTLs correctly so we never receives disconnections events from it. Also we don't have much granular control of the discovery process with this library.

I changed the library used to github.com/hashicorp/mdns and the internal behaviour of the discovery process.

Every 15 seconds we query the network to know if there are boards connected, each discovered port is cached with a TTL of 60 seconds and an add event is sent to consumers. Each time we find a board we first check if it's cached, if it is we renew the TTL to 60 seconds. No add event is sent in this case. When the TTL ends the board is removed from cache and a remove event is sent to consumers.

facchinm commented 2 years ago

When touching this library, make sure not to reintroduce some bugs long fixed in the java IDE (like to ones linked here https://github.com/arduino/Arduino/pull/5879 ). The main actions taken to bring down cpu usage were:

silvanocerza commented 2 years ago

@facchinm no worries, the CPU usage is minimal, it never went more than 0.8% for me on Linux.

facchinm commented 2 years ago

I'd suggest to test on a Windows pc with 5 network interfaces but it's not so easy to find :sweat_smile: