forked from mirror/redis
Improve API.
This commit is contained in:
parent
8d6a169007
commit
e90db6f494
|
@ -112,18 +112,19 @@ func ExamplePubSub() {
|
||||||
})
|
})
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
pubsub, err := client.PubSubClient()
|
pubsub := client.PubSub()
|
||||||
defer pubsub.Close()
|
defer pubsub.Close()
|
||||||
|
|
||||||
err = pubsub.Subscribe("mychannel")
|
err := pubsub.Subscribe("mychannel")
|
||||||
|
_ = err
|
||||||
|
|
||||||
msg, err := pubsub.Receive(0)
|
msg, err := pubsub.Receive()
|
||||||
fmt.Println(msg, err)
|
fmt.Println(msg, err)
|
||||||
|
|
||||||
pub := client.Publish("mychannel", "hello")
|
pub := client.Publish("mychannel", "hello")
|
||||||
_ = pub.Err()
|
_ = pub.Err()
|
||||||
|
|
||||||
msg, err = pubsub.Receive(0)
|
msg, err = pubsub.Receive()
|
||||||
fmt.Println(msg, err)
|
fmt.Println(msg, err)
|
||||||
|
|
||||||
// Output: &{subscribe mychannel 1} <nil>
|
// Output: &{subscribe mychannel 1} <nil>
|
||||||
|
|
26
v2/pubsub.go
26
v2/pubsub.go
|
@ -5,17 +5,17 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PubSubClient struct {
|
type PubSub struct {
|
||||||
*baseClient
|
*baseClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) PubSubClient() (*PubSubClient, error) {
|
func (c *Client) PubSub() *PubSub {
|
||||||
return &PubSubClient{
|
return &PubSub{
|
||||||
baseClient: &baseClient{
|
baseClient: &baseClient{
|
||||||
opt: c.opt,
|
opt: c.opt,
|
||||||
connPool: newSingleConnPool(c.connPool, nil, false),
|
connPool: newSingleConnPool(c.connPool, nil, false),
|
||||||
},
|
},
|
||||||
}, nil
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Publish(channel, message string) *IntReq {
|
func (c *Client) Publish(channel, message string) *IntReq {
|
||||||
|
@ -41,7 +41,11 @@ type Subscription struct {
|
||||||
Count int
|
Count int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PubSubClient) Receive(timeout time.Duration) (interface{}, error) {
|
func (c *PubSub) Receive() (interface{}, error) {
|
||||||
|
return c.ReceiveTimeout(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *PubSub) ReceiveTimeout(timeout time.Duration) (interface{}, error) {
|
||||||
cn, err := c.conn()
|
cn, err := c.conn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -80,7 +84,7 @@ func (c *PubSubClient) Receive(timeout time.Duration) (interface{}, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PubSubClient) subscribe(cmd string, channels ...string) error {
|
func (c *PubSub) subscribe(cmd string, channels ...string) error {
|
||||||
cn, err := c.conn()
|
cn, err := c.conn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -91,15 +95,15 @@ func (c *PubSubClient) subscribe(cmd string, channels ...string) error {
|
||||||
return c.writeReq(cn, req)
|
return c.writeReq(cn, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PubSubClient) Subscribe(channels ...string) error {
|
func (c *PubSub) Subscribe(channels ...string) error {
|
||||||
return c.subscribe("SUBSCRIBE", channels...)
|
return c.subscribe("SUBSCRIBE", channels...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PubSubClient) PSubscribe(patterns ...string) error {
|
func (c *PubSub) PSubscribe(patterns ...string) error {
|
||||||
return c.subscribe("PSUBSCRIBE", patterns...)
|
return c.subscribe("PSUBSCRIBE", patterns...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PubSubClient) unsubscribe(cmd string, channels ...string) error {
|
func (c *PubSub) unsubscribe(cmd string, channels ...string) error {
|
||||||
cn, err := c.conn()
|
cn, err := c.conn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -110,10 +114,10 @@ func (c *PubSubClient) unsubscribe(cmd string, channels ...string) error {
|
||||||
return c.writeReq(cn, req)
|
return c.writeReq(cn, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PubSubClient) Unsubscribe(channels ...string) error {
|
func (c *PubSub) Unsubscribe(channels ...string) error {
|
||||||
return c.unsubscribe("UNSUBSCRIBE", channels...)
|
return c.unsubscribe("UNSUBSCRIBE", channels...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PubSubClient) PUnsubscribe(patterns ...string) error {
|
func (c *PubSub) PUnsubscribe(patterns ...string) error {
|
||||||
return c.unsubscribe("PUNSUBSCRIBE", patterns...)
|
return c.unsubscribe("PUNSUBSCRIBE", patterns...)
|
||||||
}
|
}
|
||||||
|
|
|
@ -202,12 +202,12 @@ func (t *RedisConnectorTest) TestUnixConnector(c *C) {
|
||||||
// c.Assert(t.closedConns, Equals, int64(10))
|
// c.Assert(t.closedConns, Equals, int64(10))
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// func (t *RedisConnPoolTest) TestConnPoolMaxSizeOnPubSubClient(c *C) {
|
// func (t *RedisConnPoolTest) TestConnPoolMaxSizeOnPubSub(c *C) {
|
||||||
// wg := &sync.WaitGroup{}
|
// wg := &sync.WaitGroup{}
|
||||||
// for i := 0; i < 1000; i++ {
|
// for i := 0; i < 1000; i++ {
|
||||||
// wg.Add(1)
|
// wg.Add(1)
|
||||||
// go func() {
|
// go func() {
|
||||||
// pubsub, err := t.client.PubSubClient()
|
// pubsub, err := t.client.PubSub()
|
||||||
// c.Assert(err, IsNil)
|
// c.Assert(err, IsNil)
|
||||||
|
|
||||||
// _, err = pubsub.Subscribe()
|
// _, err = pubsub.Subscribe()
|
||||||
|
@ -2262,8 +2262,7 @@ func (t *RedisTest) TestZUnionStore(c *C) {
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
func (t *RedisTest) TestPatternPubSub(c *C) {
|
func (t *RedisTest) TestPatternPubSub(c *C) {
|
||||||
pubsub, err := t.client.PubSubClient()
|
pubsub := t.client.PubSub()
|
||||||
c.Assert(err, IsNil)
|
|
||||||
defer func() {
|
defer func() {
|
||||||
c.Assert(pubsub.Close(), IsNil)
|
c.Assert(pubsub.Close(), IsNil)
|
||||||
}()
|
}()
|
||||||
|
@ -2277,7 +2276,7 @@ func (t *RedisTest) TestPatternPubSub(c *C) {
|
||||||
c.Assert(pubsub.PUnsubscribe("mychannel*"), IsNil)
|
c.Assert(pubsub.PUnsubscribe("mychannel*"), IsNil)
|
||||||
|
|
||||||
{
|
{
|
||||||
msgi, err := pubsub.Receive(time.Second)
|
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
subscr := msgi.(*redis.Subscription)
|
subscr := msgi.(*redis.Subscription)
|
||||||
c.Assert(subscr.Kind, Equals, "psubscribe")
|
c.Assert(subscr.Kind, Equals, "psubscribe")
|
||||||
|
@ -2286,7 +2285,7 @@ func (t *RedisTest) TestPatternPubSub(c *C) {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
msgi, err := pubsub.Receive(time.Second)
|
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
subscr := msgi.(*redis.PMessage)
|
subscr := msgi.(*redis.PMessage)
|
||||||
c.Assert(subscr.Channel, Equals, "mychannel1")
|
c.Assert(subscr.Channel, Equals, "mychannel1")
|
||||||
|
@ -2295,7 +2294,7 @@ func (t *RedisTest) TestPatternPubSub(c *C) {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
msgi, err := pubsub.Receive(time.Second)
|
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
subscr := msgi.(*redis.Subscription)
|
subscr := msgi.(*redis.Subscription)
|
||||||
c.Assert(subscr.Kind, Equals, "punsubscribe")
|
c.Assert(subscr.Kind, Equals, "punsubscribe")
|
||||||
|
@ -2304,15 +2303,14 @@ func (t *RedisTest) TestPatternPubSub(c *C) {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
msgi, err := pubsub.Receive(time.Second)
|
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||||
c.Assert(err.(net.Error).Timeout(), Equals, true)
|
c.Assert(err.(net.Error).Timeout(), Equals, true)
|
||||||
c.Assert(msgi, IsNil)
|
c.Assert(msgi, IsNil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *RedisTest) TestPubSub(c *C) {
|
func (t *RedisTest) TestPubSub(c *C) {
|
||||||
pubsub, err := t.client.PubSubClient()
|
pubsub := t.client.PubSub()
|
||||||
c.Assert(err, IsNil)
|
|
||||||
defer func() {
|
defer func() {
|
||||||
c.Assert(pubsub.Close(), IsNil)
|
c.Assert(pubsub.Close(), IsNil)
|
||||||
}()
|
}()
|
||||||
|
@ -2330,7 +2328,7 @@ func (t *RedisTest) TestPubSub(c *C) {
|
||||||
c.Assert(pubsub.Unsubscribe("mychannel", "mychannel2"), IsNil)
|
c.Assert(pubsub.Unsubscribe("mychannel", "mychannel2"), IsNil)
|
||||||
|
|
||||||
{
|
{
|
||||||
msgi, err := pubsub.Receive(time.Second)
|
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
subscr := msgi.(*redis.Subscription)
|
subscr := msgi.(*redis.Subscription)
|
||||||
c.Assert(subscr.Kind, Equals, "subscribe")
|
c.Assert(subscr.Kind, Equals, "subscribe")
|
||||||
|
@ -2339,7 +2337,7 @@ func (t *RedisTest) TestPubSub(c *C) {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
msgi, err := pubsub.Receive(time.Second)
|
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
subscr := msgi.(*redis.Subscription)
|
subscr := msgi.(*redis.Subscription)
|
||||||
c.Assert(subscr.Kind, Equals, "subscribe")
|
c.Assert(subscr.Kind, Equals, "subscribe")
|
||||||
|
@ -2348,7 +2346,7 @@ func (t *RedisTest) TestPubSub(c *C) {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
msgi, err := pubsub.Receive(time.Second)
|
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
subscr := msgi.(*redis.Message)
|
subscr := msgi.(*redis.Message)
|
||||||
c.Assert(subscr.Channel, Equals, "mychannel")
|
c.Assert(subscr.Channel, Equals, "mychannel")
|
||||||
|
@ -2356,7 +2354,7 @@ func (t *RedisTest) TestPubSub(c *C) {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
msgi, err := pubsub.Receive(time.Second)
|
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
msg := msgi.(*redis.Message)
|
msg := msgi.(*redis.Message)
|
||||||
c.Assert(msg.Channel, Equals, "mychannel2")
|
c.Assert(msg.Channel, Equals, "mychannel2")
|
||||||
|
@ -2364,7 +2362,7 @@ func (t *RedisTest) TestPubSub(c *C) {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
msgi, err := pubsub.Receive(time.Second)
|
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
subscr := msgi.(*redis.Subscription)
|
subscr := msgi.(*redis.Subscription)
|
||||||
c.Assert(subscr.Kind, Equals, "unsubscribe")
|
c.Assert(subscr.Kind, Equals, "unsubscribe")
|
||||||
|
@ -2373,7 +2371,7 @@ func (t *RedisTest) TestPubSub(c *C) {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
msgi, err := pubsub.Receive(time.Second)
|
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
subscr := msgi.(*redis.Subscription)
|
subscr := msgi.(*redis.Subscription)
|
||||||
c.Assert(subscr.Kind, Equals, "unsubscribe")
|
c.Assert(subscr.Kind, Equals, "unsubscribe")
|
||||||
|
@ -2382,7 +2380,7 @@ func (t *RedisTest) TestPubSub(c *C) {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
msgi, err := pubsub.Receive(time.Second)
|
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||||
c.Assert(err.(net.Error).Timeout(), Equals, true)
|
c.Assert(err.(net.Error).Timeout(), Equals, true)
|
||||||
c.Assert(msgi, IsNil)
|
c.Assert(msgi, IsNil)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue