Improve websocket logging (#24259)
After reliable websockets were introduced, we would keep pushing to a webConn even after the client has disconnected. This would lead to the websocket.slow/full and eventually the disconnect messages which unnecessarily confuse the admin. We don't log them if the connection is inactive. We use the active field and convert it to an atomic to use more widely. ```release-note NONE ```
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
2ed0c6495b
Коммит
0e75982f2b
@@ -383,7 +383,7 @@ func (h *Hub) Start() {
|
||||
conns := connIndex.ForUser(webSessionMessage.userID)
|
||||
var isRegistered bool
|
||||
for _, conn := range conns {
|
||||
if !conn.active {
|
||||
if !conn.active.Load() {
|
||||
continue
|
||||
}
|
||||
if conn.GetSessionToken() == webSessionMessage.sessionToken {
|
||||
@@ -411,7 +411,7 @@ func (h *Hub) Start() {
|
||||
// Mark the current one as active.
|
||||
// There is no need to check if it was inactive or not,
|
||||
// we will anyways need to make it active.
|
||||
webConn.active = true
|
||||
webConn.active.Store(true)
|
||||
|
||||
connIndex.Add(webConn)
|
||||
atomic.StoreInt64(&h.connectionCount, int64(connIndex.AllActive()))
|
||||
@@ -426,7 +426,7 @@ func (h *Hub) Start() {
|
||||
case webConn := <-h.unregister:
|
||||
// If already removed (via queue full), then removing again becomes a noop.
|
||||
// But if not removed, mark inactive.
|
||||
webConn.active = false
|
||||
webConn.active.Store(false)
|
||||
|
||||
atomic.StoreInt64(&h.connectionCount, int64(connIndex.AllActive()))
|
||||
|
||||
@@ -444,7 +444,7 @@ func (h *Hub) Start() {
|
||||
}
|
||||
var latestActivity int64
|
||||
for _, conn := range conns {
|
||||
if !conn.active {
|
||||
if !conn.active.Load() {
|
||||
continue
|
||||
}
|
||||
if conn.lastUserActivityAt > latestActivity {
|
||||
@@ -464,7 +464,7 @@ func (h *Hub) Start() {
|
||||
}
|
||||
case activity := <-h.activity:
|
||||
for _, webConn := range connIndex.ForUser(activity.userID) {
|
||||
if !webConn.active {
|
||||
if !webConn.active.Load() {
|
||||
continue
|
||||
}
|
||||
if webConn.GetSessionToken() == activity.sessionToken {
|
||||
@@ -478,9 +478,12 @@ func (h *Hub) Start() {
|
||||
select {
|
||||
case directMsg.conn.send <- directMsg.msg:
|
||||
default:
|
||||
mlog.Error("webhub.broadcast: cannot send, closing websocket for user",
|
||||
mlog.String("user_id", directMsg.conn.UserId),
|
||||
mlog.String("conn_id", directMsg.conn.GetConnectionID()))
|
||||
// Don't log the warning if it's an inactive connection.
|
||||
if directMsg.conn.active.Load() {
|
||||
mlog.Error("webhub.broadcast: cannot send, closing websocket for user",
|
||||
mlog.String("user_id", directMsg.conn.UserId),
|
||||
mlog.String("conn_id", directMsg.conn.GetConnectionID()))
|
||||
}
|
||||
close(directMsg.conn.send)
|
||||
connIndex.Remove(directMsg.conn)
|
||||
}
|
||||
@@ -497,9 +500,12 @@ func (h *Hub) Start() {
|
||||
select {
|
||||
case webConn.send <- msg:
|
||||
default:
|
||||
mlog.Error("webhub.broadcast: cannot send, closing websocket for user",
|
||||
mlog.String("user_id", webConn.UserId),
|
||||
mlog.String("conn_id", webConn.GetConnectionID()))
|
||||
// Don't log the warning if it's an inactive connection.
|
||||
if webConn.active.Load() {
|
||||
mlog.Error("webhub.broadcast: cannot send, closing websocket for user",
|
||||
mlog.String("user_id", webConn.UserId),
|
||||
mlog.String("conn_id", webConn.GetConnectionID()))
|
||||
}
|
||||
close(webConn.send)
|
||||
connIndex.Remove(webConn)
|
||||
}
|
||||
@@ -639,7 +645,7 @@ func (i *hubConnectionIndex) RemoveInactiveByConnectionID(userID, connectionID s
|
||||
return nil
|
||||
}
|
||||
for _, conn := range i.ForUser(userID) {
|
||||
if conn.GetConnectionID() == connectionID && !conn.active {
|
||||
if conn.GetConnectionID() == connectionID && !conn.active.Load() {
|
||||
i.Remove(conn)
|
||||
return conn
|
||||
}
|
||||
@@ -652,7 +658,7 @@ func (i *hubConnectionIndex) RemoveInactiveByConnectionID(userID, connectionID s
|
||||
func (i *hubConnectionIndex) RemoveInactiveConnections() {
|
||||
now := model.GetMillis()
|
||||
for conn := range i.byConnection {
|
||||
if !conn.active && now-conn.lastUserActivityAt > i.staleThreshold.Milliseconds() {
|
||||
if !conn.active.Load() && now-conn.lastUserActivityAt > i.staleThreshold.Milliseconds() {
|
||||
i.Remove(conn)
|
||||
}
|
||||
}
|
||||
@@ -664,7 +670,7 @@ func (i *hubConnectionIndex) RemoveInactiveConnections() {
|
||||
func (i *hubConnectionIndex) AllActive() int {
|
||||
cnt := 0
|
||||
for conn := range i.byConnection {
|
||||
if conn.active {
|
||||
if conn.active.Load() {
|
||||
cnt++
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user