-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.go
180 lines (151 loc) · 4.33 KB
/
cluster.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
package mermaidlive
import (
"fmt"
"log"
"os"
"slices"
"strings"
"time"
"github.com/cskr/pubsub/v2"
"github.com/d-led/percounter"
"github.com/d-led/zmqcluster"
)
const peerUpdateDelay = 5 * time.Second
var firstPeerCountUpdated = false
type Cluster struct {
events *pubsub.PubSub[string, Event]
peers []string
cluster zmqcluster.Cluster
counter *percounter.ZmqMultiGcounter
peerLocator PeerLocator
}
func NewCluster(events *pubsub.PubSub[string, Event], clusterEventObserver *PersistentClusterObserver, cluster zmqcluster.Cluster) *Cluster {
counterDirectory := GetCounterDirectory()
log.Println("Counter directory:", counterDirectory)
identity := GetCounterIdentity()
counterListener := NewCounterListener(events)
counter := percounter.NewObservableZmqMultiGcounterInCluster(
identity,
counterDirectory,
cluster,
counterListener,
)
counter.ShouldPersistOnSignal()
counter.SetClusterObserver(clusterEventObserver)
if err := counter.LoadAllSync(); err != nil {
log.Printf("failed to load all counters, continuing nonetheless: %v", err)
}
return &Cluster{
peerLocator: ChoosePeerLocator(),
events: events,
peers: []string{},
cluster: cluster,
counter: counter,
}
}
func GetCounterIdentity() string {
return getPrivateReplicaId()
}
func getFlyZmqBindAddr() string {
zmqPort := getFlyZmqPort()
return fmt.Sprintf("tcp://:%s", zmqPort)
}
func getFlyZmqPort() string {
if port, ok := os.LookupEnv("ZMQ_PORT"); ok {
return port
}
return "5000"
}
func (ps *Cluster) Start() {
go ps.listenToInternalEventsForever()
if ps.peerLocator == nil {
log.Println("not polling for peers")
return
}
log.Printf("Starting to poll for peers")
go ps.pollForever()
}
func (ps *Cluster) pollForever() {
ps.cluster.Start()
defer ps.counter.Stop()
for {
ps.getPeers()
time.Sleep(peerUpdateDelay)
}
}
func (ps *Cluster) listenToInternalEventsForever() {
subscription := ps.events.Sub(InternalTopic)
defer ps.events.Unsub(subscription, InternalTopic)
for event := range subscription {
switch event.Name {
case VisitorJoinedEvent:
ps.counter.Increment(NewConnectionsCounter)
ps.counter.Increment(StartedConnectionsCounter)
ps.events.Pub(NewEventWithParam(TotalVisitorsEvent, ps.counter.Value(NewConnectionsCounter)), Topic, ClusterMessageTopic)
sendInitialClusterConnectionCount(ps.events, ps.counter)
case VisitorLeftEvent:
ps.counter.Increment(ClosedConnectionsCounter)
}
}
}
func (ps *Cluster) getPeers() {
if ps.peerLocator == nil {
return
}
peers, replicaCount, err := ps.peerLocator.GetPeers()
if err != nil {
log.Printf("Error getting peers: %v", err)
// something might be off, better disconnect from everyone at this point
peers = []string{}
}
slices.Sort(peers)
if !slices.Equal(peers, ps.peers) || (len(ps.peers) == 0 && len(peers) != 0) {
log.Printf("Peers changed %v -> %v", ps.peers, peers)
ps.peers = peers
ps.counter.UpdatePeers(zmqPeers(peers))
ps.events.Pub(GetReplicasEvent(replicaCount), Topic, ClusterMessageTopic)
} else if !firstPeerCountUpdated {
ps.events.Pub(GetReplicasEvent(replicaCount), Topic, ClusterMessageTopic)
firstPeerCountUpdated = true
}
}
func zmqAddressOf(peer string) string {
return fmt.Sprintf("tcp://[%s]:%s", peer, getFlyZmqPort())
}
func zmqPeers(peers []string) []string {
res := []string{}
for _, peer := range peers {
res = append(res, zmqAddressOf(peer))
}
return res
}
func ChoosePeerLocator() PeerLocator {
flyDiscoveryDomainName := strings.TrimSpace(getFlyPeersDomain())
if flyDiscoveryDomainName != "" {
return NewFlyPeerLocator(flyDiscoveryDomainName)
}
traefikServicesUrl := getTraefikServicesUrl()
if traefikServicesUrl != "" {
return NewTraefikPeerLocator(traefikServicesUrl)
}
return &nullPeerLocator{}
}
func getTraefikServicesUrl() string {
return os.Getenv("TRAEFIK_SERVICES_URL")
}
func sendInitialClusterConnectionCount(
events *pubsub.PubSub[string, Event],
counter *percounter.ZmqMultiGcounter,
) {
started := counter.Value(StartedConnectionsCounter)
closed := counter.Value(ClosedConnectionsCounter)
events.Pub(NewEventWithParam(TotalClusterVisitorsActiveEvent, started-closed), Topic, ClusterMessageTopic)
}
type nullPeerLocator struct {
}
func (_ *nullPeerLocator) GetPeers() ([]string, int, error) {
return nil, 0, nil
}
func (_ *nullPeerLocator) GetMyIP() string {
return ""
}