Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy: Add timeouts to requests to Janus and cancel if session is closed. #847

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ func NewClient(ctx context.Context, conn *websocket.Conn, remoteAddress string,
}

client := &Client{
ctx: ctx,
agent: agent,
logRTT: true,
}
client.SetConn(conn, remoteAddress, handler)
client.SetConn(ctx, conn, remoteAddress, handler)
return client, nil
}

func (c *Client) SetConn(conn *websocket.Conn, remoteAddress string, handler ClientHandler) {
func (c *Client) SetConn(ctx context.Context, conn *websocket.Conn, remoteAddress string, handler ClientHandler) {
c.ctx = ctx
c.conn = conn
c.addr = remoteAddress
c.SetHandler(handler)
Expand Down
5 changes: 3 additions & 2 deletions proxy/proxy_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package main

import (
"context"
"sync/atomic"
"time"

Expand All @@ -37,11 +38,11 @@ type ProxyClient struct {
session atomic.Pointer[ProxySession]
}

func NewProxyClient(proxy *ProxyServer, conn *websocket.Conn, addr string) (*ProxyClient, error) {
func NewProxyClient(ctx context.Context, proxy *ProxyServer, conn *websocket.Conn, addr string) (*ProxyClient, error) {
client := &ProxyClient{
proxy: proxy,
}
client.SetConn(conn, addr, client)
client.SetConn(ctx, conn, addr, client)
return client, nil
}

Expand Down
56 changes: 46 additions & 10 deletions proxy/proxy_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ const (
initialMcuRetry = time.Second
maxMcuRetry = time.Second * 16

// MCU requests will be cancelled if they take too long.
defaultMcuTimeoutSeconds = 10

updateLoadInterval = time.Second
expireSessionsInterval = 10 * time.Second

Expand Down Expand Up @@ -103,6 +106,7 @@ type ProxyServer struct {
welcomeMessage string
welcomeMsg *signaling.WelcomeServerMessage
config *goconf.ConfigFile
mcuTimeout time.Duration

url string
mcu signaling.Mcu
Expand Down Expand Up @@ -319,6 +323,12 @@ func NewProxyServer(r *mux.Router, version string, config *goconf.ConfigFile) (*

maxIncoming, maxOutgoing := getTargetBandwidths(config)

mcuTimeoutSeconds, _ := config.GetInt("mcu", "timeout")
if mcuTimeoutSeconds <= 0 {
mcuTimeoutSeconds = defaultMcuTimeoutSeconds
}
mcuTimeout := time.Duration(mcuTimeoutSeconds) * time.Second

result := &ProxyServer{
version: version,
country: country,
Expand All @@ -328,7 +338,8 @@ func NewProxyServer(r *mux.Router, version string, config *goconf.ConfigFile) (*
Country: country,
Features: defaultProxyFeatures,
},
config: config,
config: config,
mcuTimeout: mcuTimeout,

shutdownChannel: make(chan struct{}),

Expand Down Expand Up @@ -634,14 +645,14 @@ func (s *ProxyServer) proxyHandler(w http.ResponseWriter, r *http.Request) {
return
}

client, err := NewProxyClient(s, conn, addr)
client, err := NewProxyClient(r.Context(), s, conn, addr)
if err != nil {
log.Printf("Could not create client for %s: %s", addr, err)
return
}

go client.WritePump()
go client.ReadPump()
client.ReadPump()
}

func (s *ProxyServer) clientClosed(client *signaling.Client) {
Expand Down Expand Up @@ -789,14 +800,16 @@ func (s *ProxyServer) processMessage(client *ProxyClient, data []byte) {
return
}

ctx := context.WithValue(context.Background(), ContextKeySession, session)
ctx := context.WithValue(session.Context(), ContextKeySession, session)
session.MarkUsed()

switch message.Type {
case "command":
s.processCommand(ctx, client, session, &message)
case "payload":
s.processPayload(ctx, client, session, &message)
case "bye":
s.processBye(ctx, client, session, &message)
default:
session.sendMessage(message.NewErrorServerMessage(UnsupportedMessage))
}
Expand Down Expand Up @@ -873,8 +886,11 @@ func (s *ProxyServer) processCommand(ctx context.Context, client *ProxyClient, s
return
}

ctx2, cancel := context.WithTimeout(ctx, s.mcuTimeout)
defer cancel()

id := uuid.New().String()
publisher, err := s.mcu.NewPublisher(ctx, session, id, cmd.Sid, cmd.StreamType, cmd.Bitrate, cmd.MediaTypes, &emptyInitiator{})
publisher, err := s.mcu.NewPublisher(ctx2, session, id, cmd.Sid, cmd.StreamType, cmd.Bitrate, cmd.MediaTypes, &emptyInitiator{})
if err == context.DeadlineExceeded {
log.Printf("Timeout while creating %s publisher %s for %s", cmd.StreamType, id, session.PublicId())
session.sendMessage(message.NewErrorServerMessage(TimeoutCreatingPublisher))
Expand Down Expand Up @@ -977,7 +993,10 @@ func (s *ProxyServer) processCommand(ctx context.Context, client *ProxyClient, s

log.Printf("Created remote %s subscriber %s as %s for %s on %s", cmd.StreamType, subscriber.Id(), id, session.PublicId(), cmd.RemoteUrl)
} else {
subscriber, err = s.mcu.NewSubscriber(ctx, session, publisherId, cmd.StreamType, &emptyInitiator{})
ctx2, cancel := context.WithTimeout(ctx, s.mcuTimeout)
defer cancel()

subscriber, err = s.mcu.NewSubscriber(ctx2, session, publisherId, cmd.StreamType, &emptyInitiator{})
if err != nil {
handleCreateError(err)
return
Expand Down Expand Up @@ -1083,21 +1102,30 @@ func (s *ProxyServer) processCommand(ctx context.Context, client *ProxyClient, s
return
}

if err := publisher.PublishRemote(ctx, session.PublicId(), cmd.Hostname, cmd.Port, cmd.RtcpPort); err != nil {
ctx2, cancel := context.WithTimeout(ctx, s.mcuTimeout)
defer cancel()

if err := publisher.PublishRemote(ctx2, session.PublicId(), cmd.Hostname, cmd.Port, cmd.RtcpPort); err != nil {
var je *janus.ErrorMsg
if !errors.As(err, &je) || je.Err.Code != signaling.JANUS_VIDEOROOM_ERROR_ID_EXISTS {
log.Printf("Error publishing %s %s to remote %s (port=%d, rtcpPort=%d): %s", publisher.StreamType(), cmd.ClientId, cmd.Hostname, cmd.Port, cmd.RtcpPort, err)
session.sendMessage(message.NewWrappedErrorServerMessage(err))
return
}

if err := publisher.UnpublishRemote(ctx, session.PublicId()); err != nil {
ctx2, cancel = context.WithTimeout(ctx, s.mcuTimeout)
defer cancel()

if err := publisher.UnpublishRemote(ctx2, session.PublicId()); err != nil {
log.Printf("Error unpublishing old %s %s to remote %s (port=%d, rtcpPort=%d): %s", publisher.StreamType(), cmd.ClientId, cmd.Hostname, cmd.Port, cmd.RtcpPort, err)
session.sendMessage(message.NewWrappedErrorServerMessage(err))
return
}

if err := publisher.PublishRemote(ctx, session.PublicId(), cmd.Hostname, cmd.Port, cmd.RtcpPort); err != nil {
ctx2, cancel = context.WithTimeout(ctx, s.mcuTimeout)
defer cancel()

if err := publisher.PublishRemote(ctx2, session.PublicId(), cmd.Hostname, cmd.Port, cmd.RtcpPort); err != nil {
log.Printf("Error publishing %s %s to remote %s (port=%d, rtcpPort=%d): %s", publisher.StreamType(), cmd.ClientId, cmd.Hostname, cmd.Port, cmd.RtcpPort, err)
session.sendMessage(message.NewWrappedErrorServerMessage(err))
return
Expand Down Expand Up @@ -1202,7 +1230,10 @@ func (s *ProxyServer) processPayload(ctx context.Context, client *ProxyClient, s
return
}

mcuClient.SendMessage(ctx, nil, mcuData, func(err error, response map[string]interface{}) {
ctx2, cancel := context.WithTimeout(ctx, s.mcuTimeout)
defer cancel()

mcuClient.SendMessage(ctx2, nil, mcuData, func(err error, response map[string]interface{}) {
var responseMsg *signaling.ProxyServerMessage
if err != nil {
log.Printf("Error sending %+v to %s client %s: %s", mcuData, mcuClient.StreamType(), payload.ClientId, err)
Expand All @@ -1223,6 +1254,11 @@ func (s *ProxyServer) processPayload(ctx context.Context, client *ProxyClient, s
})
}

func (s *ProxyServer) processBye(ctx context.Context, client *ProxyClient, session *ProxySession, message *signaling.ProxyClientMessage) {
log.Printf("Closing session %s", session.PublicId())
s.DeleteSession(session.Sid())
}

func (s *ProxyServer) parseToken(tokenValue string) (*signaling.TokenClaims, string, error) {
reason := "auth-failed"
token, err := jwt.ParseWithClaims(tokenValue, &signaling.TokenClaims{}, func(token *jwt.Token) (interface{}, error) {
Expand Down
Loading
Loading