parent
2bb5e2d71b
commit
020db40b0c
12 changed files with 306 additions and 58 deletions
@ -0,0 +1,68 @@ |
||||
package utils |
||||
|
||||
import ( |
||||
redigoCluster "github.com/chasex/redis-go-cluster" |
||||
redigo "github.com/garyburd/redigo/redis" |
||||
) |
||||
|
||||
/* implement redigo.Conn(https://github.com/garyburd/redigo)
|
||||
* Embed redis-go-cluster(https://github.com/chasex/redis-go-cluster)
|
||||
* The reason I create this struct is that redis-go-cluster isn't fulfill redigo.Conn |
||||
* interface. So I implement "Err", "Send", "Flush" and "Receive" interfaces. |
||||
*/ |
||||
type ClusterConn struct { |
||||
client *redigoCluster.Cluster |
||||
recvChan chan reply |
||||
batcher *redigoCluster.Batch |
||||
} |
||||
|
||||
type reply struct { |
||||
answer interface{} |
||||
err error |
||||
} |
||||
|
||||
func NewClusterConn(clusterClient *redigoCluster.Cluster, recvChanSize int) redigo.Conn { |
||||
return &ClusterConn{ |
||||
client: clusterClient, |
||||
recvChan: make(chan reply, recvChanSize), |
||||
} |
||||
} |
||||
|
||||
func (cc *ClusterConn) Close() error { |
||||
cc.client.Close() |
||||
return nil |
||||
} |
||||
|
||||
func (cc *ClusterConn) Err() error { |
||||
return nil |
||||
} |
||||
|
||||
func (cc *ClusterConn) Do(commandName string, args ...interface{}) (reply interface{}, err error) { |
||||
return cc.client.Do(commandName, args...) |
||||
} |
||||
|
||||
// just add into batcher
|
||||
func (cc *ClusterConn) Send(commandName string, args ...interface{}) error { |
||||
if cc.batcher == nil { |
||||
cc.batcher = cc.client.NewBatch() |
||||
} |
||||
return cc.batcher.Put(commandName, args...) |
||||
} |
||||
|
||||
// send batcher and put the return into recvChan
|
||||
func (cc *ClusterConn) Flush() error { |
||||
ret, err := cc.client.RunBatch(cc.batcher) |
||||
cc.batcher = nil // reset batcher
|
||||
cc.recvChan <- reply{ |
||||
answer: ret, |
||||
err: err, |
||||
} |
||||
|
||||
return err |
||||
} |
||||
|
||||
// read recvChan
|
||||
func (cc *ClusterConn) Receive() (reply interface{}, err error) { |
||||
ret := <- cc.recvChan |
||||
return ret.answer, ret.err |
||||
} |
@ -0,0 +1,57 @@ |
||||
package utils |
||||
|
||||
import ( |
||||
"testing" |
||||
"fmt" |
||||
"sort" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestGetAllClusterNode(t *testing.T) { |
||||
var nr int |
||||
{ |
||||
fmt.Printf("TestGetAllClusterNode case %d.\n", nr) |
||||
nr++ |
||||
|
||||
client := OpenRedisConn([]string{"10.1.1.1:21333"}, "auth", "123456", false) |
||||
ret, err := GetAllClusterNode(client, "master") |
||||
sort.Strings(ret) |
||||
assert.Equal(t, nil, err, "should be equal") |
||||
assert.Equal(t, 3, len(ret), "should be equal") |
||||
assert.Equal(t, "10.1.1.1:21331", ret[0], "should be equal") |
||||
assert.Equal(t, "10.1.1.1:21332", ret[1], "should be equal") |
||||
assert.Equal(t, "10.1.1.1:21333", ret[2], "should be equal") |
||||
} |
||||
|
||||
{ |
||||
fmt.Printf("TestGetAllClusterNode case %d.\n", nr) |
||||
nr++ |
||||
|
||||
client := OpenRedisConn([]string{"10.1.1.1:21333"}, "auth", "123456", false) |
||||
ret, err := GetAllClusterNode(client, "slave") |
||||
sort.Strings(ret) |
||||
assert.Equal(t, nil, err, "should be equal") |
||||
assert.Equal(t, 3, len(ret), "should be equal") |
||||
assert.Equal(t, "10.1.1.1:21334", ret[0], "should be equal") |
||||
assert.Equal(t, "10.1.1.1:21335", ret[1], "should be equal") |
||||
assert.Equal(t, "10.1.1.1:21336", ret[2], "should be equal") |
||||
} |
||||
|
||||
{ |
||||
fmt.Printf("TestGetAllClusterNode case %d.\n", nr) |
||||
nr++ |
||||
|
||||
client := OpenRedisConn([]string{"10.1.1.1:21333"}, "auth", "123456", false) |
||||
ret, err := GetAllClusterNode(client, "all") |
||||
sort.Strings(ret) |
||||
assert.Equal(t, nil, err, "should be equal") |
||||
assert.Equal(t, 6, len(ret), "should be equal") |
||||
assert.Equal(t, "10.1.1.1:21331", ret[0], "should be equal") |
||||
assert.Equal(t, "10.1.1.1:21332", ret[1], "should be equal") |
||||
assert.Equal(t, "10.1.1.1:21333", ret[2], "should be equal") |
||||
assert.Equal(t, "10.1.1.1:21334", ret[3], "should be equal") |
||||
assert.Equal(t, "10.1.1.1:21335", ret[4], "should be equal") |
||||
assert.Equal(t, "10.1.1.1:21336", ret[5], "should be equal") |
||||
} |
||||
} |
Loading…
Reference in new issue