Skip to content

Commit 1d3093c

Browse files
committed
update example, optimize the time when data is first displayed
1 parent b797f63 commit 1d3093c

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

_example/fasthttp/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func main() {
3737
ws.HandleFunc("/debug/statsviz/ws", srv.Ws())
3838

3939
// Server start
40-
go http.Serve(m.Match(cmux.HTTP1HeaderField("Upgrade", "websocket")), ws)
40+
go http.Serve(m.Match(cmux.HTTP1HeaderField("Accept", "text/event-stream")), ws)
4141
go fasthttp.Serve(m.Match(cmux.Any()), r.Handler)
4242
fmt.Println("Point your browser to http://localhost:8083/debug/statsviz/")
4343
m.Serve()

_example/fiber/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func main() {
4343
fmt.Println("Point your browser to http://localhost:8093/debug/statsviz/")
4444

4545
// Server start
46-
go http.Serve(m.Match(cmux.HTTP1HeaderField("Upgrade", "websocket")), ws)
46+
go http.Serve(m.Match(cmux.HTTP1HeaderField("Accept", "text/event-stream")), ws)
4747
go app.Listener(m.Match(cmux.Any()))
4848
m.Serve()
4949
}

statsviz.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,33 @@ func (s *Server) startTransfer(w io.Writer) {
296296
<-c
297297
}
298298
func (s *Server) callData() {
299+
{
300+
//the first time it was sent immediately
301+
buffer := bytes.Buffer{}
302+
buffer.WriteString("data: ")
303+
if err := s.plots.WriteValues(&buffer); err == nil {
304+
buffer.WriteString("\n\n")
305+
err = s.onData[0](buffer.Bytes())
306+
if err != nil {
307+
s.lock.Lock()
308+
if len(s.onData) == 1 {
309+
s.onData = nil
310+
s.lock.Unlock()
311+
return
312+
} else {
313+
s.onData = s.onData[1:]
314+
}
315+
s.lock.Unlock()
316+
}
317+
}
318+
}
299319
tick := time.NewTicker(s.intv)
300320
defer tick.Stop()
301321
for range tick.C {
302322
buffer := bytes.Buffer{}
303323
buffer.WriteString("data: ")
304324
if err := s.plots.WriteValues(&buffer); err != nil {
325+
//maybe all connections should be closed?
305326
//fmt.Println("Error plots.WriteValues:", err)
306327
continue
307328
}

0 commit comments

Comments
 (0)