Compare commits

..

No commits in common. "v1.3.12" and "master" have entirely different histories.

6 changed files with 81 additions and 173 deletions

7
go.mod
View File

@ -4,8 +4,11 @@ go 1.18
require ( require (
github.com/gomodule/redigo v1.9.2 github.com/gomodule/redigo v1.9.2
qoobing.com/gomod/log v1.4.2 qoobing.com/gomod/log v1.2.8
qoobing.com/gomod/str v1.0.5 qoobing.com/gomod/str v1.0.5
) )
require github.com/tylerb/gls v0.0.0-20150407001822-e606233f194d // indirect require (
github.com/tylerb/gls v0.0.0-20150407001822-e606233f194d // indirect
github.com/tylerb/is v2.1.4+incompatible // indirect
)

15
go.sum
View File

@ -1,15 +0,0 @@
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.4.2 h1:BR6WA79nUfvYhHww/Nbzu/iM9RdCfx3G3hg1XusjIIM=
qoobing.com/gomod/log v1.4.2/go.mod h1:rNXuq0d/EWog4+8hIEVGvkusLD/pzafYBQo6w+Evv6A=
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,6 +18,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"log"
"time" "time"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
@ -25,43 +26,41 @@ import (
var ( var (
_ redis.ConnWithTimeout = (*loggingConn)(nil) _ redis.ConnWithTimeout = (*loggingConn)(nil)
logCallDepth = 3
) )
type Logger interface { // NewLoggingConn returns a logging wrapper around a connection.
Debugf(format string, v ...interface{}) func NewLoggingConn(conn redis.Conn, logger *log.Logger, prefix string) redis.Conn {
Errorf(format string, v ...interface{}) 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 loggingConn struct { type loggingConn struct {
redis.Conn redis.Conn
startSend []struct { logger *log.Logger
start time.Time
commandName string
args []interface{}
}
logger Logger
prefix string prefix string
logskip func(cmdName string) bool skip 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, nil, 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, nil, logger, prefix, skip}
} }
func (c *loggingConn) Close() error { func (c *loggingConn) Close() error {
c.startSend = nil
err := c.Conn.Close() err := c.Conn.Close()
//c.logger.Debugf("%sClose() -> (%v)", c.prefix, err) var buf bytes.Buffer
fmt.Fprintf(&buf, "%sClose() -> (%v)", c.prefix, err)
c.logger.Output(logCallDepth, buf.String()) // nolint: errcheck
return err return err
} }
func (c *loggingConn) formatPrint(buf *bytes.Buffer, v interface{}) { func (c *loggingConn) printValue(buf *bytes.Buffer, v interface{}) {
const chop = 128 const chop = 128
switch v := v.(type) { switch v := v.(type) {
case []byte: case []byte:
@ -88,7 +87,7 @@ func (c *loggingConn) formatPrint(buf *bytes.Buffer, v interface{}) {
} }
for _, vv := range v { for _, vv := range v {
buf.WriteString(sep) buf.WriteString(sep)
c.formatPrint(buf, vv) c.printValue(buf, vv)
sep = ", " sep = ", "
} }
buf.WriteString(fin) buf.WriteString(fin)
@ -98,95 +97,66 @@ func (c *loggingConn) formatPrint(buf *bytes.Buffer, v interface{}) {
} }
} }
func (c *loggingConn) trace(method string, start time.Time, commandName string, args []interface{}, reply interface{}, err error) { func (c *loggingConn) print(method, commandName string, args []interface{}, reply interface{}, err error) {
if commandName == "" { if c.skip != nil && c.skip(commandName) {
return return
} }
if c.logskip != nil && c.logskip(commandName) { var buf bytes.Buffer
return fmt.Fprintf(&buf, "%s%s(", c.prefix, method)
} if method != "Receive" {
var cost float64 buf.WriteString(commandName)
var bufCmd bytes.Buffer
var bufReply bytes.Buffer
if !start.IsZero() {
cost = float64(time.Since(start).Microseconds()) / 1000.0
} else {
cost = 0.0
}
bufCmd.WriteString(commandName)
for _, arg := range args { for _, arg := range args {
bufCmd.WriteString(", ") buf.WriteString(", ")
c.formatPrint(&bufCmd, arg) c.printValue(&buf, arg)
} }
if err != nil {
fmt.Fprintf(&bufReply, "\x1b[31merror: %v\x1b[0m", err)
} else {
c.formatPrint(&bufReply, reply)
} }
c.logger.Debugf("\x1b[33m[%.3fms]\x1b[0m %s%s(%s) -> (%s)", cost, c.prefix, method, bufCmd.String(), bufReply.String()) buf.WriteString(") -> (")
if method != "Send" {
c.printValue(&buf, reply)
buf.WriteString(", ")
}
fmt.Fprintf(&buf, "%v)", err)
c.logger.Output(logCallDepth+1, buf.String()) // nolint: errcheck
} }
func (c *loggingConn) Do(commandName string, args ...interface{}) (interface{}, error) { func (c *loggingConn) Do(commandName string, args ...interface{}) (interface{}, error) {
start := time.Now()
reply, err := c.Conn.Do(commandName, args...) reply, err := c.Conn.Do(commandName, args...)
c.trace("Do", start, commandName, args, reply, err) c.print("Do", commandName, args, reply, err)
return reply, err return reply, err
} }
func (c *loggingConn) DoContext(ctx context.Context, commandName string, args ...interface{}) (interface{}, error) { func (c *loggingConn) DoContext(ctx context.Context, commandName string, args ...interface{}) (interface{}, error) {
start := time.Now()
reply, err := redis.DoContext(c.Conn, ctx, commandName, args...) reply, err := redis.DoContext(c.Conn, ctx, commandName, args...)
c.trace("DoContext", start, commandName, args, reply, err) c.print("DoContext", commandName, args, reply, err)
return reply, err return reply, err
} }
func (c *loggingConn) DoWithTimeout(timeout time.Duration, commandName string, args ...interface{}) (interface{}, error) { func (c *loggingConn) DoWithTimeout(timeout time.Duration, commandName string, args ...interface{}) (interface{}, error) {
start := time.Now()
reply, err := redis.DoWithTimeout(c.Conn, timeout, commandName, args...) reply, err := redis.DoWithTimeout(c.Conn, timeout, commandName, args...)
c.trace("DoWithTimeout", start, commandName, args, reply, err) c.print("DoWithTimeout", commandName, args, reply, err)
return reply, err return reply, err
} }
func (c *loggingConn) pushSendArgs(commandName string, args []interface{}) {
c.startSend = append(c.startSend, struct {
start time.Time
commandName string
args []interface{}
}{time.Now(), commandName, args})
}
func (c *loggingConn) popSendArgs() (time.Time, string, []interface{}) {
if len(c.startSend) > 0 {
send := c.startSend[0]
c.startSend = c.startSend[1:]
return send.start, send.commandName, send.args
}
return time.Time{}, "", nil
}
func (c *loggingConn) Send(commandName string, args ...interface{}) error { func (c *loggingConn) Send(commandName string, args ...interface{}) error {
c.pushSendArgs(commandName, args)
err := c.Conn.Send(commandName, args...) err := c.Conn.Send(commandName, args...)
c.print("Send", commandName, args, nil, err)
return err return err
} }
func (c *loggingConn) Receive() (interface{}, error) { func (c *loggingConn) Receive() (interface{}, error) {
reply, err := c.Conn.Receive() reply, err := c.Conn.Receive()
start, commandName, args := c.popSendArgs() c.print("Receive", "", nil, reply, err)
c.trace("Receive", start, commandName, args, reply, err)
return reply, err return reply, err
} }
func (c *loggingConn) ReceiveContext(ctx context.Context) (interface{}, error) { func (c *loggingConn) ReceiveContext(ctx context.Context) (interface{}, error) {
reply, err := redis.ReceiveContext(c.Conn, ctx) reply, err := redis.ReceiveContext(c.Conn, ctx)
start, commandName, args := c.popSendArgs() c.print("ReceiveContext", "", nil, reply, err)
c.trace("ReceiveContext", start, commandName, args, reply, err)
return reply, err return reply, err
} }
func (c *loggingConn) ReceiveWithTimeout(timeout time.Duration) (interface{}, error) { func (c *loggingConn) ReceiveWithTimeout(timeout time.Duration) (interface{}, error) {
reply, err := redis.ReceiveWithTimeout(c.Conn, timeout) reply, err := redis.ReceiveWithTimeout(c.Conn, timeout)
start, commandName, args := c.popSendArgs() c.print("ReceiveWithTimeout", "", nil, reply, err)
c.trace("ReceiveWithTimeout", start, commandName, args, reply, err)
return reply, err return reply, err
} }

View File

@ -1,39 +1,11 @@
package redis package redis
import ( import (
redigo "github.com/gomodule/redigo/redis"
"qoobing.com/gomod/redis/redis" "qoobing.com/gomod/redis/redis"
"qoobing.com/gomod/redis/sentinel" "qoobing.com/gomod/redis/sentinel"
) )
type ( type Config = sentinel.Config
Conn = redigo.Conn
Pool = redigo.Pool
Config = sentinel.Config
)
var ( var NewPool = redis.NewPool
NewRedisPool = redis.NewPool var NewSentinelPool = sentinel.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,11 +2,10 @@ package redis
import ( import (
"fmt" "fmt"
"strings" "log"
"os"
"time" "time"
"qoobing.com/gomod/log"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
"qoobing.com/gomod/redis/logging" "qoobing.com/gomod/redis/logging"
"qoobing.com/gomod/redis/sentinel" "qoobing.com/gomod/redis/sentinel"
@ -53,18 +52,11 @@ func NewPool(cfg Config) *redis.Pool {
var ( var (
logPrefix = "redis" logPrefix = "redis"
logLogger = log.New("redis") logStdPrefix = "DBUG "
logStdWriter = os.Stdout
logStdFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
logStdLogger = log.New(logStdWriter, logStdPrefix, logStdFlags)
) )
logLogger.SetCalldepth(3)
logLogger.SetSkipfunc(func(filename string) bool {
if strings.Contains(filename, "/github.com/gomodule/redigo") {
return true
}
if strings.Contains(filename, "/qoobing.com/gomod/") {
return true
}
return false
})
return &redis.Pool{ return &redis.Pool{
MaxIdle: *cfg.MaxIdle, MaxIdle: *cfg.MaxIdle,
MaxActive: *cfg.MaxActive, MaxActive: *cfg.MaxActive,
@ -73,7 +65,6 @@ func NewPool(cfg Config) *redis.Pool {
Dial: func() (redis.Conn, error) { Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", masterAddr) c, err := redis.Dial("tcp", masterAddr)
if err != nil { if err != nil {
logLogger.Errorf("connect [%s] error: %s", masterAddr, err)
return nil, err return nil, err
} }
@ -85,15 +76,21 @@ func NewPool(cfg Config) *redis.Pool {
} }
if err != nil { if err != nil {
logLogger.Errorf("auth [%s] error: %s", masterAddr, err)
return nil, fmt.Errorf("redis master AUTH failed: <%s>", err.Error()) return nil, fmt.Errorf("redis master AUTH failed: <%s>", err.Error())
} else if okstr != "OK" { } 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) 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 { if cfg.Debug {
c = logging.NewLoggingConn(c, logLogger, logPrefix) c = logging.NewLoggingConn(c, logStdLogger, logPrefix)
} }
return c, nil return c, nil
}, },

View File

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