fix: wsConnMap mutex lock

This commit is contained in:
lilong.129
2024-08-20 20:48:19 +08:00
parent 69b9902176
commit 55f56c9bcc
+11 -6
View File
@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"net/http" "net/http"
"sync"
"testing" "testing"
"time" "time"
"unsafe" "unsafe"
@@ -17,11 +18,18 @@ import (
) )
var ( var (
wsMutex sync.Mutex
wsConnMap map[string]*websocket.Conn // save all websocket connections wsConnMap map[string]*websocket.Conn // save all websocket connections
pongResponseChan chan string // channel used to receive pong response message pongResponseChan chan string // channel used to receive pong response message
closeResponseChan chan *wsCloseRespObject // channel used to receive close response message closeResponseChan chan *wsCloseRespObject // channel used to receive close response message
) )
func init() {
wsConnMap = make(map[string]*websocket.Conn)
pongResponseChan = make(chan string, 1)
closeResponseChan = make(chan *wsCloseRespObject, 1)
}
const ( const (
wsOpen ActionType = "open" wsOpen ActionType = "open"
wsPing ActionType = "ping" wsPing ActionType = "ping"
@@ -426,12 +434,6 @@ func runStepWebSocket(r *SessionRunner, step *TStep) (stepResult *StepResult, er
} }
func getWsClient(url string) *websocket.Conn { func getWsClient(url string) *websocket.Conn {
if wsConnMap == nil {
wsConnMap = make(map[string]*websocket.Conn)
pongResponseChan = make(chan string, 1)
closeResponseChan = make(chan *wsCloseRespObject, 1)
}
if client, ok := wsConnMap[url]; ok { if client, ok := wsConnMap[url]; ok {
return client return client
} }
@@ -500,7 +502,10 @@ func openWithTimeout(urlStr string, requestHeader http.Header, r *SessionRunner,
return nil return nil
}) })
wsMutex.Lock()
wsConnMap[urlStr] = conn wsConnMap[urlStr] = conn
wsMutex.Unlock()
openResponseChan <- resp openResponseChan <- resp
}() }()