MM-39612: Make acquiring and removing connections atomic (#18982)
* MM-39612: Make acquiring and removing connections atomic The reconnect phase of a websocket was split into two parts: one where we check if a connection with a given connectionID exists or not. And second, where we remove that connection and insert the new connection again in the index. This would lead to a race where it would be possible for 2 concurrent requests for the same connectionID to go through which would lead to separate goroutines working on the same dead queue. We simplify this by removing the connection from the index in the check connection stage itself. And then just add that during register phase. And to distinguish between a fresh and an old connection, we add a new field called reuseCount. While here, we also cleanup some old comments and add more in some places. https://mattermost.atlassian.net/browse/MM-39612 ```release-note NONE ``` * remove unused method ```release-note NONE ``` * race test ```release-note NONE ```
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
e8591537e0
Коммит
b7a7d8e7b6
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -199,6 +200,44 @@ func TestWebsocketOriginSecurity(t *testing.T) {
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowCorsFrom = "" })
|
||||
}
|
||||
|
||||
func TestWebSocketReconnectRace(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.ServiceSettings.EnableReliableWebSockets = true
|
||||
})
|
||||
|
||||
WebSocketClient, err := th.CreateWebSocketClient()
|
||||
require.NoError(t, err)
|
||||
defer WebSocketClient.Close()
|
||||
WebSocketClient.Listen()
|
||||
|
||||
ev := <-WebSocketClient.EventChannel
|
||||
require.Equal(t, model.WebsocketEventHello, ev.EventType())
|
||||
evData := ev.GetData()
|
||||
connID := evData["connection_id"].(string)
|
||||
seq := int(ev.GetSequence())
|
||||
|
||||
var wg sync.WaitGroup
|
||||
n := 10
|
||||
wg.Add(n)
|
||||
|
||||
WebSocketClient.Close()
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
ws, err := th.CreateReliableWebSocketClient(connID, seq+1)
|
||||
require.NoError(t, err)
|
||||
defer ws.Close()
|
||||
ws.Listen()
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestWebSocketStatuses(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
Ссылка в новой задаче
Block a user