Fix MessageChannel leaks (#646)

This commit is contained in:
David Zhao
2022-04-22 10:53:20 -07:00
committed by GitHub
parent 2e182afb61
commit b7d22c4f34
8 changed files with 161 additions and 117 deletions
+53 -3
View File
@@ -2,6 +2,7 @@ package routing
import (
"context"
"encoding/json"
"github.com/go-redis/redis/v8"
"google.golang.org/protobuf/proto"
@@ -19,13 +20,13 @@ import (
type MessageSink interface {
WriteMessage(msg proto.Message) error
Close()
OnClose(f func())
}
//counterfeiter:generate . MessageSource
type MessageSource interface {
// ReadChan exposes a one way channel to make it easier to use with select
ReadChan() <-chan proto.Message
Close()
}
type ParticipantInit struct {
@@ -39,8 +40,20 @@ type ParticipantInit struct {
AdaptiveStream bool
}
type NewParticipantCallback func(ctx context.Context, roomName livekit.RoomName, pi ParticipantInit, requestSource MessageSource, responseSink MessageSink)
type RTCMessageCallback func(ctx context.Context, roomName livekit.RoomName, identity livekit.ParticipantIdentity, msg *livekit.RTCNodeMessage)
type NewParticipantCallback func(
ctx context.Context,
roomName livekit.RoomName,
pi ParticipantInit,
requestSource MessageSource,
responseSink MessageSink,
) error
type RTCMessageCallback func(
ctx context.Context,
roomName livekit.RoomName,
identity livekit.ParticipantIdentity,
msg *livekit.RTCNodeMessage,
)
// Router allows multiple nodes to coordinate the participant session
//counterfeiter:generate . Router
@@ -88,3 +101,40 @@ func CreateRouter(rc *redis.Client, node LocalNode) Router {
logger.Infow("using single-node routing")
return NewLocalRouter(node)
}
func (pi *ParticipantInit) ToStartSession(roomName livekit.RoomName, connectionID livekit.ConnectionID) (*livekit.StartSession, error) {
claims, err := json.Marshal(pi.Grants)
if err != nil {
return nil, err
}
return &livekit.StartSession{
RoomName: string(roomName),
Identity: string(pi.Identity),
Name: string(pi.Name),
// connection id is to allow the RTC node to identify where to route the message back to
ConnectionId: string(connectionID),
Reconnect: pi.Reconnect,
AutoSubscribe: pi.AutoSubscribe,
Client: pi.Client,
GrantsJson: string(claims),
AdaptiveStream: pi.AdaptiveStream,
}, nil
}
func ParticipantInitFromStartSession(ss *livekit.StartSession) (*ParticipantInit, error) {
claims := &auth.ClaimGrants{}
if err := json.Unmarshal([]byte(ss.GrantsJson), claims); err != nil {
return nil, err
}
return &ParticipantInit{
Identity: livekit.ParticipantIdentity(ss.Identity),
Name: livekit.ParticipantName(ss.Name),
Reconnect: ss.Reconnect,
Client: ss.Client,
AutoSubscribe: ss.AutoSubscribe,
Grants: claims,
AdaptiveStream: ss.AdaptiveStream,
}, nil
}