diff --git a/cmd/circologd/http.go b/cmd/circologd/http.go index f9d33f1..5129c7e 100644 --- a/cmd/circologd/http.go +++ b/cmd/circologd/http.go @@ -38,6 +38,7 @@ func parseParameterL(r *http.Request) (int, error) { func parseParameters(w http.ResponseWriter, r *http.Request) (circolog.ClientOptions, error) { var opts circolog.ClientOptions + opts.BacklogLength = 10 // default err := r.ParseForm() if err != nil { log.Println("error parsing http request", err) @@ -86,7 +87,9 @@ func getWSHandler(hub circolog.Hub) http.HandlerFunc { if err != nil { return } - client := circolog.Client{Messages: make(chan format.LogParts, 20)} + client := circolog.Client{Messages: make(chan format.LogParts, 20), + Options: circolog.ClientOptions{BacklogLength: -1}, + } 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 8af32c1..10bf8c1 100644 --- a/hub.go +++ b/hub.go @@ -49,21 +49,26 @@ func (h *Hub) register(cl Client) { h.clients[cl] = true } - circbufDoExit := false - h.circbuf.Do(func(x interface{}) { - if circbufDoExit { - return - } - if x != nil { + howmany := cl.Options.BacklogLength + if howmany > h.circbuf.Len() || howmany == -1 { + howmany = h.circbuf.Len() + } + buf := h.circbuf.Move(-howmany) + for i := 0; i < howmany; i++ { + item := buf.Value + if item != nil { select { // send with short timeout - case cl.Messages <- x.(format.LogParts): + case cl.Messages <- item.(format.LogParts): break case <-time.After(500 * time.Millisecond): - circbufDoExit = true - break + close(cl.Messages) + return } } - }) + + buf = buf.Next() + } + if cl.Options.Nofollow { close(cl.Messages) }