make tail with param ?l= (HTTP only)

This commit is contained in:
boyska 2018-11-08 19:37:03 +01:00
parent fbc0f96647
commit f4e6370abd
2 changed files with 19 additions and 11 deletions

View file

@ -38,6 +38,7 @@ func parseParameterL(r *http.Request) (int, error) {
func parseParameters(w http.ResponseWriter, r *http.Request) (circolog.ClientOptions, error) { func parseParameters(w http.ResponseWriter, r *http.Request) (circolog.ClientOptions, error) {
var opts circolog.ClientOptions var opts circolog.ClientOptions
opts.BacklogLength = 10 // default
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
log.Println("error parsing http request", err) log.Println("error parsing http request", err)
@ -86,7 +87,9 @@ func getWSHandler(hub circolog.Hub) http.HandlerFunc {
if err != nil { if err != nil {
return 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 hub.Register <- client
// Allow collection of memory referenced by the caller by doing all work in // Allow collection of memory referenced by the caller by doing all work in

25
hub.go
View file

@ -49,21 +49,26 @@ func (h *Hub) register(cl Client) {
h.clients[cl] = true h.clients[cl] = true
} }
circbufDoExit := false howmany := cl.Options.BacklogLength
h.circbuf.Do(func(x interface{}) { if howmany > h.circbuf.Len() || howmany == -1 {
if circbufDoExit { howmany = h.circbuf.Len()
return }
} buf := h.circbuf.Move(-howmany)
if x != nil { for i := 0; i < howmany; i++ {
item := buf.Value
if item != nil {
select { // send with short timeout select { // send with short timeout
case cl.Messages <- x.(format.LogParts): case cl.Messages <- item.(format.LogParts):
break break
case <-time.After(500 * time.Millisecond): case <-time.After(500 * time.Millisecond):
circbufDoExit = true close(cl.Messages)
break return
} }
} }
})
buf = buf.Next()
}
if cl.Options.Nofollow { if cl.Options.Nofollow {
close(cl.Messages) close(cl.Messages)
} }