refactor: hub reorganized a little

This commit is contained in:
boyska 2018-08-23 12:25:07 +02:00
parent 5b7ddb62a6
commit 34593d380a
3 changed files with 30 additions and 26 deletions

View file

@ -9,7 +9,7 @@ import (
"gopkg.in/mcuadros/go-syslog.v2/format" "gopkg.in/mcuadros/go-syslog.v2/format"
) )
func setupHttp(hub circolog.Hub) { func setupHTTP(hub circolog.Hub) {
http.HandleFunc("/", getHTTPHandler(hub)) http.HandleFunc("/", getHTTPHandler(hub))
http.HandleFunc("/ws", getWSHandler(hub)) http.HandleFunc("/ws", getWSHandler(hub))
} }

View file

@ -46,7 +46,7 @@ func main() {
} }
go hub.Run() go hub.Run()
setupHttp(hub) setupHTTP(hub)
if *querySocket != "" { if *querySocket != "" {
fmt.Printf("Binding address `%s` [http]\n", *querySocket) fmt.Printf("Binding address `%s` [http]\n", *querySocket)
unixListener, err := net.Listen("unix", *querySocket) unixListener, err := net.Listen("unix", *querySocket)

20
hub.go
View file

@ -38,19 +38,15 @@ func NewHub(ringBufSize int) Hub {
} }
} }
// Run is hub main loop; keeps everything going func (h *Hub) register(cl Client) {
func (h *Hub) Run() {
for {
select {
case cl := <-h.Register:
if _, ok := h.clients[cl]; !ok { if _, ok := h.clients[cl]; !ok {
if !cl.Nofollow { // we won't need it in future if !cl.Nofollow { // we won't need it in future
h.clients[cl] = true h.clients[cl] = true
} }
circbuf_do_exit := false circbufDoExit := false
h.circbuf.Do(func(x interface{}) { h.circbuf.Do(func(x interface{}) {
if circbuf_do_exit { if circbufDoExit {
return return
} }
if x != nil { if x != nil {
@ -58,7 +54,7 @@ func (h *Hub) Run() {
case cl.Messages <- x.(format.LogParts): case cl.Messages <- x.(format.LogParts):
break break
case <-time.After(500 * time.Millisecond): case <-time.After(500 * time.Millisecond):
circbuf_do_exit = true circbufDoExit = true
break break
} }
} }
@ -67,6 +63,14 @@ func (h *Hub) Run() {
close(cl.Messages) close(cl.Messages)
} }
} }
}
// Run is hub main loop; keeps everything going
func (h *Hub) Run() {
for {
select {
case cl := <-h.Register:
h.register(cl)
case cl := <-h.Unregister: case cl := <-h.Unregister:
_, ok := h.clients[cl] _, ok := h.clients[cl]
if ok { if ok {