Compare commits

..

8 Commits

Author SHA1 Message Date
ddf1439876 feat: use qoobing log 2025-06-15 09:08:40 +08:00
eef1014397 [DEV] add Int/Int64/... 2025-05-21 23:49:44 +08:00
9b4cfc9706 dev 2025-04-08 10:44:28 +08:00
34d1b513ba add GetSecDsn to get config description 2025-04-08 08:44:20 +08:00
bf3cd9bc27 fix typo 2024-11-13 22:59:38 +08:00
5a26f88da7 Merge branch 'master' 2024-11-13 12:29:14 +08:00
7c4e176775 add error log 2024-11-12 00:42:57 +08:00
bryanqiu
576807b94e dev 2024-06-28 18:25:26 +08:00
5 changed files with 99 additions and 57 deletions

15
go.sum Normal file
View File

@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/gomodule/redigo v1.9.2 h1:HrutZBLhSIU8abiSfW8pj8mPhOyMYjZT/wcA4/L9L9s=
github.com/gomodule/redigo v1.9.2/go.mod h1:KsU3hiK/Ay8U42qpaJk+kuNa3C+spxapWpM+ywhcgtw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/tylerb/gls v0.0.0-20150407001822-e606233f194d h1:yYYPFFlbqxF5mrj5sEfETtM/Ssz2LTy0/VKlDdXYctc=
github.com/tylerb/gls v0.0.0-20150407001822-e606233f194d/go.mod h1:0MwyId/pXK5wkYYEXe7NnVknX+aNBuF73fLV3U0reU8=
github.com/tylerb/is v2.1.4+incompatible h1:BMf2zP0kY2Ykzx2W1fDrjwKj1x1B4E0mELkpjaNy1tM=
github.com/tylerb/is v2.1.4+incompatible/go.mod h1:3Bw2NWEEe8Kx7/etYqgm9ug53iNDgabnloch75jjOSc=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
qoobing.com/gomod/log v1.2.8 h1:J1L87VhDMHvCgPZH2H/6QuwTLiMorJL97xVx7FJyi2g=
qoobing.com/gomod/log v1.2.8/go.mod h1:/ZTN/ukAbSqRb4eMlF9LpfkVgM21xwprbd5y3tcQxpM=
qoobing.com/gomod/str v1.0.1/go.mod h1:gbhN2dba/P5gFRGVJvEI57KEJLlMHHAd6Kuuxn4GlMY=
qoobing.com/gomod/str v1.0.5 h1:AXEB8k/yhepLK5jVez+WL4sWVuCFb8pWAgmo3nvt96A=
qoobing.com/gomod/str v1.0.5/go.mod h1:gbhN2dba/P5gFRGVJvEI57KEJLlMHHAd6Kuuxn4GlMY=

View File

@ -18,45 +18,40 @@ import (
"bytes"
"context"
"fmt"
"log"
"time"
"github.com/gomodule/redigo/redis"
)
var (
_ redis.ConnWithTimeout = (*loggingConn)(nil)
logCallDepth = 3
_ redis.ConnWithTimeout = (*loggingConn)(nil)
)
// NewLoggingConn returns a logging wrapper around a connection.
func NewLoggingConn(conn redis.Conn, logger *log.Logger, prefix string) redis.Conn {
if prefix != "" {
prefix = prefix + "."
}
return &loggingConn{conn, logger, prefix, nil}
}
// NewLoggingConnFilter returns a logging wrapper around a connection and a filter function.
func NewLoggingConnFilter(conn redis.Conn, logger *log.Logger, prefix string, skip func(cmdName string) bool) redis.Conn {
if prefix != "" {
prefix = prefix + "."
}
return &loggingConn{conn, logger, prefix, skip}
type Logger interface {
Debugf(format string, v ...interface{})
Errorf(format string, v ...interface{})
}
type loggingConn struct {
redis.Conn
logger *log.Logger
prefix string
skip func(cmdName string) bool
logger Logger
prefix string
logskip func(cmdName string) bool
}
// NewLoggingConn returns a logging wrapper around a connection.
func NewLoggingConn(conn redis.Conn, logger Logger, prefix string) redis.Conn {
return &loggingConn{conn, logger, prefix, nil}
}
// NewLoggingConnFilter returns a logging wrapper around a connection and a filter function.
func NewLoggingConnFilter(conn redis.Conn, logger Logger, prefix string, skip func(cmdName string) bool) redis.Conn {
return &loggingConn{conn, logger, prefix, skip}
}
func (c *loggingConn) Close() error {
err := c.Conn.Close()
var buf bytes.Buffer
fmt.Fprintf(&buf, "%sClose() -> (%v)", c.prefix, err)
c.logger.Output(logCallDepth, buf.String()) // nolint: errcheck
c.logger.Debugf("%sClose() -> (%v)", c.prefix, err)
return err
}
@ -98,7 +93,7 @@ func (c *loggingConn) printValue(buf *bytes.Buffer, v interface{}) {
}
func (c *loggingConn) print(method, commandName string, args []interface{}, reply interface{}, err error) {
if c.skip != nil && c.skip(commandName) {
if c.logskip != nil && c.logskip(commandName) {
return
}
var buf bytes.Buffer
@ -111,12 +106,15 @@ func (c *loggingConn) print(method, commandName string, args []interface{}, repl
}
}
buf.WriteString(") -> (")
if method != "Send" {
c.printValue(&buf, reply)
buf.WriteString(", ")
if err != nil {
fmt.Fprintf(&buf, "error: %v)", err)
} else {
if method != "Send" {
c.printValue(&buf, reply)
}
buf.WriteString(")")
}
fmt.Fprintf(&buf, "%v)", err)
c.logger.Output(logCallDepth+1, buf.String()) // nolint: errcheck
c.logger.Debugf("%s", buf.String())
}
func (c *loggingConn) Do(commandName string, args ...interface{}) (interface{}, error) {

View File

@ -1,11 +1,39 @@
package redis
import (
redigo "github.com/gomodule/redigo/redis"
"qoobing.com/gomod/redis/redis"
"qoobing.com/gomod/redis/sentinel"
)
type Config = sentinel.Config
type (
Conn = redigo.Conn
Pool = redigo.Pool
Config = sentinel.Config
)
var NewPool = redis.NewPool
var NewSentinelPool = sentinel.NewPool
var (
NewRedisPool = redis.NewPool
NewSentinelPool = sentinel.NewPool
Int = redigo.Int
Int64 = redigo.Int64
Uint64 = redigo.Uint64
Float64 = redigo.Float64
String = redigo.String
Strings = redigo.Strings
Bool = redigo.Bool
Ints = redigo.Ints
Float64s = redigo.Float64s
Values = redigo.Values
ErrNil = redigo.ErrNil
ErrPoolExhausted = redigo.ErrPoolExhausted
)
func NewPool(cfg Config) *redigo.Pool {
if cfg.Master != "" {
return NewRedisPool(cfg)
} else if cfg.MasterName != "" {
return NewSentinelPool(cfg)
}
panic("invalid config: Master & MasterName are both empty")
}

View File

@ -2,10 +2,10 @@ package redis
import (
"fmt"
"log"
"os"
"time"
"qoobing.com/gomod/log"
"github.com/gomodule/redigo/redis"
"qoobing.com/gomod/redis/logging"
"qoobing.com/gomod/redis/sentinel"
@ -51,11 +51,8 @@ func NewPool(cfg Config) *redis.Pool {
}
var (
logPrefix = "redis"
logStdPrefix = "DBUG "
logStdWriter = os.Stdout
logStdFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
logStdLogger = log.New(logStdWriter, logStdPrefix, logStdFlags)
logPrefix = "redis "
logLogger = log.New("redis")
)
return &redis.Pool{
MaxIdle: *cfg.MaxIdle,
@ -65,6 +62,7 @@ func NewPool(cfg Config) *redis.Pool {
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", masterAddr)
if err != nil {
logLogger.Errorf("connect [%s] error: %s", masterAddr, err)
return nil, err
}
@ -76,21 +74,15 @@ func NewPool(cfg Config) *redis.Pool {
}
if err != nil {
logLogger.Errorf("auth [%s] error: %s", masterAddr, err)
return nil, fmt.Errorf("redis master AUTH failed: <%s>", err.Error())
} else if okstr != "OK" {
logLogger.Errorf("auth [%s] not return ok but '%s'", masterAddr, okstr)
return nil, fmt.Errorf("redis master AUTH failed: <%s>", okstr)
}
//// if !TestRole(c, "master") {
//// c.Close()
//// err = fmt.Errorf(
//// "master(%s) got by name '%s' is not redis master",
//// masterAddr, masterName)
//// return nil, err
//// }
if cfg.Debug {
c = logging.NewLoggingConn(c, logStdLogger, logPrefix)
c = logging.NewLoggingConn(c, logLogger, logPrefix)
}
return c, nil
},

View File

@ -3,14 +3,13 @@ package sentinel
import (
"errors"
"fmt"
"log"
"net"
"os"
"strings"
"sync"
"time"
"github.com/gomodule/redigo/redis"
"qoobing.com/gomod/log"
"qoobing.com/gomod/redis/logging"
)
@ -29,6 +28,15 @@ type Config struct {
IdleTimeout *int `toml:"idle_timeout"` //空闲超时时间
}
func (cfg Config) GetSecDsn() (dsn string) {
if cfg.Master != "" {
dsn += fmt.Sprintf("mode=redis address=%s", cfg.Master)
} else if cfg.MasterName != "" {
dsn += fmt.Sprintf("mode=sentinel address=%s mastername=%s", cfg.Sentinels, cfg.MasterName)
}
return dsn
}
type Sentinel struct {
// Addrs is a slice with known Sentinel addresses.
Addrs []string
@ -114,13 +122,9 @@ func NewPool(cfg Config) *redis.Pool {
} else if *cfg.MaxActive < 0 {
*cfg.MaxActive = 100
}
var (
logPrefix = "redis"
logStdPrefix = "DBUG "
logStdWriter = os.Stdout
logStdFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
logStdLogger = log.New(logStdWriter, logStdPrefix, logStdFlags)
logPrefix = "redis "
logLogger = log.New("redis")
)
return &redis.Pool{
MaxIdle: *cfg.MaxIdle,
@ -130,11 +134,13 @@ func NewPool(cfg Config) *redis.Pool {
Dial: func() (redis.Conn, error) {
masterAddr, err := sntnl.MasterAddr()
if err != nil {
logLogger.Errorf("get master from sentinel error: %s", err)
return nil, err
}
c, err := redis.Dial("tcp", masterAddr)
if err != nil {
logLogger.Errorf("connect [%s] error: %s", masterAddr, err)
return nil, err
}
@ -146,8 +152,10 @@ func NewPool(cfg Config) *redis.Pool {
}
if err != nil {
logLogger.Errorf("auth [%s] error: %s", masterAddr, err)
return nil, fmt.Errorf("redis master AUTH failed: <%s>", err.Error())
} else if okstr != "OK" {
logLogger.Errorf("auth [%s] not return ok but '%s'", masterAddr, okstr)
return nil, fmt.Errorf("redis master AUTH failed: <%s>", okstr)
}
@ -156,11 +164,12 @@ func NewPool(cfg Config) *redis.Pool {
err = fmt.Errorf(
"master(%s) got by name '%s' is not redis master",
masterAddr, masterName)
logLogger.Errorf("%s", err.Error())
return nil, err
}
if cfg.Debug {
c = logging.NewLoggingConn(c, logStdLogger, logPrefix)
c = logging.NewLoggingConn(c, logLogger, logPrefix)
}
return c, nil
},