-
Notifications
You must be signed in to change notification settings - Fork 0
/
producerproxy.go
72 lines (56 loc) · 1.4 KB
/
producerproxy.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
package decs
import (
"encoding/json"
"fmt"
"github.com/x-cellent/decs/mq"
"sync/atomic"
)
type producerProxy struct {
mq.Producer
cbus *CommandBus
}
func newProducerProxy(producer mq.Producer, cbus *CommandBus) *producerProxy {
return &producerProxy{
Producer: producer,
cbus: cbus,
}
}
func (p *producerProxy) Publish(topic string, data interface{}) error {
cmd := data.(*command)
bb, err := json.Marshal(cmd)
if err != nil {
return fmt.Errorf("cannot marshal data to json: %v", err)
}
if !cmd.internal && !cmd.Oneshot && !p.publishingSuspended() {
_ = p.Producer.Publish(topic, bb)
// defer command handling if localFilter is not active
if p.cbus.localFilter == nil {
return nil
}
}
cmd, err = cmd.bus.UnmarshalCommand(bb)
if err != nil {
return fmt.Errorf("cannot unmarshal command from json: %v", err)
}
if atomic.LoadInt32(&cmd.bus.incomingCount) > 0 {
cmd.bus.queue.queue(cmd)
return nil
}
cmd.bus.handleCommand(cmd)
return nil
}
func (p *producerProxy) CreateTopic(topic string) error {
if !p.publishingSuspended() {
return p.Producer.CreateTopic(topic)
}
return nil
}
func (p *producerProxy) CreateChannel(topic, channel string) error {
if !p.publishingSuspended() {
return p.Producer.CreateChannel(topic, channel)
}
return nil
}
func (p *producerProxy) publishingSuspended() bool {
return p == nil || p.Producer == nil || p.cbus.publishingSuspended
}