pubsub.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2012 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package redis
  15. import (
  16. "errors"
  17. pkgerr "github.com/pkg/errors"
  18. )
  19. var (
  20. errPubSub = errors.New("redigo: unknown pubsub notification")
  21. )
  22. // Subscription represents a subscribe or unsubscribe notification.
  23. type Subscription struct {
  24. // Kind is "subscribe", "unsubscribe", "psubscribe" or "punsubscribe"
  25. Kind string
  26. // The channel that was changed.
  27. Channel string
  28. // The current number of subscriptions for connection.
  29. Count int
  30. }
  31. // Message represents a message notification.
  32. type Message struct {
  33. // The originating channel.
  34. Channel string
  35. // The message data.
  36. Data []byte
  37. }
  38. // PMessage represents a pmessage notification.
  39. type PMessage struct {
  40. // The matched pattern.
  41. Pattern string
  42. // The originating channel.
  43. Channel string
  44. // The message data.
  45. Data []byte
  46. }
  47. // Pong represents a pubsub pong notification.
  48. type Pong struct {
  49. Data string
  50. }
  51. // PubSubConn wraps a Conn with convenience methods for subscribers.
  52. type PubSubConn struct {
  53. Conn Conn
  54. }
  55. // Close closes the connection.
  56. func (c PubSubConn) Close() error {
  57. return c.Conn.Close()
  58. }
  59. // Subscribe subscribes the connection to the specified channels.
  60. func (c PubSubConn) Subscribe(channel ...interface{}) error {
  61. c.Conn.Send("SUBSCRIBE", channel...)
  62. return c.Conn.Flush()
  63. }
  64. // PSubscribe subscribes the connection to the given patterns.
  65. func (c PubSubConn) PSubscribe(channel ...interface{}) error {
  66. c.Conn.Send("PSUBSCRIBE", channel...)
  67. return c.Conn.Flush()
  68. }
  69. // Unsubscribe unsubscribes the connection from the given channels, or from all
  70. // of them if none is given.
  71. func (c PubSubConn) Unsubscribe(channel ...interface{}) error {
  72. c.Conn.Send("UNSUBSCRIBE", channel...)
  73. return c.Conn.Flush()
  74. }
  75. // PUnsubscribe unsubscribes the connection from the given patterns, or from all
  76. // of them if none is given.
  77. func (c PubSubConn) PUnsubscribe(channel ...interface{}) error {
  78. c.Conn.Send("PUNSUBSCRIBE", channel...)
  79. return c.Conn.Flush()
  80. }
  81. // Ping sends a PING to the server with the specified data.
  82. func (c PubSubConn) Ping(data string) error {
  83. c.Conn.Send("PING", data)
  84. return c.Conn.Flush()
  85. }
  86. // Receive returns a pushed message as a Subscription, Message, PMessage, Pong
  87. // or error. The return value is intended to be used directly in a type switch
  88. // as illustrated in the PubSubConn example.
  89. func (c PubSubConn) Receive() interface{} {
  90. reply, err := Values(c.Conn.Receive())
  91. if err != nil {
  92. return err
  93. }
  94. var kind string
  95. reply, err = Scan(reply, &kind)
  96. if err != nil {
  97. return err
  98. }
  99. switch kind {
  100. case "message":
  101. var m Message
  102. if _, err := Scan(reply, &m.Channel, &m.Data); err != nil {
  103. return err
  104. }
  105. return m
  106. case "pmessage":
  107. var pm PMessage
  108. if _, err := Scan(reply, &pm.Pattern, &pm.Channel, &pm.Data); err != nil {
  109. return err
  110. }
  111. return pm
  112. case "subscribe", "psubscribe", "unsubscribe", "punsubscribe":
  113. s := Subscription{Kind: kind}
  114. if _, err := Scan(reply, &s.Channel, &s.Count); err != nil {
  115. return err
  116. }
  117. return s
  118. case "pong":
  119. var p Pong
  120. if _, err := Scan(reply, &p.Data); err != nil {
  121. return err
  122. }
  123. return p
  124. }
  125. return pkgerr.WithStack(errPubSub)
  126. }