From 76799714b377bdb27bfd087ca30ca16ec0de3849 Mon Sep 17 00:00:00 2001 From: Tom Bevan Date: Wed, 30 Jan 2019 18:22:28 +0000 Subject: [PATCH] =?UTF-8?q?corrected=20bad=20stream=20writes=20and=20incre?= =?UTF-8?q?ased=20logging=C2=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auth.go | 3 ++- build.go | 3 +-- handler.go | 13 +++++++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/auth.go b/auth.go index 52e9ad5..9df1e6a 100644 --- a/auth.go +++ b/auth.go @@ -8,6 +8,7 @@ import ( "encoding/json" "errors" "fmt" + "log" "net/http" "github.com/dgrijalva/jwt-go" @@ -24,7 +25,7 @@ type Session struct { } func unauthorized(w http.ResponseWriter) error { - http.Error(w, "Unauthorized", http.StatusUnauthorized) + log.Println("Unauthorized") return errors.New("Unauthorized") } diff --git a/build.go b/build.go index 618e19b..6d558c1 100644 --- a/build.go +++ b/build.go @@ -50,9 +50,8 @@ func processBuild(msg *nats.Msg) { bc.Publish(id, data) go func(bc *broadcast.Server) { // Wait for any late connecting clients before closing stream - time.Sleep(1 * time.Second) + time.Sleep(broadcast.DefaultMaxInactivity) log.Println("Closing stream: ", id) - bc.RemoveStream(id) }(bc) } } diff --git a/handler.go b/handler.go index 10997b1..4e58817 100644 --- a/handler.go +++ b/handler.go @@ -7,6 +7,7 @@ package main import ( // "errors" + "log" "net/http" "github.com/gorilla/websocket" @@ -27,6 +28,7 @@ func handler(w http.ResponseWriter, r *http.Request) { } var authorized bool + var areq *Session var ch chan *broadcast.Event var sub *broadcast.Subscriber @@ -40,7 +42,7 @@ func handler(w http.ResponseWriter, r *http.Request) { for { if !authorized { - areq, err := authenticate(w, c) + areq, err = authenticate(w, c) if err != nil { return } @@ -57,8 +59,10 @@ func handler(w http.ResponseWriter, r *http.Request) { return } + log.Println("Sending Message to ", areq.Stream) err := c.WriteMessage(websocket.TextMessage, msg.Data) if err != nil { + log.Println("failed to write to connection") _ = internalerror(w) return } @@ -87,20 +91,21 @@ func register(w http.ResponseWriter, s *Session) (*broadcast.Subscriber, chan *b } func upgradefail(w http.ResponseWriter) { + log.Println("Unable to upgrade to websocket connection") http.Error(w, "Unable to upgrade to websocket connection", http.StatusBadRequest) } func badrequest(w http.ResponseWriter) error { - http.Error(w, "Could not process sent data", http.StatusBadRequest) + log.Println("Could not process sent data") return errors.New("Could not process sent data") } func badstream(w http.ResponseWriter) error { - http.Error(w, "Please specify a valid stream", http.StatusBadRequest) + log.Println("Please specify a valid stream") return errors.New("Please specify a valid stream") } func internalerror(w http.ResponseWriter) error { - http.Error(w, "Internal server error", http.StatusInternalServerError) + log.Println("Internal server error") return errors.New("Internal server error") }