mirror of
https://github.com/henrygd/beszel.git
synced 2026-04-16 01:41:49 +02:00
hub: fix possible nil pointer panic in realtime worker
This commit is contained in:
@@ -19,7 +19,6 @@ type subscriptionInfo struct {
|
|||||||
var (
|
var (
|
||||||
activeSubscriptions = make(map[string]*subscriptionInfo)
|
activeSubscriptions = make(map[string]*subscriptionInfo)
|
||||||
workerRunning bool
|
workerRunning bool
|
||||||
realtimeTicker *time.Ticker
|
|
||||||
tickerStopChan chan struct{}
|
tickerStopChan chan struct{}
|
||||||
realtimeMutex sync.Mutex
|
realtimeMutex sync.Mutex
|
||||||
)
|
)
|
||||||
@@ -70,7 +69,7 @@ func (sm *SystemManager) onRealtimeSubscribeRequest(e *core.RealtimeSubscribeReq
|
|||||||
}
|
}
|
||||||
|
|
||||||
// onRealtimeSubscriptionAdded initializes or starts the realtime worker when the first subscription is added.
|
// onRealtimeSubscriptionAdded initializes or starts the realtime worker when the first subscription is added.
|
||||||
// It ensures only one worker runs at a time and creates the ticker for periodic data fetching.
|
// It ensures only one worker runs at a time.
|
||||||
func (sm *SystemManager) onRealtimeSubscriptionAdded() {
|
func (sm *SystemManager) onRealtimeSubscriptionAdded() {
|
||||||
realtimeMutex.Lock()
|
realtimeMutex.Lock()
|
||||||
defer realtimeMutex.Unlock()
|
defer realtimeMutex.Unlock()
|
||||||
@@ -82,11 +81,6 @@ func (sm *SystemManager) onRealtimeSubscriptionAdded() {
|
|||||||
tickerStopChan = make(chan struct{})
|
tickerStopChan = make(chan struct{})
|
||||||
go sm.startRealtimeWorker()
|
go sm.startRealtimeWorker()
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no ticker exists, create one
|
|
||||||
if realtimeTicker == nil {
|
|
||||||
realtimeTicker = time.NewTicker(1 * time.Second)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkSubscriptions stops the realtime worker when there are no active subscriptions.
|
// checkSubscriptions stops the realtime worker when there are no active subscriptions.
|
||||||
@@ -107,11 +101,6 @@ func (sm *SystemManager) checkSubscriptions() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if realtimeTicker != nil {
|
|
||||||
realtimeTicker.Stop()
|
|
||||||
realtimeTicker = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark worker as stopped (will be reset when next subscription comes in)
|
// Mark worker as stopped (will be reset when next subscription comes in)
|
||||||
workerRunning = false
|
workerRunning = false
|
||||||
}
|
}
|
||||||
@@ -135,17 +124,16 @@ func (sm *SystemManager) removeRealtimeSubscription(subscription string, options
|
|||||||
// It continuously fetches system data and broadcasts it to subscribed clients via WebSocket.
|
// It continuously fetches system data and broadcasts it to subscribed clients via WebSocket.
|
||||||
func (sm *SystemManager) startRealtimeWorker() {
|
func (sm *SystemManager) startRealtimeWorker() {
|
||||||
sm.fetchRealtimeDataAndNotify()
|
sm.fetchRealtimeDataAndNotify()
|
||||||
|
tick := time.Tick(1 * time.Second)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-tickerStopChan:
|
case <-tickerStopChan:
|
||||||
return
|
return
|
||||||
case <-realtimeTicker.C:
|
case <-tick:
|
||||||
// Check if ticker is still valid (might have been stopped)
|
if len(activeSubscriptions) == 0 {
|
||||||
if realtimeTicker == nil || len(activeSubscriptions) == 0 {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// slog.Debug("activeSubscriptions", "count", len(activeSubscriptions))
|
|
||||||
sm.fetchRealtimeDataAndNotify()
|
sm.fetchRealtimeDataAndNotify()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user