From 8568280dd1af3a86750972b7d15aa1e75e3cee0e Mon Sep 17 00:00:00 2001 From: boyska Date: Thu, 23 Aug 2018 02:05:50 +0200 Subject: [PATCH] FIX timeout issues between channels we introduced non-blocking writes; however, being non-blocking when the goroutine has no buffer means that many messages will be considered lost. This commit change from "non blocking" to "max 500ms; first timeout means stop it all"; it also put a little buffer on client messages. --- cmd/circologd/http.go | 4 ++-- hub.go | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/cmd/circologd/http.go b/cmd/circologd/http.go index 3a6385a..d7f0121 100644 --- a/cmd/circologd/http.go +++ b/cmd/circologd/http.go @@ -17,7 +17,7 @@ func setupHttp(hub circolog.Hub) { func getHTTPHandler(hub circolog.Hub) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { client := circolog.Client{ - Messages: make(chan format.LogParts), + Messages: make(chan format.LogParts, 20), Nofollow: true} hub.Register <- client @@ -43,7 +43,7 @@ func getWSHandler(hub circolog.Hub) http.HandlerFunc { if err != nil { return } - client := circolog.Client{Messages: make(chan format.LogParts)} + client := circolog.Client{Messages: make(chan format.LogParts, 20)} hub.Register <- client // Allow collection of memory referenced by the caller by doing all work in diff --git a/hub.go b/hub.go index 81a9bc9..0f2fa3b 100644 --- a/hub.go +++ b/hub.go @@ -2,6 +2,7 @@ package circolog import ( "container/ring" + "time" "gopkg.in/mcuadros/go-syslog.v2/format" ) @@ -46,12 +47,18 @@ func (h *Hub) Run() { if !cl.Nofollow { // we won't need it in future h.clients[cl] = true } + + circbuf_do_exit := false h.circbuf.Do(func(x interface{}) { + if circbuf_do_exit { + return + } if x != nil { - select { // send without blocking + select { // send with short timeout case cl.Messages <- x.(format.LogParts): break - default: + case <-time.After(500 * time.Millisecond): + circbuf_do_exit = true break } }