-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcounter_listener.go
54 lines (46 loc) · 1.27 KB
/
counter_listener.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
package mermaidlive
import (
"log"
"github.com/Arceliar/phony"
"github.com/cskr/pubsub/v2"
"github.com/d-led/percounter"
)
type CounterListener struct {
phony.Inbox
events *pubsub.PubSub[string, Event]
startedConnections int64
closedConnections int64
}
func NewCounterListener(events *pubsub.PubSub[string, Event]) *CounterListener {
return &CounterListener{
events: events,
}
}
func (n *CounterListener) OnNewCount(ev percounter.CountEvent) {
n.Act(n, func() {
switch ev.Name {
case NewConnectionsCounter:
log.Println("New visitor count:", ev.Count)
n.events.Pub(NewEventWithParam(TotalVisitorsEvent, ev.Count), Topic, ClusterMessageTopic)
case StartedConnectionsCounter:
log.Printf("started event: %v", ev)
n.startedConnections = ev.Count
n.events.Pub(NewEventWithParam(
TotalClusterVisitorsActiveEvent,
n.startedConnections-
n.closedConnections,
), Topic, ClusterMessageTopic)
case ClosedConnectionsCounter:
log.Printf("closed event: %v", ev)
n.closedConnections = ev.Count
n.events.Pub(NewEventWithParam(
TotalClusterVisitorsActiveEvent,
n.startedConnections-
n.closedConnections,
), Topic, ClusterMessageTopic)
default:
// ignore the event
// log.Printf("New counter event: %v", ev)
}
})
}