forked from sony/appsync-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.go
171 lines (149 loc) · 3.59 KB
/
subscriber.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
package appsync
import (
"encoding/json"
"log"
"sync"
"time"
"github.com/cenkalti/backoff"
"github.com/mec07/appsync-client-go/graphql"
MQTT "github.com/eclipse/paho.mqtt.golang"
)
// Subscriber has MQTT connections and subscription information.
type Subscriber struct {
clientID string
topic string
url string
callback func(response *graphql.Response)
onConnectionLost func(err error)
started rwBool
mqtt MQTT.Client
}
const (
quiesce = 100
)
// NewSubscriber returns a new Subscriber instance.
func NewSubscriber(extensions Extensions, callback func(response *graphql.Response), onConnectionLost func(err error)) *Subscriber {
if len(extensions.Subscription.MqttConnections) == 0 {
log.Printf("There is no mqtt connections.\n%+v\n", extensions)
return nil
}
topic := func() string {
if len(extensions.Subscription.NewSubscriptions) > 1 {
log.Printf("Multiple subscriptions are currently not supported.\n%+v\n", extensions)
return ""
}
for _, v := range extensions.Subscription.NewSubscriptions {
return v.Topic
}
return ""
}()
if len(topic) == 0 {
log.Printf("There are no new subscriptions.\n%+v\n", extensions)
return nil
}
index := func() int {
for i, c := range extensions.Subscription.MqttConnections {
for _, t := range c.Topics {
if t == topic {
return i
}
}
}
return -1
}()
if index < 0 {
log.Printf("There are no new subscriptions.\n%+v\n", extensions)
return nil
}
mqttConnection := extensions.Subscription.MqttConnections[index]
return &Subscriber{
clientID: mqttConnection.Client,
url: mqttConnection.URL,
topic: topic,
callback: callback,
onConnectionLost: onConnectionLost,
started: rwBool{b: false},
}
}
type rwBool struct {
sync.RWMutex
b bool
}
func (r *rwBool) load() bool {
r.RLock()
defer r.RUnlock()
return r.b
}
func (r *rwBool) store(b bool) {
r.Lock()
defer r.Unlock()
r.b = b
}
// Start starts a new subscription.
func (s *Subscriber) Start() error {
opts := MQTT.NewClientOptions().
AddBroker(s.url).
SetClientID(s.clientID).
SetAutoReconnect(false).
SetConnectionLostHandler(func(c MQTT.Client, err error) {
if !s.started.load() {
return
}
s.onConnectionLost(err)
})
ch := make(chan error, 1)
defer close(ch)
opts.OnConnect = func(c MQTT.Client) {
mqttCallback := func(client MQTT.Client, msg MQTT.Message) {
if !s.started.load() {
return
}
r := new(graphql.Response)
if err := json.Unmarshal(msg.Payload(), r); err != nil {
log.Println(err)
return
}
s.callback(r)
}
subscribe := func() error {
if token := c.Subscribe(s.topic, 0, mqttCallback); token.Wait() && token.Error() != nil {
return token.Error()
}
s.started.store(true)
return nil
}
// Here be dragons.
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = 60 * time.Second
ch <- backoff.Retry(subscribe, b)
}
mqtt := MQTT.NewClient(opts)
connect := func() error {
if token := mqtt.Connect(); token.Wait() && token.Error() != nil {
return token.Error()
}
s.mqtt = mqtt
return nil
}
// Here be dragons.
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = 5 * time.Minute
if err := backoff.Retry(connect, b); err != nil {
return err
}
return <-ch
}
// Stop ends the subscription.
func (s *Subscriber) Stop() {
if s.mqtt == nil {
return
}
s.started.store(false)
defer func() {
s.mqtt.Disconnect(quiesce)
s.mqtt = nil
}()
if token := s.mqtt.Unsubscribe(s.topic); token.Wait() && token.Error() != nil {
log.Println(token.Error())
}
}