-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.go
61 lines (51 loc) · 1.32 KB
/
redis.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
package plasma_client
import (
"encoding/json"
"github.com/openfresh/plasma-go/config"
"github.com/openfresh/plasma-go/event"
"github.com/pkg/errors"
"gopkg.in/redis.v5"
)
const TypeRedis = "redis"
type Redis struct {
client *redis.Client
config config.Redis
channel string
}
func newRedis(config config.Config) (Publisher, error) {
redisConf := config.Redis
addr := redisConf.Addr
opt := &redis.Options{
Addr: addr,
Password: redisConf.Password,
DB: redisConf.DB,
}
client := redis.NewClient(opt)
return &Redis{
client: client,
config: redisConf,
channel: config.Redis.Channel,
}, nil
}
// NOTE: If Go version less than 1.8, RawMessage marshals as base64
// https://groups.google.com/forum/#!topic/Golang-Nuts/38ShOlhxAYY
type internalPayload struct {
Meta event.MetaData `json:"meta"`
Data *json.RawMessage `json:"data"`
}
// Publish sends payload to the redis channel
func (r *Redis) Publish(payload event.Payload) error {
eventType := payload.Meta.Type
p := internalPayload{
Meta: payload.Meta,
Data: &payload.Data,
}
message, err := json.Marshal(p)
if err != nil {
return errors.Wrap(err, "failed to marshal json")
}
if err := r.client.Publish(r.channel, string(message)).Err(); err != nil {
return errors.Wrapf(err, "failed to publish %s:%s", eventType, message)
}
return nil
}