fix: wsConnMap mutex lock

This commit is contained in:
lilong.129
2024-08-20 20:48:19 +08:00
parent 69b9902176
commit 55f56c9bcc

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"net/http"
"sync"
"testing"
"time"
"unsafe"
@@ -17,11 +18,18 @@ import (
)
var (
wsMutex sync.Mutex
wsConnMap map[string]*websocket.Conn // save all websocket connections
pongResponseChan chan string // channel used to receive pong 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 (
wsOpen ActionType = "open"
wsPing ActionType = "ping"
@@ -426,12 +434,6 @@ func runStepWebSocket(r *SessionRunner, step *TStep) (stepResult *StepResult, er
}
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 {
return client
}
@@ -500,7 +502,10 @@ func openWithTimeout(urlStr string, requestHeader http.Header, r *SessionRunner,
return nil
})
wsMutex.Lock()
wsConnMap[urlStr] = conn
wsMutex.Unlock()
openResponseChan <- resp
}()