pubsub.go 4.0 KB

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