-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.go
148 lines (122 loc) · 3.1 KB
/
dashboard.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
package dashboard
import (
"fmt"
"log"
"sync"
"golang.org/x/net/websocket"
)
const (
defaultMaxBacklog = 10
defaultPublishLatestData = true
)
var (
dsConns = map[string]map[DSClientId]*websocket.Conn{}
connLock sync.Mutex
lastClientId = DSClientId(0)
latestData = map[string][]byte{}
maxBacklog = defaultMaxBacklog
publishLatestData = defaultPublishLatestData
)
// DSClientId dashboard client id
type DSClientId uint32
// DSOptions dashboard configuration options
type DSOptions struct {
DataBacklog *int
PublishLatestData *bool
}
// SetOptions sets dashboard configuration options
func SetOptions(options *DSOptions) {
if options == nil {
return
}
if options.DataBacklog != nil {
maxBacklog = *options.DataBacklog
}
if options.PublishLatestData != nil {
publishLatestData = *options.PublishLatestData
}
}
// NewDashboard
func NewDashboard(topic string, conn *websocket.Conn) error {
id, err := registerConnection(topic, conn)
if err != nil {
return err
}
// publish latest data
if publishLatestData {
data := latestData[topic]
if data != nil && len(data) > 0 {
conn.Write(data)
}
}
return startClient(topic, id, conn)
}
// registerConnection registers a client connection
func registerConnection(topic string, conn *websocket.Conn) (DSClientId, error) {
if _, ok := dsTopics[topic]; !ok {
return DSClientId(0), fmt.Errorf("invalid topic '%s'", topic)
}
connLock.Lock()
defer connLock.Unlock()
topicConns, ok := dsConns[topic]
if !ok {
topicConns = map[DSClientId]*websocket.Conn{}
dsConns[topic] = topicConns
}
lastClientId++
topicConns[lastClientId] = conn
log.Printf("topic '%s' add client '%d', has #of clients: %d\n", topic, lastClientId, len(topicConns))
return lastClientId, nil
}
func unregisterConnection(topic string, id DSClientId) error {
connLock.Lock()
defer connLock.Unlock()
topicConns, ok := dsConns[topic]
if !ok {
return fmt.Errorf("invalid topic '%s'", topic)
}
if _, ok := topicConns[id]; !ok {
return fmt.Errorf("client '%d' not found", id)
}
delete(topicConns, id)
log.Printf("topic '%s' remove client '%d', remains #of clients: %d\n", topic, id, len(topicConns))
return nil
}
func startClient(topic string, id DSClientId, conn *websocket.Conn) error {
// wait for client colse
for {
msg := make([]byte, 512)
if _, err := conn.Read(msg); err != nil {
log.Println("[Closed] read real-time status:", err)
break
}
// received something else: just ignore
}
// delete client
if err := unregisterConnection(topic, id); err != nil {
return fmt.Errorf("unregister client '%d' from topic '%s' fail: %v\n",
id, topic, err)
}
return nil
}
// pushDataToClients pushes data to clients
func pushDataToClients(topic string, data []byte) error {
// catch latest data
if publishLatestData {
latestData[topic] = data
}
connLock.Lock()
defer connLock.Unlock()
topicConns, ok := dsConns[topic]
if !ok {
// nobody is interested in the topic
return nil
}
for _, q := range topicConns {
// async to avoid client no response
go func(conn *websocket.Conn) {
conn.Write(data)
}(q)
}
return nil
}