This repository was archived by the owner on Nov 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumerproxy.go
More file actions
58 lines (47 loc) · 1.37 KB
/
consumerproxy.go
File metadata and controls
58 lines (47 loc) · 1.37 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
package decs
import (
"fmt"
"sync/atomic"
)
func (cbus *CommandBus) createConsumer(topic string) {
cbus.createTopic(topic)
cbus.createChannel(topic, cbus.commandChannel(topic))
cbus.createConsumerProxy(topic)
}
func (cbus *CommandBus) createTopic(topic string) {
err := cbus.producer.CreateTopic(topic)
if err != nil {
cbus.log.Sugar().Error("Failed to create topic", "topic", topic, "error", err)
}
}
func (cbus *CommandBus) createChannel(topic string, channel string) {
err := cbus.producer.CreateChannel(topic, channel)
if err != nil {
cbus.log.Sugar().Error("Failed to create channel", "topic", topic, "channel", channel, "error", err)
}
}
func (cbus *CommandBus) createConsumerProxy(topic string) {
callback := func(cmd *command) error {
if cmd.bus.filterLocalsOut(cmd) {
return nil
}
atomic.AddInt32(&cbus.incomingCount, 1)
cbus.queue.queue(cmd)
return nil
}
cbus.mqProvider.CreateConsumer(topic, cbus.commandChannel(topic), cbus.callbackProxy(callback))
}
func (cbus *CommandBus) commandChannel(topic string) string {
return fmt.Sprintf("%v-%v", topic, cbus.id)
}
func (cbus *CommandBus) callbackProxy(callback func(*command) error) func(bytes []byte) error {
return func(bytes []byte) error {
cmd := &command{bus: cbus}
err := cmd.UnmarshalJSON(bytes)
if err != nil {
cbus.log.Sugar().Error(err)
return err
}
return callback(cmd)
}
}