-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
47 lines (38 loc) · 1.16 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
package brokers
import (
"context"
"github.com/go-redis/redis/v8"
"github.com/pkg/errors"
"github.com/willdot/sendit/config"
"github.com/willdot/sendit/service"
)
// RedisPublisher is a publisher that can send messages to a Redis server
type RedisPublisher struct {
client *redis.Client
}
// NewRedisPublisher will create a connection to a Redis server. Shutdown on the returned publisher should be called
// to close the connection once finished
func NewRedisPublisher(cfg *config.Config) (*RedisPublisher, error) {
client := redis.NewClient(&redis.Options{
Addr: cfg.URL,
})
err := client.Ping(context.Background()).Err()
if err != nil {
return nil, errors.Wrap(err, "failed to connect to Redis")
}
return &RedisPublisher{
client: client,
}, nil
}
// Shutdown will close the Redis connection
func (p *RedisPublisher) Shutdown() {
_ = p.client.Shutdown(context.Background())
}
// Send will send the provided message
func (p *RedisPublisher) Send(destination string, msg service.Message) error {
err := p.client.Publish(context.Background(), destination, msg.Body).Err()
if err != nil {
return errors.Wrap(err, "failed to send message")
}
return nil
}