This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bus.go
181 lines (146 loc) · 3.38 KB
/
bus.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package bus
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/nsqio/go-nsq"
"github.com/opsee/basic/schema"
"github.com/opsee/keelhaul/config"
log "github.com/opsee/logrus"
"github.com/satori/go.uuid"
)
type Bus interface {
Start()
Publish(*Message) error
Subscribe(*schema.User) *Subscription
Unsubscribe(*Subscription)
Stop() error
}
type bus struct {
config *config.Config
channels map[string]map[*Subscription]bool
subscribeChan chan *Subscription
unsubscribeChan chan *Subscription
messageChan chan *Message
producer *nsq.Producer
consumer *nsq.Consumer
}
type Subscription struct {
CustomerID string
Chan chan *Message
}
func New(cfg *config.Config) (*bus, error) {
b := &bus{
config: cfg,
channels: make(map[string]map[*Subscription]bool),
subscribeChan: make(chan *Subscription),
unsubscribeChan: make(chan *Subscription),
messageChan: make(chan *Message),
}
channel := uuid.NewV4().String() + "#ephemeral"
lookupdAddrs := make([]string, 0)
for _, a := range strings.Split(cfg.NSQLookupds, ",") {
lookupdAddrs = append(lookupdAddrs, strings.Trim(a, " "))
}
nsqCfg := nsq.NewConfig()
nsqCfg.MaxInFlight = 4
consumer, err := nsq.NewConsumer(cfg.NSQTopic, channel, nsqCfg)
if err != nil {
return nil, err
}
consumer.AddConcurrentHandlers(b, 4)
consumer.ConnectToNSQLookupds(lookupdAddrs)
producer, err := nsq.NewProducer(cfg.NSQDAddr, nsq.NewConfig())
if err != nil {
return nil, err
}
b.consumer = consumer
b.producer = producer
return b, nil
}
func (b *bus) Start() {
go func() {
for {
select {
case sub := <-b.subscribeChan:
_, ok := b.channels[sub.CustomerID]
if !ok {
b.channels[sub.CustomerID] = make(map[*Subscription]bool)
}
b.channels[sub.CustomerID][sub] = true
case sub := <-b.unsubscribeChan:
channel, ok := b.channels[sub.CustomerID]
if !ok {
return
}
_, ok = channel[sub]
if ok {
close(sub.Chan)
delete(channel, sub)
}
if len(channel) == 0 {
delete(b.channels, sub.CustomerID)
}
case msg := <-b.messageChan:
channel, ok := b.channels[msg.CustomerID]
if ok {
for sub, _ := range channel {
sub.Chan <- msg
}
}
}
}
}()
}
func (b *bus) Publish(msg *Message) error {
err := msg.Validate()
if err != nil {
return err
}
msgBytes, err := json.Marshal(msg)
if err != nil {
return err
}
return b.producer.Publish(b.config.NSQTopic, msgBytes)
}
func (b *bus) Subscribe(user *schema.User) *Subscription {
sub := &Subscription{
CustomerID: user.CustomerId,
Chan: make(chan *Message),
}
b.subscribeChan <- sub
return sub
}
func (b *bus) Unsubscribe(sub *Subscription) {
b.unsubscribeChan <- sub
}
func (b *bus) HandleMessage(m *nsq.Message) error {
message := &Message{}
err := json.Unmarshal(m.Body, message)
if err != nil {
b.handleError(m, err)
return nil
}
err = message.Validate()
if err != nil {
b.handleError(m, err)
return nil
}
b.messageChan <- message
return nil
}
func (b *bus) Stop() error {
b.consumer.Stop()
var err error
select {
case <-b.consumer.StopChan:
err = nil
case <-time.After(15 * time.Second):
err = fmt.Errorf("timed out waiting for consumer shutdown")
}
return err
}
func (b *bus) handleError(m *nsq.Message, err error) {
log.WithError(err).Warn("error processing nsq message")
}