refactor(hub): replace sync.map with app.store

This commit is contained in:
henrygd
2025-02-11 18:30:52 -05:00
parent 0c54f95546
commit 3376a97bea

View File

@@ -20,7 +20,6 @@ import (
"net/url" "net/url"
"os" "os"
"strings" "strings"
"sync"
"time" "time"
"github.com/goccy/go-json" "github.com/goccy/go-json"
@@ -33,7 +32,6 @@ import (
type Hub struct { type Hub struct {
app *pocketbase.PocketBase app *pocketbase.PocketBase
systemConnections sync.Map
sshClientConfig *ssh.ClientConfig sshClientConfig *ssh.ClientConfig
pubKey string pubKey string
am *alerts.AlertManager am *alerts.AlertManager
@@ -304,7 +302,7 @@ func (h *Hub) updateSystem(record *core.Record) {
var err error var err error
// check if system connection exists // check if system connection exists
if existingClient, ok := h.systemConnections.Load(record.Id); ok { if existingClient, ok := h.app.Store().GetOk(record.Id); ok {
client = existingClient.(*ssh.Client) client = existingClient.(*ssh.Client)
} else { } else {
// create system connection // create system connection
@@ -316,7 +314,7 @@ func (h *Hub) updateSystem(record *core.Record) {
} }
return return
} }
h.systemConnections.Store(record.Id, client) h.app.Store().Set(record.Id, client)
} }
// get system stats from agent // get system stats from agent
var systemData system.CombinedData var systemData system.CombinedData
@@ -400,11 +398,11 @@ func (h *Hub) updateSystemStatus(record *core.Record, status string) {
// delete system connection from map and close connection // delete system connection from map and close connection
func (h *Hub) deleteSystemConnection(record *core.Record) { func (h *Hub) deleteSystemConnection(record *core.Record) {
if client, ok := h.systemConnections.Load(record.Id); ok { if client, ok := h.app.Store().GetOk(record.Id); ok {
if sshClient := client.(*ssh.Client); sshClient != nil { if sshClient := client.(*ssh.Client); sshClient != nil {
sshClient.Close() sshClient.Close()
} }
h.systemConnections.Delete(record.Id) h.app.Store().Remove(record.Id)
} }
} }