-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmq.go
More file actions
68 lines (54 loc) · 1.28 KB
/
mq.go
File metadata and controls
68 lines (54 loc) · 1.28 KB
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
package queue
import (
"fmt"
"sync"
"unsafe"
"github.com/sirupsen/logrus"
)
// 内存模式 主题消息队列
var _ Queue = (*mQueue)(nil)
type mQueue struct {
topics
wg sync.WaitGroup
mux sync.Mutex
cfg Config
}
func NewMQueue(opts ...Option) *mQueue {
mq := &mQueue{
topics: make(topics),
cfg: NewConfig(opts...),
}
return mq
}
func (m *mQueue) AddTopic(topicName string, handle ConsumeHandle, handleErr ConsumeErrHandle) {
if te := m.topics.addTopic(topicName, handle, handleErr); te != nil {
te.bchan = make(chan batchEntry, 1)
}
}
func (m *mQueue) Produce(topicName string, ee ...Entry) error {
if _, ok := m.topics[topicName]; !ok {
return fmt.Errorf("Topic '%s' is not existed", topicName)
}
topicEntry := m.topics[topicName]
topicEntry.insertEntry(ee, m.cfg.batchSize, unsafe.Pointer(&m.mux))
return nil
}
func (m *mQueue) Consume(topicName string) {
te, ok := m.topics[topicName]
if !ok {
return
}
for b := range te.bchan {
m.wg.Add(1)
if err := te.topic.handle(topicName, b); err != nil && te.topic.handleErr != nil {
te.topic.handleErr(topicName, b, err)
}
m.wg.Done()
}
}
func (m *mQueue) Wait() {
m.wg.Wait()
}
func stdHandleErr(topicName string, batch batchEntry, err error) {
logrus.Errorf("[%s] %d %v", topicName, batch._id, err)
}