Compare commits

...
2 Commits
Author SHA1 Message Date
Akizon 1572b92751 Merge pull request #65 from komari-monitor/dev
fix: #57 api.Records引入线程安全的泛型映射
2025-06-23 23:39:40 +08:00
Akizon77 8da28d6b95 fix: #57 api.Records引入线程安全的泛型映射 2025-06-23 23:09:21 +08:00
4 changed files with 106 additions and 13 deletions

No files matched your search

+11 -9
View File
@@ -15,7 +15,7 @@ import (
)
var (
Records map[string][]common.Report = make(map[string][]common.Report)
Records = utils.NewSafeMap[string, []common.Report]()
)
type TerminalSession struct {
@@ -31,8 +31,10 @@ var TerminalSessions = make(map[string]*TerminalSession)
func SaveClientReportToDB() error {
lastMinute := time.Now().Add(-time.Minute * 1).Unix()
record := []models.Record{}
var err error
// 遍历所有的客户端记录
for uuid, reports := range Records {
Records.Range(func(uuid string, reports []common.Report) bool {
// 删除一分钟前的记录
filtered := reports[:0]
for _, r := range reports {
@@ -40,24 +42,24 @@ func SaveClientReportToDB() error {
filtered = append(filtered, r)
}
}
Records[uuid] = filtered
Records.Store(uuid, filtered)
if uuid == "" {
continue
return true
}
r := utils.AverageReport(uuid, time.Now(), filtered)
record = append(record, r)
db := dbcore.GetDBInstance()
err := db.Model(&models.Record{}).Create(record).Error
err = db.Model(&models.Record{}).Create(record).Error
if err != nil {
return err
return false
}
}
return true
})
return nil
return err
}
type Response struct {
+5 -3
View File
@@ -178,13 +178,15 @@ func WebSocketReport(c *gin.Context) {
}
func SaveClientReport(uuid string, report common.Report) error {
if api.Records[uuid] == nil {
api.Records[uuid] = []common.Report{}
reports, _ := api.Records.Get(uuid)
if reports == nil {
reports = []common.Report{}
}
if report.CPU.Usage < 0.01 {
report.CPU.Usage = 0.01
}
api.Records[uuid] = append(api.Records[uuid], report)
reports = append(reports, report)
api.Records.Set(uuid, reports)
return nil
}
+2 -1
View File
@@ -10,5 +10,6 @@ func GetClientRecentRecords(c *gin.Context) {
RespondError(c, 400, "UUID is required")
return
}
RespondSuccess(c, Records[uuid])
records, _ := Records.Get(uuid)
RespondSuccess(c, records)
}
+88
View File
@@ -0,0 +1,88 @@
package utils
import (
"sync"
)
// SafeMap 是一个线程安全的泛型映射类型
// 使用 sync.RWMutex 来确保并发访问的安全性
type SafeMap[K comparable, V any] struct {
mu sync.RWMutex // 读写锁,用于保护 items 的并发访问
items map[K]V // 存储键值对的底层映射
}
// NewSafeMap 创建一个新的 SafeMap 实例
// 返回一个指向 SafeMap 的指针
func NewSafeMap[K comparable, V any]() *SafeMap[K, V] {
return &SafeMap[K, V]{
items: make(map[K]V), // 初始化底层映射
}
}
// Set 添加或更新映射中的键值对
// 使用写锁保护操作
func (sm *SafeMap[K, V]) Set(key K, value V) {
sm.mu.Lock()
defer sm.mu.Unlock()
sm.items[key] = value
}
// Store 添加或更新映射中的键值对
// 是 Set 方法的别名
func (sm *SafeMap[K, V]) Store(key K, value V) {
sm.Set(key, value)
}
// Get 根据键从映射中检索值
// 返回值和一个布尔值,指示键是否存在
func (sm *SafeMap[K, V]) Get(key K) (V, bool) {
sm.mu.RLock()
defer sm.mu.RUnlock()
value, exists := sm.items[key]
return value, exists
}
// Delete 从映射中移除指定键的键值对
// 使用写锁保护操作
func (sm *SafeMap[K, V]) Delete(key K) {
sm.mu.Lock()
defer sm.mu.Unlock()
delete(sm.items, key)
}
// Keys 返回映射中所有键的切片
// 使用读锁保护操作
func (sm *SafeMap[K, V]) Keys() []K {
sm.mu.RLock()
defer sm.mu.RUnlock()
keys := make([]K, 0, len(sm.items))
for key := range sm.items {
keys = append(keys, key)
}
return keys
}
// Len 返回映射中键值对的数量
// 使用读锁保护操作
func (sm *SafeMap[K, V]) Len() int {
sm.mu.RLock()
defer sm.mu.RUnlock()
return len(sm.items)
}
// Range 遍历映射中的所有键值对
// 接受一个函数作为参数,如果函数返回 false,则停止遍历
func (sm *SafeMap[K, V]) Range(f func(key K, value V) bool) {
sm.mu.RLock()
defer sm.mu.RUnlock()
for key, value := range sm.items {
if !f(key, value) {
break
}
}
}