-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.go
143 lines (114 loc) · 2.54 KB
/
producer.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package redis
import (
"fmt"
"log"
"sync"
redis "github.com/go-redis/redis/v7"
)
type Producer struct {
handle redis.UniversalClient
logger *log.Logger
wg sync.WaitGroup
mutex sync.Mutex
disposed bool
initialized bool
}
func NewProducer(config *ProducerConfig) (*Producer, error) {
instance := &Producer{}
var err error
err = instance.init(config)
if err != nil {
return nil, err
}
return instance, nil
}
func (p *Producer) Handle() redis.UniversalClient {
return p.handle
}
func (p *Producer) WriteContent(stream string, msg *MessageContent, opts ...ProduceMessageOption) (string, error) {
if p.disposed {
return "", fmt.Errorf("the Producer has been disposed")
}
if !p.initialized {
p.logger.Panic("the Producer haven't be initialized yet")
}
id := StreamAsteriskID
// apply options
for _, opt := range opts {
switch opt.(type) {
case ProduceMessageContentOption:
err := opt.applyContent(msg)
if err != nil {
return "", err
}
case ProduceMessageIDOption:
id = opt.applyID(id)
}
}
var values map[string]interface{}
msg.WriteTo(values)
return p.internalWrite(stream, id, values)
}
func (p *Producer) Write(stream string, values map[string]interface{}, opts ...ProduceMessageOption) (string, error) {
if p.disposed {
return "", fmt.Errorf("the Producer has been disposed")
}
if !p.initialized {
p.logger.Panic("the Producer haven't be initialized yet")
}
id := StreamAsteriskID
// apply options
for _, opt := range opts {
switch opt.(type) {
case ProduceMessageIDOption:
id = opt.applyID(id)
}
}
return p.internalWrite(stream, id, values)
}
func (p *Producer) Close() {
if p.disposed {
return
}
p.mutex.Lock()
defer p.mutex.Unlock()
p.disposed = true
p.wg.Wait()
p.handle.Close()
}
func (p *Producer) init(config *ProducerConfig) error {
if p.initialized {
return nil
}
client, err := createRedisUniversalClient(config.UniversalOptions)
if err != nil {
return err
}
// config logger
p.configureLogger(config)
p.handle = client
p.initialized = true
return nil
}
func (p *Producer) configureLogger(config *ProducerConfig) {
if config.Logger != nil {
p.logger = config.Logger
return
}
p.logger = defaultLogger
}
func (p *Producer) internalWrite(stream string, id string, values map[string]interface{}) (string, error) {
p.wg.Add(1)
defer p.wg.Done()
reply, err := p.handle.XAdd(&redis.XAddArgs{
Stream: stream,
ID: id,
Values: values,
}).Result()
if err != nil {
if err != redis.Nil {
return "", err
}
}
return reply, nil
}