-
Notifications
You must be signed in to change notification settings - Fork 64
/
consumer.go
59 lines (50 loc) · 1.25 KB
/
consumer.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
package message
import (
"github.com/bricks-cloud/bricksllm/internal/event"
"github.com/bricks-cloud/bricksllm/internal/key"
"go.uber.org/zap"
)
type Consumer struct {
messageChan <-chan Message
done chan bool
log *zap.Logger
numOfEventConsumers int
handle func(Message) error
}
type recorder interface {
RecordKeySpend(keyId string, micros int64, costLimitUnit key.TimeUnit) error
RecordUserSpend(userId string, micros int64, costLimitUnit key.TimeUnit) error
RecordEvent(e *event.Event) error
}
func NewConsumer(mc <-chan Message, log *zap.Logger, num int, handle func(Message) error) *Consumer {
return &Consumer{
messageChan: mc,
done: make(chan bool),
log: log,
numOfEventConsumers: num,
handle: handle,
}
}
func (c *Consumer) StartEventMessageConsumers() {
for i := 0; i < c.numOfEventConsumers; i++ {
go func() {
for {
select {
case <-c.done:
c.log.Info("event message consumer stoped...")
return
case m := <-c.messageChan:
err := c.handle(m)
if err != nil {
continue
}
continue
}
}
}()
}
}
func (c *Consumer) Stop() {
c.log.Info("shutting down consumer...")
c.done <- true
}