From 8807c8fc9bd97c7aa9b0aed5c69453a120c64680 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 12 May 2019 06:18:06 -0700 Subject: [PATCH] clean up the websocket Dial code to correctly restart Signed-off-by: Jeff Carr --- gorilla.go | 84 +++++++++++++++++++++++++++++++++++------------------- main.go | 6 ++-- 2 files changed, 59 insertions(+), 31 deletions(-) diff --git a/gorilla.go b/gorilla.go index ce1b362..f80e23d 100644 --- a/gorilla.go +++ b/gorilla.go @@ -4,7 +4,7 @@ package main -import "flag" +// import "flag" import "log" import "net/url" import "os" @@ -18,8 +18,7 @@ import pb "git.wit.com/wit/witProtobuf" var gorillaConn *websocket.Conn -func readGorillaConn(done chan struct{}, conn *websocket.Conn) { - defer close(done) +func readGorillaConn(conn *websocket.Conn) { for { mytype, message, err := conn.ReadMessage() if err != nil { @@ -46,7 +45,6 @@ func gorillaSendProtobuf() { if (gorillaConn == nil) { log.Println("gorillaSendProtobuf() gorillaConn == nil") log.Println("Need to re-open connection here") - // go gorillaDial() return } msg := pb.CreateSampleEvent() @@ -60,30 +58,62 @@ func gorillaSendProtobuf() { } } -func gorillaDial() { - var addr = flag.String("addr", "v000185.testing.com.customers.wprod.wit.com:9000", "http service address") +func closeGorillaConn() { + if gorillaConn == nil { + log.Println("gorillaConn already was nil") + return + } + + // Cleanly close the connection by sending a close message and then + // waiting (with timeout) for the server to close the connection. + err := gorillaConn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) + if err != nil { + log.Println("write close:", err) + return + } + + // this must be called or set to be called with defer + gorillaConn.Close() + gorillaConn = nil +} + +func gorillaDial(hostname string) { + // var addr = flag.String("addr", "v000185.testing.com.customers.wprod.wit.com:9000", "http service address") + + for { + // u := url.URL{Scheme: "ws", Host: *addr, Path: "/event"} + u := url.URL{Scheme: "ws", Host: hostname, Path: "/event"} + log.Printf("connecting to %s", u.String()) + + conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil) + if err != nil { + log.Println("gorilla Dial failed", err) + } else { + gorillaConn = conn + + // handle inbound messages on the channel + readGorillaConn(conn) + closeGorillaConn() + } + + time.Sleep(time.Second * 5) // try every 5 seconds + } +} + +// +// this is a facinating code snippet that I wanted to leave here because it is +// so interesting. Complements to the gorilla websocket example developers +// +func beautifulAndFacinatingChannel() { + done := make(chan struct{}) interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, os.Interrupt) - u := url.URL{Scheme: "ws", Host: *addr, Path: "/event"} - log.Printf("connecting to %s", u.String()) - - conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil) - if err != nil { - log.Println("gorilla Dial failed", err) - return - } - defer conn.Close() - gorillaConn = conn - - done := make(chan struct{}) - - // handle inbound messages on the channel - go readGorillaConn(done, conn) - ticker := time.NewTicker(time.Second * 1) + defer ticker.Stop() + defer close(done) for { select { @@ -94,13 +124,9 @@ func gorillaDial() { case <-interrupt: log.Println("interrupt") - // Cleanly close the connection by sending a close message and then - // waiting (with timeout) for the server to close the connection. - err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) - if err != nil { - log.Println("write close:", err) - return - } + gorillaSendProtobuf() + + // not sure what this does. nothing right? select { case <-done: case <-time.After(time.Second): diff --git a/main.go b/main.go index 2ad1526..d51a978 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,9 @@ import _ "github.com/andlabs/ui/winmanifest" // always sorted slice (new project) // https://github.com/yaa110/sslice +// several smart slice functions (new project. April 2019) +// https://github.com/elliotchance/pie + // Exit and write out the config // (in yaml and json I guess.) // switch to json once it can be made human readable @@ -62,8 +65,7 @@ func main() { // setups up a dnssecsocket() go retrySocket() - time.Sleep(3 * 1000 * 1000 * 1000) - go gorillaDial() + go gorillaDial("v000185.testing.com.customers.wprod.wit.com:9000") // make this the main loop in an attempt to figure out the crashes // do not change this until the GUI is stable