mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
parent
93a447c5dd
commit
355736ec35
2 changed files with 13 additions and 2 deletions
|
@ -15,6 +15,7 @@ package livereload
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
@ -25,6 +26,16 @@ type connection struct {
|
||||||
|
|
||||||
// Buffered channel of outbound messages.
|
// Buffered channel of outbound messages.
|
||||||
send chan []byte
|
send chan []byte
|
||||||
|
|
||||||
|
// There is a potential data race, especially visible with large files.
|
||||||
|
// This is protected by synchronisation of the send channel's close.
|
||||||
|
closer sync.Once
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *connection) close() {
|
||||||
|
c.closer.Do(func() {
|
||||||
|
close(c.send)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connection) reader() {
|
func (c *connection) reader() {
|
||||||
|
|
|
@ -41,14 +41,14 @@ func (h *hub) run() {
|
||||||
h.connections[c] = true
|
h.connections[c] = true
|
||||||
case c := <-h.unregister:
|
case c := <-h.unregister:
|
||||||
delete(h.connections, c)
|
delete(h.connections, c)
|
||||||
close(c.send)
|
c.close()
|
||||||
case m := <-h.broadcast:
|
case m := <-h.broadcast:
|
||||||
for c := range h.connections {
|
for c := range h.connections {
|
||||||
select {
|
select {
|
||||||
case c.send <- m:
|
case c.send <- m:
|
||||||
default:
|
default:
|
||||||
delete(h.connections, c)
|
delete(h.connections, c)
|
||||||
close(c.send)
|
c.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue