-
Notifications
You must be signed in to change notification settings - Fork 5
/
redis_transport.go
60 lines (51 loc) · 944 Bytes
/
redis_transport.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
package gosumer
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
type Redis struct {
Host string
Port int
User string
Password string
DB uint8
Channel string
}
var rdb *redis.Client
var _ Transport = (*Redis)(nil)
func (red Redis) connect() error {
var err error
rdb = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", red.Host, red.Port),
Username: red.User,
Password: red.Password,
DB: int(red.DB),
})
ctx := context.Background()
err = rdb.Ping(ctx).Err()
if err != nil {
return err
}
return nil
}
func (red Redis) listen(fn process, message any, _ int) error {
err := red.connect()
if err != nil {
return err
}
if err != nil {
return err
}
ctx := context.Background()
sub := rdb.Subscribe(ctx, red.Channel)
defer sub.Close()
for {
msg, err := sub.ReceiveMessage(ctx)
if err != nil {
panic(err)
}
var e chan error
go fn(msg, e)
}
}