This repository has been archived by the owner on Apr 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
105 lines (94 loc) · 2.88 KB
/
collector.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
package main
import (
"fmt"
"math/rand"
"sync/atomic"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/iotaledger/hive.go/backoff"
"github.com/iotaledger/hive.go/daemon"
)
var initialRetryDelay = time.Second * 1
var maxRetryDelay = time.Second * 10
func spawnCollector(mqttURL string) {
name := fmt.Sprintf("collector-%s", mqttURL)
collectorLog := log.Named(name)
if err := daemon.BackgroundWorker(name, func(shutdownSignal <-chan struct{}) {
collectorLog.Infof("spawned collector")
defer collectorLog.Infof("collector stopped")
connLost := make(chan error, 1)
msgChan := make(chan mqtt.Message, 1)
connectCollector := func(c mqtt.Client) error {
if token := c.Connect(); token.Wait() && token.Error() != nil {
return fmt.Errorf("could not connect: %v", token.Error())
}
if token := c.Subscribe("spent_address", 0, func(client mqtt.Client, message mqtt.Message) {
msgChan <- message
}); token.Wait() && token.Error() != nil {
return fmt.Errorf("could not subscribe: %w", token.Error())
}
return nil
}
// mqtt client opts
opts := mqtt.NewClientOptions().AddBroker(fmt.Sprintf("tcp://%s", mqttURL))
opts.SetClientID(fmt.Sprintf("client-%d", rand.Int()%1000))
opts.SetCleanSession(true)
opts.SetConnectionLostHandler(func(c mqtt.Client, err error) { connLost <- err })
// connect
c := mqtt.NewClient(opts)
// 1, 2, 4, 8 seconds delay
collectorLog.Info("connecting...")
backOffPolicy := backoff.ExponentialBackOff(initialRetryDelay, 2).
With(backoff.MaxInterval(maxRetryDelay))
if err := backoff.Retry(backOffPolicy, func() error {
if err := connectCollector(c); err != nil {
collectorLog.Error(err)
return err
}
return nil
}); err != nil {
// note that this can never be hit, since we have no max retries
collectorLog.Errorf("stopped collector due to: %s", err)
}
collectorLog.Info("connected")
var collected int64
go func() {
ticker := time.NewTicker(time.Second * 10)
defer ticker.Stop()
for {
select {
case <-shutdownSignal:
return
case <-ticker.C:
collectorLog.Infof("collected %d addresses", atomic.LoadInt64(&collected))
}
}
}()
for {
select {
case <-shutdownSignal:
c.Disconnect(0)
return
case err := <-connLost:
collectorLog.Warnf("lost connection: '%s', trying to reconnect...", err)
if err := backoff.Retry(backOffPolicy, func() error {
if err := connectCollector(c); err != nil {
collectorLog.Error(err)
return err
}
return nil
}); err != nil {
// note that this can never be hit, since we have no max retries
collectorLog.Errorf("stopped collector due to: %s", err)
return
}
collectorLog.Info("connected")
case msg := <-msgChan:
atomic.AddInt64(&collected, 1)
spentAddrCache.Set(string(msg.Payload()), true)
}
}
}, PriorityCollector); err != nil {
log.Fatal(err)
}
}