From a0b57e1a78acc4bad6a8c29df46f3e12cbedfbee Mon Sep 17 00:00:00 2001 From: Blallo Date: Fri, 9 Nov 2018 11:21:50 +0100 Subject: [PATCH] [http][ctail] n parameter also on circolog-tails --- cmd/circolog-tail/main.go | 7 +++++++ cmd/circologd/http.go | 15 +++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/circolog-tail/main.go b/cmd/circolog-tail/main.go index 7c3fb4b..5b25ba7 100644 --- a/cmd/circolog-tail/main.go +++ b/cmd/circolog-tail/main.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "os/signal" + "strconv" "time" "github.com/gorilla/websocket" @@ -16,6 +17,7 @@ import ( func main() { addr := flag.String("addr", "localhost:9080", "http service address") querySocket := flag.String("socket", "", "Path to a unix domain socket for the HTTP server") + backlogLimit := flag.Int("n", -1, "Limit the backlog length, defaults to no limit (-1)") flag.Parse() interrupt := make(chan os.Signal, 1) @@ -25,6 +27,11 @@ func main() { Host: *addr, // ignored in case of -socket; see the Dialer below Path: "/ws", } + q := u.Query() + if *backlogLimit >= 0 { + q.Set("l", strconv.Itoa(*backlogLimit)) + } + u.RawQuery = q.Encode() if *querySocket != "" { d = &websocket.Dialer{ NetDial: func(network, addr string) (net.Conn, error) { diff --git a/cmd/circologd/http.go b/cmd/circologd/http.go index 5129c7e..8bec128 100644 --- a/cmd/circologd/http.go +++ b/cmd/circologd/http.go @@ -18,7 +18,7 @@ func setupHTTP(hub circolog.Hub) { } func parseParameterL(r *http.Request) (int, error) { - var requestMessageLen int + var requestMessageLen int = -1 var err error if reqL, ok := r.Form["l"]; ok { if len(reqL) == 1 { @@ -38,7 +38,6 @@ 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) @@ -83,12 +82,20 @@ func getWSHandler(hub circolog.Hub) http.HandlerFunc { WriteBufferSize: 1024, } return func(w http.ResponseWriter, r *http.Request) { + opts, err := parseParameters(w, r) + if err != nil { + log.Println("Error on request parameter \"l\":", err) + w.WriteHeader(400) + return + } + opts.Nofollow = false conn, err := upgrader.Upgrade(w, r, nil) if err != nil { return } - client := circolog.Client{Messages: make(chan format.LogParts, 20), - Options: circolog.ClientOptions{BacklogLength: -1}, + client := circolog.Client{ + Messages: make(chan format.LogParts, 20), + Options: opts, } hub.Register <- client