/
source.go
206 lines (169 loc) · 4.67 KB
/
source.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package adapter
import (
//"context"
//"encoding/json"
"fmt"
"sync"
"time"
"unsafe"
eventbus "github.com/BrobridgeOrg/gravity-adapter-stan/pkg/eventbus/service"
jsoniter "github.com/json-iterator/go"
"github.com/nats-io/nats.go"
"github.com/nats-io/stan.go"
log "github.com/sirupsen/logrus"
)
var defaultInfo = SourceInfo{
DurableName: "DefaultGravity",
PingInterval: 10,
MaxPingsOutstanding: 3,
MaxReconnects: -1,
}
type Packet struct {
EventName string
Payload []byte
}
type Source struct {
adapter *Adapter
eventBus *eventbus.EventBus
name string
host string
port int
clusterID string
durableName string
channel string
pingInterval int64
maxPingsOutstanding int
maxReconnects int
}
var requestPool = sync.Pool{
New: func() interface{} {
return &Packet{}
},
}
func StrToBytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
func NewSource(adapter *Adapter, name string, sourceInfo *SourceInfo) *Source {
// required cluster ID
if len(sourceInfo.ClusterID) == 0 {
log.WithFields(log.Fields{
"source": name,
}).Error("Required cluster ID")
return nil
}
// required channel
if len(sourceInfo.Channel) == 0 {
log.WithFields(log.Fields{
"source": name,
}).Error("Required channel")
return nil
}
info := sourceInfo
// default settings
if defaultInfo.DurableName != info.DurableName {
info.DurableName = defaultInfo.DurableName
}
if defaultInfo.PingInterval != info.PingInterval {
info.PingInterval = defaultInfo.PingInterval
}
if defaultInfo.MaxPingsOutstanding != info.MaxPingsOutstanding {
info.MaxPingsOutstanding = defaultInfo.MaxPingsOutstanding
}
if defaultInfo.MaxReconnects != info.MaxReconnects {
info.MaxReconnects = defaultInfo.MaxReconnects
}
return &Source{
adapter: adapter,
name: name,
host: info.Host,
port: info.Port,
clusterID: info.ClusterID,
durableName: info.DurableName,
channel: info.Channel,
pingInterval: info.PingInterval,
maxPingsOutstanding: info.MaxPingsOutstanding,
maxReconnects: info.MaxReconnects,
}
}
func (source *Source) InitSubscription() error {
// Subscribe to channel
stanConn := source.eventBus.GetSTANConnection()
if len(source.durableName) == 0 {
// Subscribe without durable name
_, err := stanConn.Subscribe(source.channel, source.HandleMessage, stan.SetManualAckMode())
if err != nil {
return err
}
return nil
}
// Subscribe with durable name
_, err := stanConn.Subscribe(source.channel, source.HandleMessage, stan.DurableName(source.durableName), stan.SetManualAckMode())
if err != nil {
return err
}
return nil
}
func (source *Source) Init() error {
address := fmt.Sprintf("%s:%d", source.host, source.port)
log.WithFields(log.Fields{
"source": source.name,
"address": address,
"client_name": source.adapter.clientID + "-" + source.name,
"cluster_id": source.clusterID,
"durableName": source.durableName,
"channel": source.channel,
}).Info("Initializing source connector")
options := eventbus.Options{
ClusterID: source.clusterID,
ClientName: source.adapter.clientID + "-" + source.name,
PingInterval: time.Duration(source.pingInterval),
MaxPingsOutstanding: source.maxPingsOutstanding,
MaxReconnects: source.maxReconnects,
}
source.eventBus = eventbus.NewEventBus(
address,
eventbus.EventBusHandler{
Reconnect: func(natsConn *nats.Conn) {
err := source.InitSubscription()
if err != nil {
log.Error(err)
return
}
log.Warn("re-connected to event server")
},
Disconnect: func(natsConn *nats.Conn) {
log.Error("event server was disconnected")
},
},
options,
)
err := source.eventBus.Connect()
if err != nil {
return err
}
return source.InitSubscription()
}
func (source *Source) HandleMessage(m *stan.Msg) {
eventName := jsoniter.Get(m.Data, "event").ToString()
payload := jsoniter.Get(m.Data, "payload").ToString()
// Preparing request
request := requestPool.Get().(*Packet)
request.EventName = eventName
request.Payload = StrToBytes(payload)
meta := make(map[string]interface{})
meta["Msg-Id"] = fmt.Sprintf("%s-%s-%s", source.name, source.channel, m.Sequence)
for {
connector := source.adapter.app.GetAdapterConnector()
err := connector.Publish(request.EventName, request.Payload, meta)
if err != nil {
log.Error(err)
time.Sleep(time.Second)
continue
}
break
}
m.Ack()
requestPool.Put(request)
}