forked from minostauros/cluster-smi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster-smi-sse.go
222 lines (172 loc) · 4.88 KB
/
cluster-smi-sse.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"flag"
"fmt"
"encoding/json"
"net/http"
"github.com/minostauros/cluster-smi/cluster"
"github.com/pebbe/zmq4"
"github.com/vmihailenco/msgpack"
"log"
"os"
"time"
)
// the amount of time to wait when pushing a message to
// a slow client or a client that closed after `range clients` started.
const patience time.Duration = time.Second*1
// Example SSE server in Golang.
// $ go run sse.go
type Broker struct {
// Events are pushed to this channel by the main events-gathering routine
Notifier chan []byte
// New client connections
newClients chan chan []byte
// Closed client connections
closingClients chan chan []byte
// Client connections registry
clients map[chan []byte]bool
}
func NewServer() (broker *Broker) {
// Instantiate a broker
broker = &Broker{
Notifier: make(chan []byte, 1),
newClients: make(chan chan []byte),
closingClients: make(chan chan []byte),
clients: make(map[chan []byte]bool),
}
// Set it running - listening and broadcasting events
go broker.listen()
return
}
func (broker *Broker) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Make sure that the writer supports flushing.
//
flusher, ok := rw.(http.Flusher)
if !ok {
http.Error(rw, "Streaming unsupported!", http.StatusInternalServerError)
return
}
rw.Header().Set("Content-Type", "text/event-stream")
rw.Header().Set("Cache-Control", "no-cache")
rw.Header().Set("Connection", "keep-alive")
rw.Header().Set("Access-Control-Allow-Origin", "*")
// Each connection registers its own message channel with the Broker's connections registry
messageChan := make(chan []byte)
// Signal the broker that we have a new connection
broker.newClients <- messageChan
// Remove this client from the map of connected clients
// when this handler exits.
defer func() {
broker.closingClients <- messageChan
}()
// Listen to connection close and un-register messageChan
notify := rw.(http.CloseNotifier).CloseNotify()
for {
select {
case <-notify:
return
default:
// Write to the ResponseWriter
// Server Sent Events compatible
fmt.Fprintf(rw, "data: %s\n\n", <-messageChan)
// Flush the data immediatly instead of buffering it for later.
flusher.Flush()
}
}
}
func (broker *Broker) listen() {
for {
select {
case s := <-broker.newClients:
// A new client has connected.
// Register their message channel
broker.clients[s] = true
log.Printf("Client added. %d registered clients", len(broker.clients))
case s := <-broker.closingClients:
// A client has dettached and we want to
// stop sending them messages.
delete(broker.clients, s)
log.Printf("Removed client. %d registered clients", len(broker.clients))
case event := <-broker.Notifier:
// We got a new event from the outside!
// Send event to all connected clients
for clientMessageChan, _ := range broker.clients {
select {
case clientMessageChan <- event:
case <-time.After(patience):
log.Print("Skipping client.")
}
}
}
}
}
// dummy request for REQ-ROUTER pattern
type Request struct {
Identity string
}
func RequestUpdateMessage() (buf []byte, err error) {
id := fmt.Sprintf("REQ %v", os.Getpid())
req := Request{id}
return msgpack.Marshal(&req)
}
func main() {
// SSE stands for Server Sent Events
broker := NewServer()
flag.Parse()
request_attempts := 0
// load ports and ip-address
cfg := LoadConfig()
// ask for updates messages (REQ-ROUTER)
request_socket, err := zmq4.NewSocket(zmq4.REQ)
if err != nil {
log.Fatalf("Failed open Socket ZMQ: %s\n", err.Error())
panic(err)
}
defer request_socket.Close()
SocketAddr := "tcp://" + cfg.RouterIp + ":" + cfg.Ports.Clients
request_socket.Connect(SocketAddr)
go func() {
for {
// request new update
msg, err := RequestUpdateMessage()
if err != nil {
log.Fatal("request messsage error:", err)
panic(err)
}
_, err = request_socket.SendBytes(msg, 0)
if err != nil {
log.Fatal("sending request messsage error:", err)
panic(err)
}
// response from cluster-smi-server
s, err := request_socket.RecvBytes(0)
if err != nil {
log.Println(err)
time.Sleep(10 * time.Second)
request_attempts += 1
if request_attempts == 0 {
panic("too many request attempts yielding an error")
}
continue
}
var clus cluster.Cluster
err = msgpack.Unmarshal(s, &clus)
clus.Sort()
clusjson, err := json.Marshal(clus)
if err != nil {
panic(err)
}
// Send JSON formatted object to clients
broker.Notifier <- []byte(clusjson)
time.Sleep(time.Duration(cfg.Tick) * time.Second)
}
}()
if len(cfg.SSLCert) > 0 {
err := http.ListenAndServeTLS(":" + cfg.Ports.SSE, cfg.SSLCert, cfg.SSLKey, broker)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
} else {
log.Fatal("HTTP server error: ", http.ListenAndServe(":" + cfg.Ports.SSE, broker))
}
}