-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
116 lines (99 loc) · 2.93 KB
/
client.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
package grq
import (
"context"
"fmt"
"os"
"time"
"github.com/redis/go-redis/v9"
)
// DefaultConnectionString is a usual way to connect to redis running on 127.0.0.1:6379 without password authentication, and we use database 0
const DefaultConnectionString = "redis://127.0.0.1:6379/0"
// DefaultHeartbeat depicts interval between checking, if there is anything in channel, if we haven't received notification
const DefaultHeartbeat = 5 * time.Second
// ChannelPrefix sets prefix for notification channels to reduce chaos
const ChannelPrefix = "redisQueue/"
// ParseConnectionString parses connection string to generate redis connection options
func ParseConnectionString(connectionString string) (options *redis.Options, err error) {
return redis.ParseURL(connectionString)
}
// RedisQueue is struct that wraps redis client and provides Publish and Consume commands
type RedisQueue struct {
name string
options *redis.Options
heartbeat time.Duration
id string
client *redis.Client
listener *redis.Client
isConsumerRunning bool
ticker *time.Ticker
subscriber *redis.PubSub
stopper chan bool
startedAt time.Time
Context context.Context
CancelContext context.CancelFunc
}
// GetID returns consumer id
func (rq *RedisQueue) GetID() string {
return rq.id
}
// String returns string representation of consumer
func (rq *RedisQueue) String() string {
return rq.id
}
// GetQueueName returns queue name of this client
func (rq *RedisQueue) GetQueueName() string {
return rq.name
}
// Close closes all connections to redis
func (rq *RedisQueue) Close() (err error) {
err = rq.client.Close()
if err != nil {
return
}
if rq.isConsumerRunning {
return rq.Cancel()
}
return
}
// New creates new redis queue client with default configuration
func New(queue string) (rq *RedisQueue, err error) {
options := redis.Options{
Network: "tcp",
Addr: "127.0.0.1:6379",
}
return NewFromOptions(queue, options)
}
// NewFromOptions creates redis queue client from redis.options provided
func NewFromOptions(queue string, options redis.Options) (rq *RedisQueue, err error) {
hostname, err := os.Hostname()
if err != nil {
return
}
id, err := getRandomID()
if err != nil {
return
}
ctx, cancel := context.WithCancel(context.Background())
r := RedisQueue{
name: queue,
options: &options,
heartbeat: DefaultHeartbeat,
id: fmt.Sprintf("%s/%s/%s/%v", hostname, queue, id, os.Getpid()),
Context: ctx,
CancelContext: cancel,
}
r.client = redis.NewClient(r.options)
err = r.client.Ping(r.Context).Err()
if err != nil {
return
}
return &r, nil
}
// NewFromConnectionString creates redis queue client from connection string provided
func NewFromConnectionString(queue, connectionString string) (rq *RedisQueue, err error) {
options, err := ParseConnectionString(connectionString)
if err != nil {
return
}
return NewFromOptions(queue, *options)
}