netbird icon indicating copy to clipboard operation
netbird copied to clipboard

Optimize Client Engine - event handling

Open braginini opened this issue 3 years ago • 0 comments

What? Add event handling to the Engine in the Client app (actor-like?) There are certain events (logical) on the client-side that occur from time to time on the client. E.g.:

  • Connected to Signal service
  • Disconnected From Signal Service
  • Connected to Management Service
  • Disconnected From Management Service
  • Peer Connected
  • Peer Disconnected
  • Etc

Why? Currently, things in engine.go are based on periodic checks/loops when dealing with peer connections which is not really efficient. Another reason is that we will be splitting the client into daemon (responsible for the connectivity/core logic) and cmd parts and it would make sense to have some sort of event handling. Later, we will also support OS events.

A simple example where event-based handling would be beneficial and save some CPU. Consider the following case:

  • all peers are connected
  • network drops
  • all peers disconnect
  • management and signal clients disconnect
  • currently, there is a for loop for each peer connection that periodically checks whether a peer connection can be established. Basically checks whether the signal client is ready to send a connection offer.

Instead, in the event-based system, we could have just stopped all the peer connections and waited for an event that signal has been connected. The signal client would emit an event that is available triggering peer connections.

How? Here is the idea. Use an event channel in the engine and a single goroutine reading from it and process the events. The routine logic should be very lightweight and non-blocking. The engine should keep the state accessible only by this goroutine, meaning that we are synchronizing on that event channel and processing events in order. E.g. all the events described above should be written to this channel.

Later we could also listen to the system events e.g. network up/down

braginini avatar Jan 12 '22 18:01 braginini