From cbee89773917ebc00e3a967e41c4c6907a091c20 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Mon, 26 Oct 2020 02:56:15 +0000 Subject: [PATCH] close the client tcp socket on the server --- internal/server/server.go | 13 ++++++++++--- internal/wire/client.go | 10 +++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/internal/server/server.go b/internal/server/server.go index b8b04a8..d6e13ba 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -18,9 +18,8 @@ import ( type Server struct { *blammo.Log - Host string - Port int - + Host string + Port int lastSessionID int } @@ -50,6 +49,7 @@ func (s *Server) Start() error { return fmt.Errorf("server failed to start a listener: %w", err) } s.Log.Info("listening for TCP traffic on %q", addr) + go s.runHTTPServer(lis) return nil @@ -71,6 +71,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } + defer func() { + s.Info("closing connection") + if err := conn.Close(); err != nil { + s.Error("error closing connection: %v", err) + } + }() + ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/internal/wire/client.go b/internal/wire/client.go index e46d778..78967c0 100644 --- a/internal/wire/client.go +++ b/internal/wire/client.go @@ -112,6 +112,13 @@ func (c *Client) readLoop(notifications chan<- Response) { } } c.done <- false + + c.Info("closing connection") + if err := c.conn.Close(); err != nil { + c.Error("error on connection close: %v", err) + } else { + c.Info("closed connection cleanly") + } } func (c *Client) writeLoop() { @@ -158,6 +165,8 @@ func (c *Client) writeLoop() { close(p.done) case shouldClose := <-c.done: + c.Info("write loop sees done signal: %t", shouldClose) + if shouldClose { c.Info("sending close frame") msg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "") @@ -166,7 +175,6 @@ func (c *Client) writeLoop() { } else { c.Info("sent close frame") } - c.conn = nil } return }