Close web socket connections in all paths. (#4747)
* Close web socket connections in all paths. There was a leak of WebSocket pingWorker if the initial response write errored as it did not close the WebSocket connection. * graceful close
This commit is contained in:
@@ -211,8 +211,10 @@ func (s *AgentService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
if s.signalMessageSizeLimit > 0 {
|
if s.signalMessageSizeLimit > 0 {
|
||||||
conn.SetReadLimit(s.signalMessageSizeLimit)
|
conn.SetReadLimit(s.signalMessageSizeLimit)
|
||||||
}
|
}
|
||||||
s.HandleConnection(r.Context(), NewWSSignalConnection(conn, s.signalMessageSizeLimit), registration)
|
sigConn := NewWSSignalConnection(conn, s.signalMessageSizeLimit)
|
||||||
conn.Close()
|
defer sigConn.Close()
|
||||||
|
|
||||||
|
s.HandleConnection(r.Context(), sigConn, registration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -467,6 +467,7 @@ func (s *RTCService) serve(w http.ResponseWriter, r *http.Request, needsJoinRequ
|
|||||||
|
|
||||||
// websocket established
|
// websocket established
|
||||||
sigConn := NewWSSignalConnection(conn, s.limits.SignalMessageSizeLimit)
|
sigConn := NewWSSignalConnection(conn, s.limits.SignalMessageSizeLimit)
|
||||||
|
defer sigConn.CloseWithReason("")
|
||||||
pLogger.Debugw("sending initial response", "response", logger.Proto(initialResponse))
|
pLogger.Debugw("sending initial response", "response", logger.Proto(initialResponse))
|
||||||
count, err := sigConn.WriteResponse(initialResponse)
|
count, err := sigConn.WriteResponse(initialResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -495,9 +496,7 @@ func (s *RTCService) serve(w http.ResponseWriter, r *http.Request, needsJoinRequ
|
|||||||
defer func() {
|
defer func() {
|
||||||
// when the source is terminated, this means Participant.Close had been called and RTC connection is done
|
// when the source is terminated, this means Participant.Close had been called and RTC connection is done
|
||||||
// we would terminate the signal connection as well
|
// we would terminate the signal connection as well
|
||||||
closeMsg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
|
sigConn.CloseWithReason("")
|
||||||
_ = conn.WriteControl(websocket.CloseMessage, closeMsg, time.Now().Add(time.Second))
|
|
||||||
_ = conn.Close()
|
|
||||||
}()
|
}()
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := rtc.Recover(pLogger); r != nil {
|
if r := rtc.Recover(pLogger); r != nil {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/frostbyte73/core"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
|
|
||||||
@@ -45,6 +46,8 @@ type WSSignalConnection struct {
|
|||||||
|
|
||||||
// maximum size (in bytes) of a single decompressed message; 0 disables the limit
|
// maximum size (in bytes) of a single decompressed message; 0 disables the limit
|
||||||
messageSizeLimit int64
|
messageSizeLimit int64
|
||||||
|
|
||||||
|
closed core.Fuse
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWSSignalConnection(conn types.WebsocketClient, messageSizeLimit int64) *WSSignalConnection {
|
func NewWSSignalConnection(conn types.WebsocketClient, messageSizeLimit int64) *WSSignalConnection {
|
||||||
@@ -88,10 +91,14 @@ func (c *WSSignalConnection) readMessage() (int, []byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *WSSignalConnection) Close() error {
|
func (c *WSSignalConnection) Close() error {
|
||||||
|
c.closed.Break()
|
||||||
|
|
||||||
return c.conn.Close()
|
return c.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WSSignalConnection) CloseWithReason(reason string) error {
|
func (c *WSSignalConnection) CloseWithReason(reason string) error {
|
||||||
|
c.closed.Break()
|
||||||
|
|
||||||
msg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, reason)
|
msg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, reason)
|
||||||
_ = c.conn.WriteControl(websocket.CloseMessage, msg, time.Now().Add(closeWriteTimeout))
|
_ = c.conn.WriteControl(websocket.CloseMessage, msg, time.Now().Add(closeWriteTimeout))
|
||||||
return c.conn.Close()
|
return c.conn.Close()
|
||||||
@@ -213,10 +220,16 @@ func (c *WSSignalConnection) pingWorker() {
|
|||||||
ticker := time.NewTicker(pingFrequency)
|
ticker := time.NewTicker(pingFrequency)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for range ticker.C {
|
for {
|
||||||
err := c.conn.WriteControl(websocket.PingMessage, []byte(""), time.Now().Add(pingTimeout))
|
select {
|
||||||
if err != nil {
|
case <-c.closed.Watch():
|
||||||
return
|
return
|
||||||
|
|
||||||
|
case <-ticker.C:
|
||||||
|
err := c.conn.WriteControl(websocket.PingMessage, []byte(""), time.Now().Add(pingTimeout))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user