From 1c000e8c5582890e328e180d68f338ad4a255e4b Mon Sep 17 00:00:00 2001 From: "vforfreedom96@gmail.com" Date: Sun, 2 Jun 2024 02:10:33 +0800 Subject: [PATCH] feat:websocket add sync.Mutex lock --- gateway/websocket.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gateway/websocket.go b/gateway/websocket.go index 2120524..25254b3 100644 --- a/gateway/websocket.go +++ b/gateway/websocket.go @@ -4,6 +4,7 @@ import ( "bytes" "io" "net/http" + "sync" "github.com/gorilla/websocket" ) @@ -21,6 +22,7 @@ type websocketForwarder struct { http.ResponseWriter websocket *websocket.Conn responseType int + mux sync.Mutex } func NewWebsocketForwarder(w http.ResponseWriter, req *http.Request, responseType int) (WebsocketForwarder, error) { @@ -32,12 +34,14 @@ func NewWebsocketForwarder(w http.ResponseWriter, req *http.Request, responseTyp if err != nil { return nil, err } - return &websocketForwarder{w, conn, responseType}, nil + return &websocketForwarder{w, conn, responseType,sync.Mutex{}}, nil } func (w *websocketForwarder) Flush() { } func (w *websocketForwarder) Close() error { // w.websocket.NextWriter() + w.mux.Lock() + defer w.mux.Unlock() return w.websocket.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) } func (w *websocketForwarder) CloseConn() error {