mirror of
https://github.com/netbirdio/netbird.git
synced 2026-07-17 12:09:58 +00:00
Refactor UI --------- Co-authored-by: Eduard Gert <kontakt@eduardgert.de> Co-authored-by: braginini <bangvalo@gmail.com> Co-authored-by: Pascal Fischer <32096965+pascal-fischer@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: riccardom <riccardomanfrin@gmail.com>
58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
package server
|
|
|
|
import (
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/netbirdio/netbird/client/proto"
|
|
)
|
|
|
|
// SubscribeStatus pushes a fresh StatusResponse on every connection state
|
|
// change. The first message is the current snapshot, so a re-subscribing
|
|
// client doesn't need to also call Status. Subsequent messages fire when
|
|
// the peer recorder reports any of: connected/disconnected/connecting,
|
|
// management or signal flip, address change, or peers list change.
|
|
//
|
|
// The change channel coalesces bursts to a single tick. If the consumer
|
|
// is slow the daemon drops extras (not blocks), and the next snapshot
|
|
// the consumer pulls already reflects everything.
|
|
func (s *Server) SubscribeStatus(req *proto.StatusRequest, stream proto.DaemonService_SubscribeStatusServer) error {
|
|
subID, ch := s.statusRecorder.SubscribeToStateChanges()
|
|
defer func() {
|
|
s.statusRecorder.UnsubscribeFromStateChanges(subID)
|
|
log.Debug("client unsubscribed from status updates")
|
|
}()
|
|
|
|
log.Debug("client subscribed to status updates")
|
|
|
|
if err := s.sendStatusSnapshot(req, stream); err != nil {
|
|
return err
|
|
}
|
|
|
|
for {
|
|
select {
|
|
case _, ok := <-ch:
|
|
if !ok {
|
|
return nil
|
|
}
|
|
if err := s.sendStatusSnapshot(req, stream); err != nil {
|
|
return err
|
|
}
|
|
case <-stream.Context().Done():
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Server) sendStatusSnapshot(req *proto.StatusRequest, stream proto.DaemonService_SubscribeStatusServer) error {
|
|
resp, err := s.buildStatusResponse(stream.Context(), req)
|
|
if err != nil {
|
|
log.Warnf("build status snapshot for stream: %v", err)
|
|
return err
|
|
}
|
|
if err := stream.Send(resp); err != nil {
|
|
log.Warnf("send status snapshot to stream: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|