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) {
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

23
hub.go
View file

@ -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
howmany := cl.Options.BacklogLength
if howmany > h.circbuf.Len() || howmany == -1 {
howmany = h.circbuf.Len()
}
if x != nil {
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)
}