-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpubsub.go
66 lines (56 loc) · 1.37 KB
/
pubsub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package redjet
import (
"fmt"
)
type SubMessageType string
const (
SubMessageSubscribe SubMessageType = "subscribe"
SubMessageMessage SubMessageType = "message"
)
// SubMessage is a message received from a pubsub subscription.
type SubMessage struct {
// Type is either "subscribe" (acknowledgement of subscription) or "message"
Type SubMessageType
Channel string
// Payload is the number of channels subscribed to if Type is "subscribe",
Payload string
}
// NextSubMessage reads the next subscribe from the pipeline.
// Read more: https://redis.io/docs/manual/pubsub/.
//
// It does not close the Pipeline even if CloseOnRead is true.
func (r *Pipeline) NextSubMessage() (*SubMessage, error) {
// NextSubMessage is implemented without using internal methods to
// demonstrate how to use the public API.
ln, err := r.ArrayLength()
if err != nil {
return nil, err
}
if ln != 3 {
return nil, fmt.Errorf("expected 3 elements, got %d", ln)
}
var msg SubMessage
for i := 0; i < ln; i++ {
s, err := r.String()
if err != nil {
return nil, err
}
switch i {
case 0:
msg.Type = SubMessageType(s)
case 1:
msg.Channel = s
case 2:
msg.Payload = s
}
}
return &msg, nil
}
func isSubscribeCmd(cmd string) bool {
switch cmd {
case "SUBSCRIBE", "PSUBSCRIBE", "UNSUBSCRIBE", "PUNSUBSCRIBE", "QUIT", "RESET":
return true
default:
return false
}
}