| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- // Author: simon (ynwdlxm@163.com)
- // Date: 2025/9/12 11:02
- // Desc: Encapsulation of each connection (description of each client connection)
- package connection
- import (
- "net"
- "sync"
- "time"
- "github.com/runningwater/go-redis/lib/logger"
- "github.com/runningwater/go-redis/lib/sync/wait"
- )
- // Connection represents a client connection to the Redis server
- type Connection struct {
- conn net.Conn
- waiting wait.Wait
- mu sync.Mutex
- selectedDB int
- }
- // NewConnection creates a new connection wrapper
- func NewConnection(conn net.Conn) *Connection {
- return &Connection{
- conn: conn,
- }
- }
- // RemoteAddr returns the remote network address
- func (c *Connection) RemoteAddr() net.Addr {
- return c.conn.RemoteAddr()
- }
- // Close closes the connection
- func (c *Connection) Close() error {
- timeout := c.waiting.WaitWithTimeout(10 * time.Second)
- if timeout {
- logger.Info("timeout to close connection")
- } else {
- logger.Info("connection closed")
- }
- err := c.conn.Close()
- return err
- }
- // Write writes data to the connection
- func (c *Connection) Write(bytes []byte) error {
- if len(bytes) == 0 {
- return nil
- }
- logger.InfoC("write to client: ", string(bytes))
- c.mu.Lock()
- c.waiting.Add(1)
- defer func() {
- c.waiting.Done()
- c.mu.Unlock()
- }()
- _, err := c.conn.Write(bytes)
- return err
- }
- // GetDBIndex returns the currently selected database index
- func (c *Connection) GetDBIndex() int {
- return c.selectedDB
- }
- // SelectDB selects a database
- func (c *Connection) SelectDB(dbNum int) {
- c.selectedDB = dbNum
- }
|