pubsub.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "time"
  18. )
  19. // Subscription represents a subscribe or unsubscribe notification.
  20. type Subscription struct {
  21. // Kind is "subscribe", "unsubscribe", "psubscribe" or "punsubscribe"
  22. Kind string
  23. // The channel that was changed.
  24. Channel string
  25. // The current number of subscriptions for connection.
  26. Count int
  27. }
  28. // Message represents a message notification.
  29. type Message struct {
  30. // The originating channel.
  31. Channel string
  32. // The matched pattern, if any
  33. Pattern string
  34. // The message data.
  35. Data []byte
  36. }
  37. // Pong represents a pubsub pong notification.
  38. type Pong struct {
  39. Data string
  40. }
  41. // PubSubConn wraps a Conn with convenience methods for subscribers.
  42. type PubSubConn struct {
  43. Conn Conn
  44. }
  45. // Close closes the connection.
  46. func (c PubSubConn) Close() error {
  47. return c.Conn.Close()
  48. }
  49. // Subscribe subscribes the connection to the specified channels.
  50. func (c PubSubConn) Subscribe(channel ...interface{}) error {
  51. if err := c.Conn.Send("SUBSCRIBE", channel...); err != nil {
  52. return err
  53. }
  54. return c.Conn.Flush()
  55. }
  56. // PSubscribe subscribes the connection to the given patterns.
  57. func (c PubSubConn) PSubscribe(channel ...interface{}) error {
  58. if err := c.Conn.Send("PSUBSCRIBE", channel...); err != nil {
  59. return err
  60. }
  61. return c.Conn.Flush()
  62. }
  63. // Unsubscribe unsubscribes the connection from the given channels, or from all
  64. // of them if none is given.
  65. func (c PubSubConn) Unsubscribe(channel ...interface{}) error {
  66. if err := c.Conn.Send("UNSUBSCRIBE", channel...); err != nil {
  67. return err
  68. }
  69. return c.Conn.Flush()
  70. }
  71. // PUnsubscribe unsubscribes the connection from the given patterns, or from all
  72. // of them if none is given.
  73. func (c PubSubConn) PUnsubscribe(channel ...interface{}) error {
  74. if err := c.Conn.Send("PUNSUBSCRIBE", channel...); err != nil {
  75. return err
  76. }
  77. return c.Conn.Flush()
  78. }
  79. // Ping sends a PING to the server with the specified data.
  80. //
  81. // The connection must be subscribed to at least one channel or pattern when
  82. // calling this method.
  83. func (c PubSubConn) Ping(data string) error {
  84. if err := c.Conn.Send("PING", data); err != nil {
  85. return err
  86. }
  87. return c.Conn.Flush()
  88. }
  89. // Receive returns a pushed message as a Subscription, Message, Pong or error.
  90. // The return value is intended to be used directly in a type switch as
  91. // illustrated in the PubSubConn example.
  92. func (c PubSubConn) Receive() interface{} {
  93. return c.receiveInternal(c.Conn.Receive())
  94. }
  95. // ReceiveWithTimeout is like Receive, but it allows the application to
  96. // override the connection's default timeout.
  97. func (c PubSubConn) ReceiveWithTimeout(timeout time.Duration) interface{} {
  98. return c.receiveInternal(ReceiveWithTimeout(c.Conn, timeout))
  99. }
  100. func (c PubSubConn) receiveInternal(replyArg interface{}, errArg error) interface{} {
  101. reply, err := Values(replyArg, errArg)
  102. if err != nil {
  103. return err
  104. }
  105. var kind string
  106. reply, err = Scan(reply, &kind)
  107. if err != nil {
  108. return err
  109. }
  110. switch kind {
  111. case "message":
  112. var m Message
  113. if _, err := Scan(reply, &m.Channel, &m.Data); err != nil {
  114. return err
  115. }
  116. return m
  117. case "pmessage":
  118. var m Message
  119. if _, err := Scan(reply, &m.Pattern, &m.Channel, &m.Data); err != nil {
  120. return err
  121. }
  122. return m
  123. case "subscribe", "psubscribe", "unsubscribe", "punsubscribe":
  124. s := Subscription{Kind: kind}
  125. if _, err := Scan(reply, &s.Channel, &s.Count); err != nil {
  126. return err
  127. }
  128. return s
  129. case "pong":
  130. var p Pong
  131. if _, err := Scan(reply, &p.Data); err != nil {
  132. return err
  133. }
  134. return p
  135. }
  136. return errors.New("redigo: unknown pubsub notification")
  137. }