This repository was archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrp.go
127 lines (113 loc) · 3.2 KB
/
rp.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2014 Brian Hetro <whee@smaertness.net>
// Use of this source code is governed by the ISC
// license which can be found in the LICENSE file.
//
// Package rp implements Reader and Writer interfaces to Redis Pub/Sub channels.
//
package rp
import redis "github.com/garyburd/redigo/redis"
// Config specifies the network and address for the Redis server.
type Config struct {
Network, Address string
}
// DefaultConfig is the network and address used for the Redis connection
// if none is specified.
var DefaultConfig = &Config{Network: "tcp", Address: ":6379"}
// GetNetwork returns the network used for the config.
func (c *Config) GetNetwork() string {
if c == nil {
return DefaultConfig.Network
}
return c.Network
}
// GetAddress returns the address used for the config.
func (c *Config) GetAddress() string {
if c == nil {
return DefaultConfig.Address
}
return c.Address
}
// Writer implements writing to a Redis Pub/Sub channel.
type Writer struct {
conn redis.Conn
names []string
}
// NewWriter returns a new Writer backed by the named Redis Pub/Sub
// channels. Writes are sent to each named channel.
// If config is nil, DefaultConfig is used.
// If unable to connect to Redis, the connection error is returned.
func NewWriter(config *Config, names ...string) (*Writer, error) {
conn, err := redis.Dial(config.GetNetwork(), config.GetAddress())
if err != nil {
return nil, err
}
return &Writer{conn, names}, nil
}
// Close closes the Redis connection.
func (w *Writer) Close() error {
return w.conn.Close()
}
// Write writes the contents of p to the Redis Pub/Sub channel via PUBLISH.
// It returns the number of bytes written.
// If the PUBLISH command fails, it returns why.
func (w *Writer) Write(p []byte) (n int, err error) {
n = len(p)
for _, c := range w.names {
_, err = w.conn.Do("PUBLISH", c, p)
if err != nil {
return
}
}
return
}
// Reader implements reading from a Redis Pub/Sub channel.
type Reader struct {
conn redis.Conn
names []string
psc redis.PubSubConn
}
// NewReader returns a new Reader backed by the named Redis Pub/Sub
// channels. Reads may return content from any channel.
// If config is nil, DefaultConfig is used.
// If unable to connect to Redis or subscribe to the named channel,
// the error is returned.
func NewReader(config *Config, names ...string) (r *Reader, err error) {
conn, err := redis.Dial(config.GetNetwork(), config.GetAddress())
if err != nil {
return
}
psc := redis.PubSubConn{conn}
r = &Reader{conn, names, psc}
for _, c := range names {
err = psc.Subscribe(c)
if err != nil {
return
}
}
return
}
// Read reads data from the Redis Pub/Sub channel into p.
// It returns the number of bytes read into p.
// If unable to receive from the Redis Pub/Sub channel, the error is returned.
func (r *Reader) Read(p []byte) (n int, err error) {
switch no := r.psc.Receive().(type) {
case redis.Message:
n = len(no.Data)
copy(p, no.Data)
case redis.PMessage:
case redis.Subscription:
case error:
err = no
return
}
return
}
// Close unsubscribes from the Redis Pub/Sub channel and closes
// the Redis connection.
func (r *Reader) Close() error {
err := r.psc.Unsubscribe()
if err != nil {
return err
}
return r.conn.Close()
}