feat(database): 将数据库连接池从 NutsDB 迁移到 SQLite
- 修改 server/ws/example.go 中的数据库连接池引用,将 m.nutsPool 替换为 m.sqlitePool - 更新 go.mod 文件中的 Go 版本从 1.24.3 升级到 1.25.0 - 移除 nutsdb 依赖项 github.com/nutsdb/nutsdb v1.1.0 - 移除其他间接依赖包括 github.com/antlabs/stl、github.com/antlabs/timer、 github.com/bwmarrin/snowflake、github.com/cespare/xxhash/v2 等 - 添加 SQLite 相关依赖 github.com/glebarez/go-sqlite 和 github.com/glebarez/sqlite - 更新 golang.org/x 相关包版本,包括 exp、sync、sys、text 等模块 - 在 go.sum 中同步更新所有依赖项的校验和变化
This commit is contained in:
+26
-26
@@ -33,7 +33,7 @@ type TCPServer struct {
|
||||
// ConnectionPool 连接池结构
|
||||
type ConnectionPool struct {
|
||||
connections map[string]*TcpConnection
|
||||
nutsPool *pool.NutsPool
|
||||
sqlitePool *pool.SQLitePool
|
||||
mutex sync.RWMutex
|
||||
config *TcpPoolConfig
|
||||
logger *glog.Logger
|
||||
@@ -44,15 +44,15 @@ func NewTCPServer(address string, config *TcpPoolConfig) (*TCPServer, error) {
|
||||
logger := g.Log(address)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
// 初始化NutsDB连接池
|
||||
nutsPool, err := pool.NewNutsPool()
|
||||
// 初始化SQLite连接池
|
||||
sqlitePool, err := pool.NewSQLitePool()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create nuts pool: %w", err)
|
||||
return nil, fmt.Errorf("failed to create sqlite pool: %w", err)
|
||||
}
|
||||
|
||||
pool := &ConnectionPool{
|
||||
connections: make(map[string]*TcpConnection),
|
||||
nutsPool: nutsPool,
|
||||
sqlitePool: sqlitePool,
|
||||
config: config,
|
||||
logger: logger,
|
||||
}
|
||||
@@ -95,9 +95,9 @@ func (s *TCPServer) Stop() error {
|
||||
s.Listener.Close()
|
||||
s.wg.Wait()
|
||||
s.Connection.Clear()
|
||||
// 关闭NutsDB连接池
|
||||
if err := s.Connection.nutsPool.Close(); err != nil {
|
||||
s.Logger.Error(s.ctx, fmt.Sprintf("Failed to close NutsDB pool: %v", err))
|
||||
// 关闭SQLite连接池
|
||||
if err := s.Connection.sqlitePool.Close(); err != nil {
|
||||
s.Logger.Error(s.ctx, fmt.Sprintf("Failed to close SQLite pool: %v", err))
|
||||
// 不影响服务器停止,仅记录错误
|
||||
}
|
||||
s.Logger.Info(s.ctx, "TCP server stopped")
|
||||
@@ -123,7 +123,7 @@ func (s *TCPServer) handleConnection(conn *gtcp.Conn) {
|
||||
s.Connection.Add(tcpConn)
|
||||
s.Logger.Info(s.ctx, fmt.Sprintf("New connection established: %s", connID))
|
||||
|
||||
// 存储到NutsDB
|
||||
// 存储到SQLite
|
||||
connInfo := &pool.ConnectionInfo{
|
||||
ID: connID,
|
||||
Type: pool.ConnTypeTCP,
|
||||
@@ -135,8 +135,8 @@ func (s *TCPServer) handleConnection(conn *gtcp.Conn) {
|
||||
"localAddress": conn.LocalAddr().String(),
|
||||
},
|
||||
}
|
||||
if err := s.Connection.nutsPool.Add(connInfo); err != nil {
|
||||
s.Logger.Error(s.ctx, fmt.Sprintf("Failed to store connection to NutsDB: %v", err))
|
||||
if err := s.Connection.sqlitePool.Add(connInfo); err != nil {
|
||||
s.Logger.Error(s.ctx, fmt.Sprintf("Failed to store connection to SQLite: %v", err))
|
||||
// 不影响连接建立,仅记录错误
|
||||
}
|
||||
|
||||
@@ -152,9 +152,9 @@ func (s *TCPServer) receiveMessages(conn *TcpConnection) {
|
||||
}
|
||||
s.Connection.Remove(conn.Id)
|
||||
conn.Server.Close()
|
||||
// 从NutsDB移除
|
||||
if err := s.Connection.nutsPool.Remove(conn.Id); err != nil {
|
||||
s.Logger.Error(s.ctx, fmt.Sprintf("Failed to remove connection from NutsDB: %v", err))
|
||||
// 从SQLite移除
|
||||
if err := s.Connection.sqlitePool.Remove(conn.Id); err != nil {
|
||||
s.Logger.Error(s.ctx, fmt.Sprintf("Failed to remove connection from SQLite: %v", err))
|
||||
// 不影响连接关闭,仅记录错误
|
||||
}
|
||||
s.Logger.Info(s.ctx, fmt.Sprintf("Connection closed: %s", conn.Id))
|
||||
@@ -183,12 +183,12 @@ func (s *TCPServer) receiveMessages(conn *TcpConnection) {
|
||||
conn.LastUsed = now
|
||||
conn.Mutex.Unlock()
|
||||
|
||||
// 更新NutsDB中的连接信息
|
||||
connInfo, err := s.Connection.nutsPool.Get(conn.Id)
|
||||
// 更新SQLite中的连接信息
|
||||
connInfo, err := s.Connection.sqlitePool.Get(conn.Id)
|
||||
if err == nil && connInfo != nil {
|
||||
connInfo.LastUsed = now
|
||||
if err := s.Connection.nutsPool.Update(connInfo); err != nil {
|
||||
s.Logger.Error(s.ctx, fmt.Sprintf("Failed to update connection in NutsDB: %v", err))
|
||||
if err := s.Connection.sqlitePool.Update(connInfo); err != nil {
|
||||
s.Logger.Error(s.ctx, fmt.Sprintf("Failed to update connection in SQLite: %v", err))
|
||||
// 不影响消息处理,仅记录错误
|
||||
}
|
||||
}
|
||||
@@ -259,12 +259,12 @@ func (s *TCPServer) sendMessage(conn *TcpConnection, data []byte) error {
|
||||
now := time.Now()
|
||||
conn.LastUsed = now
|
||||
|
||||
// 更新NutsDB中的连接信息
|
||||
connInfo, err := s.Connection.nutsPool.Get(conn.Id)
|
||||
// 更新SQLite中的连接信息
|
||||
connInfo, err := s.Connection.sqlitePool.Get(conn.Id)
|
||||
if err == nil && connInfo != nil {
|
||||
connInfo.LastUsed = now
|
||||
if err := s.Connection.nutsPool.Update(connInfo); err != nil {
|
||||
s.Logger.Error(s.ctx, fmt.Sprintf("Failed to update connection in NutsDB: %v", err))
|
||||
if err := s.Connection.sqlitePool.Update(connInfo); err != nil {
|
||||
s.Logger.Error(s.ctx, fmt.Sprintf("Failed to update connection in SQLite: %v", err))
|
||||
// 不影响消息发送,仅记录错误
|
||||
}
|
||||
}
|
||||
@@ -283,9 +283,9 @@ func (s *TCPServer) Kick(connID string) error {
|
||||
conn.Server.Close()
|
||||
// 从连接池移除
|
||||
s.Connection.Remove(connID)
|
||||
// 从NutsDB移除
|
||||
if err := s.Connection.nutsPool.Remove(connID); err != nil {
|
||||
s.Logger.Error(s.ctx, fmt.Sprintf("Failed to remove connection from NutsDB: %v", err))
|
||||
// 从SQLite移除
|
||||
if err := s.Connection.sqlitePool.Remove(connID); err != nil {
|
||||
s.Logger.Error(s.ctx, fmt.Sprintf("Failed to remove connection from SQLite: %v", err))
|
||||
// 不影响连接关闭,仅记录错误
|
||||
}
|
||||
|
||||
@@ -350,5 +350,5 @@ func (p *ConnectionPool) Count() int {
|
||||
|
||||
// GetAllConnIDs 获取所有在线连接的ID列表
|
||||
func (p *ConnectionPool) GetAllConnIDs() ([]string, error) {
|
||||
return p.nutsPool.GetAllConnIDs()
|
||||
return p.sqlitePool.GetAllConnIDs()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user