mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-09 09:27:26 +08:00
fix: wsConnMap mutex lock
This commit is contained in:
+11
-6
@@ -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
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user