pubsub.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. c.Conn.Send("SUBSCRIBE", channel...)
  52. return c.Conn.Flush()
  53. }
  54. // PSubscribe subscribes the connection to the given patterns.
  55. func (c PubSubConn) PSubscribe(channel ...interface{}) error {
  56. c.Conn.Send("PSUBSCRIBE", channel...)
  57. return c.Conn.Flush()
  58. }
  59. // Unsubscribe unsubscribes the connection from the given channels, or from all
  60. // of them if none is given.
  61. func (c PubSubConn) Unsubscribe(channel ...interface{}) error {
  62. c.Conn.Send("UNSUBSCRIBE", channel...)
  63. return c.Conn.Flush()
  64. }
  65. // PUnsubscribe unsubscribes the connection from the given patterns, or from all
  66. // of them if none is given.
  67. func (c PubSubConn) PUnsubscribe(channel ...interface{}) error {
  68. c.Conn.Send("PUNSUBSCRIBE", channel...)
  69. return c.Conn.Flush()
  70. }
  71. // Ping sends a PING to the server with the specified data.
  72. //
  73. // The connection must be subscribed to at least one channel or pattern when
  74. // calling this method.
  75. func (c PubSubConn) Ping(data string) error {
  76. c.Conn.Send("PING", data)
  77. return c.Conn.Flush()
  78. }
  79. // Receive returns a pushed message as a Subscription, Message, Pong or error.
  80. // The return value is intended to be used directly in a type switch as
  81. // illustrated in the PubSubConn example.
  82. func (c PubSubConn) Receive() interface{} {
  83. return c.receiveInternal(c.Conn.Receive())
  84. }
  85. // ReceiveWithTimeout is like Receive, but it allows the application to
  86. // override the connection's default timeout.
  87. func (c PubSubConn) ReceiveWithTimeout(timeout time.Duration) interface{} {
  88. return c.receiveInternal(ReceiveWithTimeout(c.Conn, timeout))
  89. }
  90. func (c PubSubConn) receiveInternal(replyArg interface{}, errArg error) interface{} {
  91. reply, err := Values(replyArg, errArg)
  92. if err != nil {
  93. return err
  94. }
  95. var kind string
  96. reply, err = Scan(reply, &kind)
  97. if err != nil {
  98. return err
  99. }
  100. switch kind {
  101. case "message":
  102. var m Message
  103. if _, err := Scan(reply, &m.Channel, &m.Data); err != nil {
  104. return err
  105. }
  106. return m
  107. case "pmessage":
  108. var m Message
  109. if _, err := Scan(reply, &m.Pattern, &m.Channel, &m.Data); err != nil {
  110. return err
  111. }
  112. return m
  113. case "subscribe", "psubscribe", "unsubscribe", "punsubscribe":
  114. s := Subscription{Kind: kind}
  115. if _, err := Scan(reply, &s.Channel, &s.Count); err != nil {
  116. return err
  117. }
  118. return s
  119. case "pong":
  120. var p Pong
  121. if _, err := Scan(reply, &p.Data); err != nil {
  122. return err
  123. }
  124. return p
  125. }
  126. return errors.New("redigo: unknown pubsub notification")
  127. }