/
hub.go
131 lines (116 loc) · 3.19 KB
/
hub.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
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"log"
"strings"
"github.com/nicklaw5/helix"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
type Message struct {
ClientID string `json:"client_id"`
AudioURL string `json:"audio_url"`
Text string `json:"text"`
Emotes map[string]string `json:"emotes"`
UserName string `json:"username"`
UserPicture string `json:"user_picture"`
}
// Hub maintains the set of active clients and broadcasts messages to the
// clients.
type Hub struct {
// Registered clients.
clients map[string]*Client
// Inbound messages from the clients.
broadcast chan *Message
// Register requests from the clients.
register chan *Client
// Unregister requests from clients.
unregister chan *Client
}
type TwitchUser struct {
//Id string `json:"_id"`
//Type string `json:"type"`
//Bio string `json:"bio"`
//CreatedAt time.Time `json:"created_at"`
//UpdatedAt time.Time `json:"updated_at"`
DisplayName string `json:"display_name"`
Name string `json:"name"`
Logo string `json:"logo"`
}
var (
usersConnected = promauto.NewGauge(prometheus.GaugeOpts{
Name: "vox_twitch_connected_users_total",
Help: "The total number of connected users",
})
ttsGenerated = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "vox_twitch_generated_tts_total",
Help: "The total number of tts messages spoken",
}, []string{"channel_id"},
)
)
func newHub() *Hub {
return &Hub{
broadcast: make(chan *Message),
register: make(chan *Client),
unregister: make(chan *Client),
clients: make(map[string]*Client),
}
}
func (h *Hub) run() {
for {
usersConnected.Set(float64(len(h.clients)))
select {
case client := <-h.register:
h.clients[client.id] = client
h.printStatus()
case client := <-h.unregister:
if c, ok := h.clients[client.id]; ok {
if c.amqpChan != nil {
c.amqpChan.Close()
}
if c.amqpConn != nil {
c.amqpConn.Close()
}
delete(h.clients, client.id)
close(client.send)
h.printStatus()
}
case message := <-h.broadcast:
client := h.clients[message.ClientID]
select {
case client.send <- message:
ttsGenerated.With(prometheus.Labels{"channel_id": client.id}).Inc()
default:
close(client.send)
delete(h.clients, client.id)
}
}
}
}
func (h *Hub) printStatus() {
log.Println("Canais:\n------------\n",
strings.Join(clientIDs(h.clients), "\n"))
}
func (h *Hub) Online(client *helix.Client) (online []TwitchUser) {
resp, err := client.GetUsers(&helix.UsersParams{IDs: clientIDs(h.clients)})
if err != nil {
return
}
for _, user := range resp.Data.Users {
online = append(online, TwitchUser{
DisplayName: user.DisplayName,
Name: user.Login,
Logo: user.ProfileImageURL,
})
}
return
}
func clientIDs(clients map[string]*Client) (keys []string) {
keys = make([]string, 0, len(clients))
for k := range clients {
keys = append(keys, k)
}
return
}