feat(pool): 添加获取所有在线连接ID的功能
- 实现了 GetAllConnIDs 方法来获取在线连接的ID列表 - 添加了内存缓存检查以提高性能 - 实现了从 BadgerDB 迭代获取所有连接记录的逻辑 - 添加了连接活跃状态过滤功能 - 实现了 JSON 反序列化连接信息的处理 - 添加了错误处理和日志记录机制main v1.0.1007
parent
845afe6dac
commit
942eff81fb
|
|
@ -215,6 +215,51 @@ func (p *BadgerPool) GetAll() ([]*ConnectionInfo, error) {
|
||||||
return conns, nil
|
return conns, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAllConnIDs 获取所有在线连接的ID列表
|
||||||
|
func (p *BadgerPool) GetAllConnIDs() ([]string, error) {
|
||||||
|
p.mutex.RLock()
|
||||||
|
// 如果内存缓存不为空,从缓存中提取在线连接的ID
|
||||||
|
if len(p.cache) > 0 {
|
||||||
|
ids := make([]string, 0, len(p.cache))
|
||||||
|
for id, conn := range p.cache {
|
||||||
|
if conn.IsActive {
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p.mutex.RUnlock()
|
||||||
|
return ids, nil
|
||||||
|
}
|
||||||
|
p.mutex.RUnlock()
|
||||||
|
|
||||||
|
// 从BadgerDB获取所有在线连接的ID
|
||||||
|
var ids []string
|
||||||
|
err := p.db.View(func(txn *badger.Txn) error {
|
||||||
|
opts := badger.DefaultIteratorOptions
|
||||||
|
opts.PrefetchSize = 10
|
||||||
|
it := txn.NewIterator(opts)
|
||||||
|
defer it.Close()
|
||||||
|
|
||||||
|
for it.Rewind(); it.Valid(); it.Next() {
|
||||||
|
item := it.Item()
|
||||||
|
var connInfo ConnectionInfo
|
||||||
|
err := item.Value(func(val []byte) error {
|
||||||
|
return json.Unmarshal(val, &connInfo)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if connInfo.IsActive {
|
||||||
|
ids = append(ids, string(item.Key()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get all connections: %w", err)
|
||||||
|
}
|
||||||
|
return ids, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetByType 根据类型获取连接
|
// GetByType 根据类型获取连接
|
||||||
func (p *BadgerPool) GetByType(connType ConnType) ([]*ConnectionInfo, error) {
|
func (p *BadgerPool) GetByType(connType ConnType) ([]*ConnectionInfo, error) {
|
||||||
allConns, err := p.GetAll()
|
allConns, err := p.GetAll()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue