From 26dca24f77e779712ed42170b317a6ef4a13368e Mon Sep 17 00:00:00 2001 From: Hemanth Krishna Date: Thu, 6 Jul 2023 01:50:34 +0530 Subject: [PATCH] fix: formatter websocket ctx Signed-off-by: Hemanth Krishna --- formatter.go | 8 ++++++++ rust_formatter.go | 8 ++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/formatter.go b/formatter.go index cec342b..9d74cfa 100644 --- a/formatter.go +++ b/formatter.go @@ -15,8 +15,16 @@ type FormatResponse struct { func fileFormatterController() func(c *websocket.Conn) { return func(c *websocket.Conn) { log.Println(c.RemoteAddr(), "Connection opened for format request!") + c.SetCloseHandler(func(code int, text string) error { + log.Printf("WebSocket connection for format closed with code %d and text: %s", code, text) + return nil + }) for { mt, msg, err := c.ReadMessage() + if mt == websocket.CloseMessage|websocket.CloseGoingAway|websocket.CloseAbnormalClosure { + log.Println(c.RemoteAddr(), "Closing formatter websocket") + return + } log.Println(c.RemoteAddr(), "Received format request!") if err != nil { diff --git a/rust_formatter.go b/rust_formatter.go index b4357fd..b5aa8e4 100644 --- a/rust_formatter.go +++ b/rust_formatter.go @@ -2,18 +2,14 @@ package main import ( "bytes" + "context" "fmt" "os/exec" "strings" - "sync" ) func runFormatter(inputCode string) ([]byte, error) { - var mutex sync.Mutex - mutex.Lock() - defer mutex.Unlock() - - cmd := exec.Command("rustfmt") + cmd := exec.CommandContext(context.Background(), "rustfmt") cmd.Stdin = strings.NewReader(inputCode) var stdOut bytes.Buffer cmd.Stdout = &stdOut