-
Notifications
You must be signed in to change notification settings - Fork 5
/
embedded.go
151 lines (120 loc) · 2.75 KB
/
embedded.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
144
145
146
147
148
149
150
151
package nsq
import (
"github.com/blushft/strana"
"github.com/blushft/strana/platform/bus/message"
"github.com/blushft/strana/platform/logger"
"github.com/nsqio/go-nsq"
"github.com/nsqio/nsq/nsqadmin"
"github.com/nsqio/nsq/nsqd"
"github.com/oklog/run"
"github.com/pkg/errors"
)
type nsqEmbedded struct {
opts Options
nsqd *nsqd.NSQD
nsqadmin *nsqadmin.NSQAdmin
stop chan struct{}
}
func newEmbedded(opts Options) (*nsqEmbedded, error) {
b, err := nsqd.New(opts.NSQOptions)
if err != nil {
return nil, err
}
var admin *nsqadmin.NSQAdmin
if opts.BrokerOptions.Web {
aopts := nsqadmin.NewOptions()
aopts.NSQDHTTPAddresses = []string{opts.NSQOptions.HTTPAddress}
admin, err = nsqadmin.New(aopts)
if err != nil {
return nil, errors.Wrap(err, "new nsqadmin")
}
}
return &nsqEmbedded{
opts: opts,
nsqd: b,
nsqadmin: admin,
stop: make(chan struct{}),
}, nil
}
func (nb *nsqEmbedded) Connect() error {
g := run.Group{}
g.Add(nb.nsqd.Main, func(error) {
nb.nsqd.Exit()
logger.Log().Info("nsqd exit")
})
if nb.nsqadmin != nil {
g.Add(nb.nsqadmin.Main, func(error) {
nb.nsqadmin.Exit()
logger.Log().Info("nsqadmin exit")
})
}
g.Add(func() error {
<-nb.stop
return nil
}, func(error) {})
return g.Run()
}
func (nb *nsqEmbedded) Disconnect() error {
close(nb.stop)
return nil
}
func (nb *nsqEmbedded) Mount(mod strana.Module) error {
return mod.Events(nb)
}
func (nb *nsqEmbedded) Handle(src, sink message.Path, h strana.EventHandlerFunc) error {
return nil
}
type nsqPublisher struct {
p *nsq.Producer
}
func (nb *nsqEmbedded) Publisher() strana.Publisher {
cfg := nsq.NewConfig()
p, err := nsq.NewProducer(nb.opts.NSQOptions.TCPAddress, cfg)
if err != nil {
return nil
}
return &nsqPublisher{
p: p,
}
}
func (np *nsqPublisher) Publish(p message.Path, m *message.Message) error {
e, err := m.Envelope()
if err != nil {
return err
}
return np.p.Publish(p.Topic, e)
}
func (np *nsqPublisher) Close() error {
np.p.Stop()
return nil
}
type nsqSubscriber struct {
c *nsq.Consumer
opts *nsqd.Options
h strana.SubscriptionHandlerFunc
}
func (nb *nsqEmbedded) Subscriber() strana.Subscriber {
return &nsqSubscriber{opts: nb.opts.NSQOptions}
}
func (ns *nsqSubscriber) Subscribe(p message.Path, fn strana.SubscriptionHandlerFunc) error {
ns.h = fn
cfg := nsq.NewConfig()
c, err := nsq.NewConsumer(p.Topic, p.Channel, cfg)
if err != nil {
return err
}
ns.c = c
c.AddHandler(nsq.HandlerFunc(ns.handle))
return c.ConnectToNSQD(ns.opts.TCPAddress)
}
func (ns *nsqSubscriber) handle(m *nsq.Message) error {
msg, err := message.Envelope(m.Body).Unmarshal()
if err != nil {
return err
}
return ns.h(msg)
}
func (ns *nsqSubscriber) Close() error {
ns.c.Stop()
return nil
}