forked from mirror/redis
commit
77d8805e91
|
@ -10,7 +10,6 @@ go:
|
||||||
- tip
|
- tip
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- go get github.com/golang/glog
|
|
||||||
- go get gopkg.in/bufio.v1
|
- go get gopkg.in/bufio.v1
|
||||||
- go get gopkg.in/check.v1
|
- go get gopkg.in/check.v1
|
||||||
- mkdir -p $HOME/gopath/src/gopkg.in
|
- mkdir -p $HOME/gopath/src/gopkg.in
|
||||||
|
|
8
pool.go
8
pool.go
|
@ -3,11 +3,11 @@ package redis
|
||||||
import (
|
import (
|
||||||
"container/list"
|
"container/list"
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
|
||||||
"gopkg.in/bufio.v1"
|
"gopkg.in/bufio.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -139,7 +139,7 @@ func (p *connPool) Get() (*conn, bool, error) {
|
||||||
}
|
}
|
||||||
if time.Since(cn.usedAt) > p.opt.IdleTimeout {
|
if time.Since(cn.usedAt) > p.opt.IdleTimeout {
|
||||||
if err := p.remove(cn); err != nil {
|
if err := p.remove(cn); err != nil {
|
||||||
glog.Errorf("remove failed: %s", err)
|
log.Printf("remove failed: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,7 +183,7 @@ func (p *connPool) Get() (*conn, bool, error) {
|
||||||
func (p *connPool) Put(cn *conn) error {
|
func (p *connPool) Put(cn *conn) error {
|
||||||
if cn.rd.Buffered() != 0 {
|
if cn.rd.Buffered() != 0 {
|
||||||
b, _ := cn.rd.ReadN(cn.rd.Buffered())
|
b, _ := cn.rd.ReadN(cn.rd.Buffered())
|
||||||
glog.Errorf("redis: connection has unread data: %q", b)
|
log.Printf("redis: connection has unread data: %q", b)
|
||||||
return p.Remove(cn)
|
return p.Remove(cn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -267,7 +267,7 @@ func (p *connPool) Close() error {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err := p.remove(e.Value.(*conn)); err != nil {
|
if err := p.remove(e.Value.(*conn)); err != nil {
|
||||||
glog.Errorf("cn.Close failed: %s", err)
|
log.Printf("cn.Close failed: %s", err)
|
||||||
retErr = err
|
retErr = err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
7
redis.go
7
redis.go
|
@ -1,10 +1,9 @@
|
||||||
package redis
|
package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type baseClient struct {
|
type baseClient struct {
|
||||||
|
@ -82,13 +81,13 @@ func (c *baseClient) freeConn(cn *conn, ei error) error {
|
||||||
|
|
||||||
func (c *baseClient) removeConn(cn *conn) {
|
func (c *baseClient) removeConn(cn *conn) {
|
||||||
if err := c.connPool.Remove(cn); err != nil {
|
if err := c.connPool.Remove(cn); err != nil {
|
||||||
glog.Errorf("pool.Remove failed: %s", err)
|
log.Printf("pool.Remove failed: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *baseClient) putConn(cn *conn) {
|
func (c *baseClient) putConn(cn *conn) {
|
||||||
if err := c.connPool.Put(cn); err != nil {
|
if err := c.connPool.Put(cn); err != nil {
|
||||||
glog.Errorf("pool.Put failed: %s", err)
|
log.Printf("pool.Put failed: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
33
sentinel.go
33
sentinel.go
|
@ -2,12 +2,11 @@ package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
@ -149,11 +148,11 @@ func (d *sentinelFailover) MasterAddr() (string, error) {
|
||||||
if d._sentinel != nil {
|
if d._sentinel != nil {
|
||||||
addr, err := d._sentinel.GetMasterAddrByName(d.masterName).Result()
|
addr, err := d._sentinel.GetMasterAddrByName(d.masterName).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("redis-sentinel: GetMasterAddrByName %s failed: %s", d.masterName, err)
|
log.Printf("redis-sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err)
|
||||||
d.resetSentinel()
|
d.resetSentinel()
|
||||||
} else {
|
} else {
|
||||||
addr := net.JoinHostPort(addr[0], addr[1])
|
addr := net.JoinHostPort(addr[0], addr[1])
|
||||||
glog.Infof("redis-sentinel: %s addr is %s", d.masterName, addr)
|
log.Printf("redis-sentinel: %q addr is %s", d.masterName, addr)
|
||||||
return addr, nil
|
return addr, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -174,14 +173,14 @@ func (d *sentinelFailover) MasterAddr() (string, error) {
|
||||||
})
|
})
|
||||||
addr, err := sentinel.GetMasterAddrByName(d.masterName).Result()
|
addr, err := sentinel.GetMasterAddrByName(d.masterName).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("redis-sentinel: GetMasterAddrByName %s failed: %s", d.masterName, err)
|
log.Printf("redis-sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err)
|
||||||
} else {
|
} else {
|
||||||
// Push working sentinel to the top.
|
// Push working sentinel to the top.
|
||||||
d.sentinelAddrs[0], d.sentinelAddrs[i] = d.sentinelAddrs[i], d.sentinelAddrs[0]
|
d.sentinelAddrs[0], d.sentinelAddrs[i] = d.sentinelAddrs[i], d.sentinelAddrs[0]
|
||||||
|
|
||||||
d.setSentinel(sentinel)
|
d.setSentinel(sentinel)
|
||||||
addr := net.JoinHostPort(addr[0], addr[1])
|
addr := net.JoinHostPort(addr[0], addr[1])
|
||||||
glog.Infof("redis-sentinel: %s addr is %s", d.masterName, addr)
|
log.Printf("redis-sentinel: %q addr is %s", d.masterName, addr)
|
||||||
return addr, nil
|
return addr, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -198,7 +197,7 @@ func (d *sentinelFailover) setSentinel(sentinel *sentinelClient) {
|
||||||
func (d *sentinelFailover) discoverSentinels(sentinel *sentinelClient) {
|
func (d *sentinelFailover) discoverSentinels(sentinel *sentinelClient) {
|
||||||
sentinels, err := sentinel.Sentinels(d.masterName).Result()
|
sentinels, err := sentinel.Sentinels(d.masterName).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("redis-sentinel: Sentinels %s failed: %s", d.masterName, err)
|
log.Printf("redis-sentinel: Sentinels %q failed: %s", d.masterName, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, sentinel := range sentinels {
|
for _, sentinel := range sentinels {
|
||||||
|
@ -208,8 +207,8 @@ func (d *sentinelFailover) discoverSentinels(sentinel *sentinelClient) {
|
||||||
if key == "name" {
|
if key == "name" {
|
||||||
sentinelAddr := vals[i+1].(string)
|
sentinelAddr := vals[i+1].(string)
|
||||||
if !contains(d.sentinelAddrs, sentinelAddr) {
|
if !contains(d.sentinelAddrs, sentinelAddr) {
|
||||||
glog.Infof(
|
log.Printf(
|
||||||
"redis-sentinel: discovered new sentinel for %s: %s",
|
"redis-sentinel: discovered new %q sentinel: %s",
|
||||||
d.masterName, sentinelAddr,
|
d.masterName, sentinelAddr,
|
||||||
)
|
)
|
||||||
d.sentinelAddrs = append(d.sentinelAddrs, sentinelAddr)
|
d.sentinelAddrs = append(d.sentinelAddrs, sentinelAddr)
|
||||||
|
@ -225,7 +224,7 @@ func (d *sentinelFailover) listen() {
|
||||||
if pubsub == nil {
|
if pubsub == nil {
|
||||||
pubsub = d._sentinel.PubSub()
|
pubsub = d._sentinel.PubSub()
|
||||||
if err := pubsub.Subscribe("+switch-master"); err != nil {
|
if err := pubsub.Subscribe("+switch-master"); err != nil {
|
||||||
glog.Errorf("redis-sentinel: Subscribe failed: %s", err)
|
log.Printf("redis-sentinel: Subscribe failed: %s", err)
|
||||||
d.lock.Lock()
|
d.lock.Lock()
|
||||||
d.resetSentinel()
|
d.resetSentinel()
|
||||||
d.lock.Unlock()
|
d.lock.Unlock()
|
||||||
|
@ -235,7 +234,7 @@ func (d *sentinelFailover) listen() {
|
||||||
|
|
||||||
msgIface, err := pubsub.Receive()
|
msgIface, err := pubsub.Receive()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("redis-sentinel: Receive failed: %s", err)
|
log.Printf("redis-sentinel: Receive failed: %s", err)
|
||||||
pubsub = nil
|
pubsub = nil
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -246,17 +245,17 @@ func (d *sentinelFailover) listen() {
|
||||||
case "+switch-master":
|
case "+switch-master":
|
||||||
parts := strings.Split(msg.Payload, " ")
|
parts := strings.Split(msg.Payload, " ")
|
||||||
if parts[0] != d.masterName {
|
if parts[0] != d.masterName {
|
||||||
glog.Errorf("redis-sentinel: ignore new %s addr", parts[0])
|
log.Printf("redis-sentinel: ignore new %s addr", parts[0])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
addr := net.JoinHostPort(parts[3], parts[4])
|
addr := net.JoinHostPort(parts[3], parts[4])
|
||||||
glog.Infof(
|
log.Printf(
|
||||||
"redis-sentinel: new %s addr is %s",
|
"redis-sentinel: new %q addr is %s",
|
||||||
d.masterName, addr,
|
d.masterName, addr,
|
||||||
)
|
)
|
||||||
d.pool.Filter(func(cn *conn) bool {
|
d.pool.Filter(func(cn *conn) bool {
|
||||||
if cn.RemoteAddr().String() != addr {
|
if cn.RemoteAddr().String() != addr {
|
||||||
glog.Infof(
|
log.Printf(
|
||||||
"redis-sentinel: closing connection to old master %s",
|
"redis-sentinel: closing connection to old master %s",
|
||||||
cn.RemoteAddr(),
|
cn.RemoteAddr(),
|
||||||
)
|
)
|
||||||
|
@ -265,12 +264,12 @@ func (d *sentinelFailover) listen() {
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
default:
|
default:
|
||||||
glog.Errorf("redis-sentinel: unsupported message: %s", msg)
|
log.Printf("redis-sentinel: unsupported message: %s", msg)
|
||||||
}
|
}
|
||||||
case *Subscription:
|
case *Subscription:
|
||||||
// Ignore.
|
// Ignore.
|
||||||
default:
|
default:
|
||||||
glog.Errorf("redis-sentinel: unsupported message: %s", msgIface)
|
log.Printf("redis-sentinel: unsupported message: %s", msgIface)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue