redis based router & roomstore

This commit is contained in:
David Zhao
2021-01-17 00:12:05 -08:00
parent 9064f6ade1
commit 30e99e8c6b
28 changed files with 1559 additions and 404 deletions
+11 -2
View File
@@ -6,8 +6,9 @@ import (
)
type Config struct {
Port uint32 `yaml:"port"`
RTC RTCConfig `yaml:"rtc"`
Port uint32 `yaml:"port"`
RTC RTCConfig `yaml:"rtc"`
Redis RedisConfig `yaml:"redis"`
// multi-node configuration,
MultiNode bool `yaml:"multi_node"`
@@ -24,6 +25,11 @@ type RTCConfig struct {
MaxBufferTime int `yaml:"max_buffer_time"`
}
type RedisConfig struct {
Address string `yaml:"address"`
Password string `yaml:"password"`
}
func NewConfig(confString string) (*Config, error) {
// start with defaults
conf := &Config{
@@ -35,6 +41,9 @@ func NewConfig(confString string) (*Config, error) {
"stun.l.google.com:19302",
},
},
Redis: RedisConfig{
Address: "localhost:6379",
},
}
if confString != "" {
yaml.Unmarshal([]byte(confString), conf)
+4 -2
View File
@@ -3,6 +3,8 @@ package routing
import "errors"
var (
ErrNodeNotFound = errors.New("could not find node")
ErrHandlerNotDefined = errors.New("handler not defined")
ErrNotFound = errors.New("could not find object")
ErrHandlerNotDefined = errors.New("handler not defined")
ErrIncorrectNodeForRoom = errors.New("incorrect node for the current room")
errInvalidRouterMessage = errors.New("invalid router message")
)
+11 -7
View File
@@ -1,6 +1,8 @@
package routing
import (
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/proto/livekit"
)
@@ -9,13 +11,14 @@ import (
// routes signaling message
//counterfeiter:generate . MessageSink
type MessageSink interface {
WriteMessage(msg interface{}) error
WriteMessage(msg proto.Message) error
Close()
OnClose(f func())
}
//counterfeiter:generate . MessageSource
type MessageSource interface {
ReadMessage() (interface{}, error)
ReadMessage() (proto.Message, error)
}
type ParticipantCallback func(roomId, participantId, participantName string, requestSource MessageSource, responseSink MessageSink)
@@ -23,14 +26,15 @@ type ParticipantCallback func(roomId, participantId, participantName string, req
//counterfeiter:generate . Router
type Router interface {
GetNodeIdForRoom(roomName string) (string, error)
RegisterNode(node *livekit.Node) error
RegisterNode() error
UnregisterNode() error
GetNode(nodeId string) (*livekit.Node, error)
StartParticipant(roomName, participantId, participantName, nodeId string) error
SetRTCNode(participantId, nodeId string) error
SetParticipantRTCNode(participantId, nodeId string) error
// functions for websocket handler
GetRequestSink(participantId string) MessageSink
GetResponseSource(participantId string) MessageSource
GetRequestSink(participantId string) (MessageSink, error)
GetResponseSource(participantId string) (MessageSource, error)
StartParticipant(roomName, participantId, participantName string) error
OnNewParticipant(callback ParticipantCallback)
Start() error
+17 -8
View File
@@ -29,7 +29,11 @@ func (r *LocalRouter) GetNodeIdForRoom(roomName string) (string, error) {
return r.currentNode.Id, nil
}
func (r *LocalRouter) RegisterNode(node *livekit.Node) error {
func (r *LocalRouter) RegisterNode() error {
return nil
}
func (r *LocalRouter) UnregisterNode() error {
return nil
}
@@ -37,10 +41,10 @@ func (r *LocalRouter) GetNode(nodeId string) (*livekit.Node, error) {
if nodeId == r.currentNode.Id {
return r.currentNode, nil
}
return nil, ErrNodeNotFound
return nil, ErrNotFound
}
func (r *LocalRouter) StartParticipant(roomName, participantId, participantName, nodeId string) error {
func (r *LocalRouter) StartParticipant(roomName, participantId, participantName string) error {
// treat it as a new participant connecting
if r.onNewParticipant == nil {
return ErrHandlerNotDefined
@@ -55,18 +59,18 @@ func (r *LocalRouter) StartParticipant(roomName, participantId, participantName,
return nil
}
func (r *LocalRouter) SetRTCNode(participantId, nodeId string) error {
func (r *LocalRouter) SetParticipantRTCNode(participantId, nodeId string) error {
// nothing to be done
return nil
}
// for a local router, sink and source are pointing to the same spot
func (r *LocalRouter) GetRequestSink(participantId string) MessageSink {
return r.getOrCreateMessageChannel(r.requestChannels, participantId)
func (r *LocalRouter) GetRequestSink(participantId string) (MessageSink, error) {
return r.getOrCreateMessageChannel(r.requestChannels, participantId), nil
}
func (r *LocalRouter) GetResponseSource(participantId string) MessageSource {
return r.getOrCreateMessageChannel(r.responseChannels, participantId)
func (r *LocalRouter) GetResponseSource(participantId string) (MessageSource, error) {
return r.getOrCreateMessageChannel(r.responseChannels, participantId), nil
}
func (r *LocalRouter) OnNewParticipant(callback ParticipantCallback) {
@@ -91,6 +95,11 @@ func (r *LocalRouter) getOrCreateMessageChannel(target map[string]*MessageChanne
}
mc = NewMessageChannel()
mc.onClose = func() {
r.lock.Lock()
delete(target, participantId)
r.lock.Unlock()
}
r.lock.Lock()
target[participantId] = mc
r.lock.Unlock()
+14 -4
View File
@@ -2,24 +2,31 @@ package routing
import (
"io"
"google.golang.org/protobuf/proto"
)
type MessageChannel struct {
msgChan chan interface{}
msgChan chan proto.Message
onClose func()
}
func NewMessageChannel() *MessageChannel {
return &MessageChannel{
msgChan: make(chan interface{}, 1),
msgChan: make(chan proto.Message, 1),
}
}
func (m *MessageChannel) WriteMessage(msg interface{}) error {
func (m *MessageChannel) OnClose(f func()) {
m.onClose = f
}
func (m *MessageChannel) WriteMessage(msg proto.Message) error {
m.msgChan <- msg
return nil
}
func (m *MessageChannel) ReadMessage() (interface{}, error) {
func (m *MessageChannel) ReadMessage() (proto.Message, error) {
msg := <-m.msgChan
// channel closed
if msg == nil {
@@ -30,4 +37,7 @@ func (m *MessageChannel) ReadMessage() (interface{}, error) {
func (m *MessageChannel) Close() {
close(m.msgChan)
if m.onClose != nil {
m.onClose()
}
}
+33 -1
View File
@@ -1,8 +1,10 @@
package routing
import (
"bytes"
"context"
"fmt"
"net"
"runtime"
"time"
@@ -30,7 +32,7 @@ func NewLocalNode(conf *config.Config) (LocalNode, error) {
return nil, err
}
return &livekit.Node{
Id: utils.NewGuid(utils.NodePrefix),
Id: fmt.Sprintf("%s%16.16X", utils.NodePrefix, macUint64()),
Ip: ip,
NumCpus: uint32(runtime.NumCPU()),
}, nil
@@ -91,3 +93,33 @@ func GetLocalIP(stunServers []string) (string, error) {
return nodeIp, nil
}
func macUint64() uint64 {
interfaces, err := net.Interfaces()
if err != nil {
return 0
}
for _, i := range interfaces {
if i.Flags&net.FlagUp != 0 && bytes.Compare(i.HardwareAddr, nil) != 0 {
// Skip locally administered addresses
if i.HardwareAddr[0]&2 == 2 {
continue
}
var mac uint64
for j, b := range i.HardwareAddr {
if j >= 8 {
break
}
mac <<= 8
mac += uint64(b)
}
return mac
}
}
return 0
}
+88
View File
@@ -0,0 +1,88 @@
package routing
import (
"context"
"github.com/go-redis/redis/v8"
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/proto/livekit"
)
const (
// hash of node_id => Node proto
NodesKey = "nodes"
// hash of room_name => node_id
NodeRoomKey = "room_node_map"
)
var redisCtx = context.Background()
// location of the participant's RTC connection, hash
func participantRTCKey(participantId string) string {
return "participant_rtc:" + participantId
}
// location of the participant's Signal connection, hash
func participantSignalKey(participantId string) string {
return "participant_signal:" + participantId
}
func nodeChannel(nodeId string) string {
return "node_channel:" + nodeId
}
func publishRouterMessage(rc *redis.Client, nodeId string, participantId string, msg proto.Message) error {
rm := &livekit.RouterMessage{
ParticipantId: participantId,
}
switch o := msg.(type) {
case *livekit.StartSession:
rm.Message = &livekit.RouterMessage_StartSession{
StartSession: o,
}
case *livekit.SignalRequest:
rm.Message = &livekit.RouterMessage_Request{
Request: o,
}
case *livekit.SignalResponse:
rm.Message = &livekit.RouterMessage_Response{
Response: o,
}
case *livekit.EndSession:
rm.Message = &livekit.RouterMessage_EndSession{
EndSession: o,
}
default:
return errInvalidRouterMessage
}
data, err := proto.Marshal(rm)
if err != nil {
return err
}
return rc.Publish(redisCtx, nodeChannel(nodeId), data).Err()
}
type RedisSink struct {
rc *redis.Client
nodeId string
participantId string
channel string
onClose func()
}
func (s *RedisSink) WriteMessage(msg proto.Message) error {
return publishRouterMessage(s.rc, s.nodeId, s.participantId, msg)
}
func (s *RedisSink) Close() {
publishRouterMessage(s.rc, s.nodeId, s.participantId, &livekit.EndSession{})
if s.onClose != nil {
s.onClose()
}
}
func (s *RedisSink) OnClose(f func()) {
s.onClose = f
}
+250
View File
@@ -0,0 +1,250 @@
package routing
import (
"context"
"time"
"github.com/go-redis/redis/v8"
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/pkg/logger"
"github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/livekit-server/proto/livekit"
)
const (
defaultCacheTTL = time.Minute
)
// TODO: need to implement redis key cleanup, for when clients disconnect and such
type RedisRouter struct {
LocalRouter
useLocal bool
rc *redis.Client
cr *utils.CachedRedis
ctx context.Context
redisSinks map[string]*RedisSink
cancel func()
}
func NewRedisRouter(currentNode LocalNode, rc *redis.Client, useLocal bool) *RedisRouter {
rr := &RedisRouter{
LocalRouter: *NewLocalRouter(currentNode),
useLocal: useLocal,
rc: rc,
}
rr.ctx, rr.cancel = context.WithCancel(context.Background())
rr.cr = utils.NewCachedRedis(rr.ctx, rr.rc)
return rr
}
func (r *RedisRouter) RegisterNode() error {
data, err := proto.Marshal((*livekit.Node)(r.currentNode))
if err != nil {
return err
}
r.cr.ExpireHash(NodesKey, r.currentNode.Id)
return r.rc.HSet(r.ctx, NodesKey, data).Err()
}
func (r *RedisRouter) UnregisterNode() error {
r.rc.HDel(r.ctx, NodesKey, r.currentNode.Id)
return nil
}
func (r *RedisRouter) GetNodeIdForRoom(roomName string) (string, error) {
return r.cr.CachedHGet(NodeRoomKey, roomName)
}
func (r *RedisRouter) GetNode(nodeId string) (*livekit.Node, error) {
data, err := r.cr.CachedHGet(NodesKey, nodeId)
if err != nil {
return nil, err
}
n := livekit.Node{}
if err = proto.Unmarshal([]byte(data), &n); err != nil {
return nil, err
}
return &n, nil
}
func (r *RedisRouter) SetParticipantRTCNode(participantId, nodeId string) error {
r.cr.Expire(participantRTCKey(participantId))
return r.rc.Set(r.ctx, participantRTCKey(participantId), nodeId, 0).Err()
}
// for a local router, sink and source are pointing to the same spot
func (r *RedisRouter) GetRequestSink(participantId string) (MessageSink, error) {
// request should go to RTC node
rtcNode, err := r.getParticipantRTCNode(participantId)
if err != nil {
return nil, err
}
if rtcNode == r.currentNode.Id && r.useLocal {
return r.LocalRouter.GetRequestSink(participantId)
}
sink := r.getOrCreateRedisSink(rtcNode, participantId)
return sink, nil
}
func (r *RedisRouter) GetResponseSource(participantId string) (MessageSource, error) {
// request should go to RTC node
rtcNode, err := r.getParticipantRTCNode(participantId)
if err != nil {
return nil, err
}
if rtcNode == r.currentNode.Id && r.useLocal {
return r.LocalRouter.GetResponseSource(participantId)
}
// a message channel that we'll send data into
source := r.getOrCreateMessageChannel(r.responseChannels, participantId)
return source, nil
}
func (r *RedisRouter) StartParticipant(roomName, participantId, participantName string) error {
// find the node where the room is hosted at
rtcNode, err := r.GetNodeIdForRoom(roomName)
if err != nil {
return err
}
// StartParticipant should only be called on the current node
if rtcNode != r.currentNode.Id {
return ErrIncorrectNodeForRoom
}
if r.useLocal {
return r.LocalRouter.StartParticipant(roomName, participantId, participantId)
}
// find signal node to send responses back
signalNode, err := r.getParticipantSignalNode(participantId)
if err != nil {
return err
}
// treat it as a new participant connecting
if r.onNewParticipant == nil {
return ErrHandlerNotDefined
}
r.onNewParticipant(
roomName,
participantId,
participantName,
r.getOrCreateMessageChannel(r.requestChannels, participantId),
r.getOrCreateRedisSink(signalNode, participantId),
)
return nil
}
func (r *RedisRouter) Start() error {
go r.subscribeWorker()
return nil
}
func (r *RedisRouter) Stop() {
r.cancel()
}
func (r *RedisRouter) getOrCreateRedisSink(nodeId string, participantId string) *RedisSink {
r.lock.RLock()
sink := r.redisSinks[participantId]
r.lock.RUnlock()
if sink != nil {
return sink
}
sink = &RedisSink{
rc: r.rc,
nodeId: nodeId,
participantId: participantId,
channel: nodeChannel(nodeId),
}
sink.OnClose(func() {
r.lock.Lock()
delete(r.redisSinks, participantId)
r.lock.Unlock()
})
r.lock.Lock()
r.redisSinks[participantId] = sink
r.lock.Unlock()
return nil
}
func (r *RedisRouter) getParticipantRTCNode(participantId string) (string, error) {
return r.cr.CachedGet(participantRTCKey(participantId))
}
func (r *RedisRouter) getParticipantSignalNode(participantId string) (nodeId string, err error) {
return r.cr.CachedGet(participantSignalKey(participantId))
}
func (r *RedisRouter) subscribeWorker() {
sub := r.rc.Subscribe(redisCtx, nodeChannel(r.currentNode.Id))
for r.ctx.Err() == nil {
obj, err := sub.Receive(r.ctx)
if err != nil {
// TODO: retry? ignore? at a minimum need to sleep here to retry
time.Sleep(100 * time.Millisecond)
continue
}
msg, ok := obj.(*redis.Message)
if !ok {
continue
}
rm := livekit.RouterMessage{}
err = proto.Unmarshal([]byte(msg.Payload), &rm)
pId := rm.ParticipantId
switch rmb := rm.Message.(type) {
case *livekit.RouterMessage_StartSession:
// RTC session should start on this node
err = r.StartParticipant(rmb.StartSession.RoomName, pId, rmb.StartSession.ParticipantName)
if err != nil {
logger.Errorw("could not start participant", "error", err)
}
case *livekit.RouterMessage_Request:
// in the event the current node is an RTC node, push to request channels
reqSink := r.getOrCreateMessageChannel(r.requestChannels, pId)
err = reqSink.WriteMessage(rmb.Request)
if err != nil {
logger.Errorw("could not write to request channel",
"participant", pId,
"error", err)
}
case *livekit.RouterMessage_Response:
// in the event the current node is an Signal node, push to response channels
resSink := r.getOrCreateMessageChannel(r.responseChannels, pId)
err = resSink.WriteMessage(rmb.Response)
if err != nil {
logger.Errorw("could not write to response channel",
"participant", pId,
"error", err)
}
case *livekit.RouterMessage_EndSession:
signalNode, err := r.getParticipantRTCNode(pId)
if err == nil {
logger.Errorw("could not get participant RTC node",
"error", err)
continue
}
// EndSession can only be initiated on an RTC node, is handled on the signal node
if signalNode == r.currentNode.Id {
resSink := r.getOrCreateMessageChannel(r.responseChannels, pId)
resSink.Close()
}
}
}
}
+46 -6
View File
@@ -5,6 +5,7 @@ import (
"sync"
"github.com/livekit/livekit-server/pkg/routing"
"google.golang.org/protobuf/reflect/protoreflect"
)
type FakeMessageSink struct {
@@ -12,10 +13,15 @@ type FakeMessageSink struct {
closeMutex sync.RWMutex
closeArgsForCall []struct {
}
WriteMessageStub func(interface{}) error
OnCloseStub func(func())
onCloseMutex sync.RWMutex
onCloseArgsForCall []struct {
arg1 func()
}
WriteMessageStub func(protoreflect.ProtoMessage) error
writeMessageMutex sync.RWMutex
writeMessageArgsForCall []struct {
arg1 interface{}
arg1 protoreflect.ProtoMessage
}
writeMessageReturns struct {
result1 error
@@ -51,11 +57,43 @@ func (fake *FakeMessageSink) CloseCalls(stub func()) {
fake.CloseStub = stub
}
func (fake *FakeMessageSink) WriteMessage(arg1 interface{}) error {
func (fake *FakeMessageSink) OnClose(arg1 func()) {
fake.onCloseMutex.Lock()
fake.onCloseArgsForCall = append(fake.onCloseArgsForCall, struct {
arg1 func()
}{arg1})
stub := fake.OnCloseStub
fake.recordInvocation("OnClose", []interface{}{arg1})
fake.onCloseMutex.Unlock()
if stub != nil {
fake.OnCloseStub(arg1)
}
}
func (fake *FakeMessageSink) OnCloseCallCount() int {
fake.onCloseMutex.RLock()
defer fake.onCloseMutex.RUnlock()
return len(fake.onCloseArgsForCall)
}
func (fake *FakeMessageSink) OnCloseCalls(stub func(func())) {
fake.onCloseMutex.Lock()
defer fake.onCloseMutex.Unlock()
fake.OnCloseStub = stub
}
func (fake *FakeMessageSink) OnCloseArgsForCall(i int) func() {
fake.onCloseMutex.RLock()
defer fake.onCloseMutex.RUnlock()
argsForCall := fake.onCloseArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeMessageSink) WriteMessage(arg1 protoreflect.ProtoMessage) error {
fake.writeMessageMutex.Lock()
ret, specificReturn := fake.writeMessageReturnsOnCall[len(fake.writeMessageArgsForCall)]
fake.writeMessageArgsForCall = append(fake.writeMessageArgsForCall, struct {
arg1 interface{}
arg1 protoreflect.ProtoMessage
}{arg1})
stub := fake.WriteMessageStub
fakeReturns := fake.writeMessageReturns
@@ -76,13 +114,13 @@ func (fake *FakeMessageSink) WriteMessageCallCount() int {
return len(fake.writeMessageArgsForCall)
}
func (fake *FakeMessageSink) WriteMessageCalls(stub func(interface{}) error) {
func (fake *FakeMessageSink) WriteMessageCalls(stub func(protoreflect.ProtoMessage) error) {
fake.writeMessageMutex.Lock()
defer fake.writeMessageMutex.Unlock()
fake.WriteMessageStub = stub
}
func (fake *FakeMessageSink) WriteMessageArgsForCall(i int) interface{} {
func (fake *FakeMessageSink) WriteMessageArgsForCall(i int) protoreflect.ProtoMessage {
fake.writeMessageMutex.RLock()
defer fake.writeMessageMutex.RUnlock()
argsForCall := fake.writeMessageArgsForCall[i]
@@ -117,6 +155,8 @@ func (fake *FakeMessageSink) Invocations() map[string][][]interface{} {
defer fake.invocationsMutex.RUnlock()
fake.closeMutex.RLock()
defer fake.closeMutex.RUnlock()
fake.onCloseMutex.RLock()
defer fake.onCloseMutex.RUnlock()
fake.writeMessageMutex.RLock()
defer fake.writeMessageMutex.RUnlock()
copiedInvocations := map[string][][]interface{}{}
+11 -10
View File
@@ -5,26 +5,27 @@ import (
"sync"
"github.com/livekit/livekit-server/pkg/routing"
"google.golang.org/protobuf/reflect/protoreflect"
)
type FakeMessageSource struct {
ReadMessageStub func() (interface{}, error)
ReadMessageStub func() (protoreflect.ProtoMessage, error)
readMessageMutex sync.RWMutex
readMessageArgsForCall []struct {
}
readMessageReturns struct {
result1 interface{}
result1 protoreflect.ProtoMessage
result2 error
}
readMessageReturnsOnCall map[int]struct {
result1 interface{}
result1 protoreflect.ProtoMessage
result2 error
}
invocations map[string][][]interface{}
invocationsMutex sync.RWMutex
}
func (fake *FakeMessageSource) ReadMessage() (interface{}, error) {
func (fake *FakeMessageSource) ReadMessage() (protoreflect.ProtoMessage, error) {
fake.readMessageMutex.Lock()
ret, specificReturn := fake.readMessageReturnsOnCall[len(fake.readMessageArgsForCall)]
fake.readMessageArgsForCall = append(fake.readMessageArgsForCall, struct {
@@ -48,34 +49,34 @@ func (fake *FakeMessageSource) ReadMessageCallCount() int {
return len(fake.readMessageArgsForCall)
}
func (fake *FakeMessageSource) ReadMessageCalls(stub func() (interface{}, error)) {
func (fake *FakeMessageSource) ReadMessageCalls(stub func() (protoreflect.ProtoMessage, error)) {
fake.readMessageMutex.Lock()
defer fake.readMessageMutex.Unlock()
fake.ReadMessageStub = stub
}
func (fake *FakeMessageSource) ReadMessageReturns(result1 interface{}, result2 error) {
func (fake *FakeMessageSource) ReadMessageReturns(result1 protoreflect.ProtoMessage, result2 error) {
fake.readMessageMutex.Lock()
defer fake.readMessageMutex.Unlock()
fake.ReadMessageStub = nil
fake.readMessageReturns = struct {
result1 interface{}
result1 protoreflect.ProtoMessage
result2 error
}{result1, result2}
}
func (fake *FakeMessageSource) ReadMessageReturnsOnCall(i int, result1 interface{}, result2 error) {
func (fake *FakeMessageSource) ReadMessageReturnsOnCall(i int, result1 protoreflect.ProtoMessage, result2 error) {
fake.readMessageMutex.Lock()
defer fake.readMessageMutex.Unlock()
fake.ReadMessageStub = nil
if fake.readMessageReturnsOnCall == nil {
fake.readMessageReturnsOnCall = make(map[int]struct {
result1 interface{}
result1 protoreflect.ProtoMessage
result2 error
})
}
fake.readMessageReturnsOnCall[i] = struct {
result1 interface{}
result1 protoreflect.ProtoMessage
result2 error
}{result1, result2}
}
+146 -82
View File
@@ -35,37 +35,40 @@ type FakeRouter struct {
result1 string
result2 error
}
GetRequestSinkStub func(string) routing.MessageSink
GetRequestSinkStub func(string) (routing.MessageSink, error)
getRequestSinkMutex sync.RWMutex
getRequestSinkArgsForCall []struct {
arg1 string
}
getRequestSinkReturns struct {
result1 routing.MessageSink
result2 error
}
getRequestSinkReturnsOnCall map[int]struct {
result1 routing.MessageSink
result2 error
}
GetResponseSourceStub func(string) routing.MessageSource
GetResponseSourceStub func(string) (routing.MessageSource, error)
getResponseSourceMutex sync.RWMutex
getResponseSourceArgsForCall []struct {
arg1 string
}
getResponseSourceReturns struct {
result1 routing.MessageSource
result2 error
}
getResponseSourceReturnsOnCall map[int]struct {
result1 routing.MessageSource
result2 error
}
OnNewParticipantStub func(routing.ParticipantCallback)
onNewParticipantMutex sync.RWMutex
onNewParticipantArgsForCall []struct {
arg1 routing.ParticipantCallback
}
RegisterNodeStub func(*livekit.Node) error
RegisterNodeStub func() error
registerNodeMutex sync.RWMutex
registerNodeArgsForCall []struct {
arg1 *livekit.Node
}
registerNodeReturns struct {
result1 error
@@ -73,16 +76,16 @@ type FakeRouter struct {
registerNodeReturnsOnCall map[int]struct {
result1 error
}
SetRTCNodeStub func(string, string) error
setRTCNodeMutex sync.RWMutex
setRTCNodeArgsForCall []struct {
SetParticipantRTCNodeStub func(string, string) error
setParticipantRTCNodeMutex sync.RWMutex
setParticipantRTCNodeArgsForCall []struct {
arg1 string
arg2 string
}
setRTCNodeReturns struct {
setParticipantRTCNodeReturns struct {
result1 error
}
setRTCNodeReturnsOnCall map[int]struct {
setParticipantRTCNodeReturnsOnCall map[int]struct {
result1 error
}
StartStub func() error
@@ -95,13 +98,12 @@ type FakeRouter struct {
startReturnsOnCall map[int]struct {
result1 error
}
StartParticipantStub func(string, string, string, string) error
StartParticipantStub func(string, string, string) error
startParticipantMutex sync.RWMutex
startParticipantArgsForCall []struct {
arg1 string
arg2 string
arg3 string
arg4 string
}
startParticipantReturns struct {
result1 error
@@ -113,6 +115,16 @@ type FakeRouter struct {
stopMutex sync.RWMutex
stopArgsForCall []struct {
}
UnregisterNodeStub func() error
unregisterNodeMutex sync.RWMutex
unregisterNodeArgsForCall []struct {
}
unregisterNodeReturns struct {
result1 error
}
unregisterNodeReturnsOnCall map[int]struct {
result1 error
}
invocations map[string][][]interface{}
invocationsMutex sync.RWMutex
}
@@ -245,7 +257,7 @@ func (fake *FakeRouter) GetNodeIdForRoomReturnsOnCall(i int, result1 string, res
}{result1, result2}
}
func (fake *FakeRouter) GetRequestSink(arg1 string) routing.MessageSink {
func (fake *FakeRouter) GetRequestSink(arg1 string) (routing.MessageSink, error) {
fake.getRequestSinkMutex.Lock()
ret, specificReturn := fake.getRequestSinkReturnsOnCall[len(fake.getRequestSinkArgsForCall)]
fake.getRequestSinkArgsForCall = append(fake.getRequestSinkArgsForCall, struct {
@@ -259,9 +271,9 @@ func (fake *FakeRouter) GetRequestSink(arg1 string) routing.MessageSink {
return stub(arg1)
}
if specificReturn {
return ret.result1
return ret.result1, ret.result2
}
return fakeReturns.result1
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakeRouter) GetRequestSinkCallCount() int {
@@ -270,7 +282,7 @@ func (fake *FakeRouter) GetRequestSinkCallCount() int {
return len(fake.getRequestSinkArgsForCall)
}
func (fake *FakeRouter) GetRequestSinkCalls(stub func(string) routing.MessageSink) {
func (fake *FakeRouter) GetRequestSinkCalls(stub func(string) (routing.MessageSink, error)) {
fake.getRequestSinkMutex.Lock()
defer fake.getRequestSinkMutex.Unlock()
fake.GetRequestSinkStub = stub
@@ -283,30 +295,33 @@ func (fake *FakeRouter) GetRequestSinkArgsForCall(i int) string {
return argsForCall.arg1
}
func (fake *FakeRouter) GetRequestSinkReturns(result1 routing.MessageSink) {
func (fake *FakeRouter) GetRequestSinkReturns(result1 routing.MessageSink, result2 error) {
fake.getRequestSinkMutex.Lock()
defer fake.getRequestSinkMutex.Unlock()
fake.GetRequestSinkStub = nil
fake.getRequestSinkReturns = struct {
result1 routing.MessageSink
}{result1}
result2 error
}{result1, result2}
}
func (fake *FakeRouter) GetRequestSinkReturnsOnCall(i int, result1 routing.MessageSink) {
func (fake *FakeRouter) GetRequestSinkReturnsOnCall(i int, result1 routing.MessageSink, result2 error) {
fake.getRequestSinkMutex.Lock()
defer fake.getRequestSinkMutex.Unlock()
fake.GetRequestSinkStub = nil
if fake.getRequestSinkReturnsOnCall == nil {
fake.getRequestSinkReturnsOnCall = make(map[int]struct {
result1 routing.MessageSink
result2 error
})
}
fake.getRequestSinkReturnsOnCall[i] = struct {
result1 routing.MessageSink
}{result1}
result2 error
}{result1, result2}
}
func (fake *FakeRouter) GetResponseSource(arg1 string) routing.MessageSource {
func (fake *FakeRouter) GetResponseSource(arg1 string) (routing.MessageSource, error) {
fake.getResponseSourceMutex.Lock()
ret, specificReturn := fake.getResponseSourceReturnsOnCall[len(fake.getResponseSourceArgsForCall)]
fake.getResponseSourceArgsForCall = append(fake.getResponseSourceArgsForCall, struct {
@@ -320,9 +335,9 @@ func (fake *FakeRouter) GetResponseSource(arg1 string) routing.MessageSource {
return stub(arg1)
}
if specificReturn {
return ret.result1
return ret.result1, ret.result2
}
return fakeReturns.result1
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakeRouter) GetResponseSourceCallCount() int {
@@ -331,7 +346,7 @@ func (fake *FakeRouter) GetResponseSourceCallCount() int {
return len(fake.getResponseSourceArgsForCall)
}
func (fake *FakeRouter) GetResponseSourceCalls(stub func(string) routing.MessageSource) {
func (fake *FakeRouter) GetResponseSourceCalls(stub func(string) (routing.MessageSource, error)) {
fake.getResponseSourceMutex.Lock()
defer fake.getResponseSourceMutex.Unlock()
fake.GetResponseSourceStub = stub
@@ -344,27 +359,30 @@ func (fake *FakeRouter) GetResponseSourceArgsForCall(i int) string {
return argsForCall.arg1
}
func (fake *FakeRouter) GetResponseSourceReturns(result1 routing.MessageSource) {
func (fake *FakeRouter) GetResponseSourceReturns(result1 routing.MessageSource, result2 error) {
fake.getResponseSourceMutex.Lock()
defer fake.getResponseSourceMutex.Unlock()
fake.GetResponseSourceStub = nil
fake.getResponseSourceReturns = struct {
result1 routing.MessageSource
}{result1}
result2 error
}{result1, result2}
}
func (fake *FakeRouter) GetResponseSourceReturnsOnCall(i int, result1 routing.MessageSource) {
func (fake *FakeRouter) GetResponseSourceReturnsOnCall(i int, result1 routing.MessageSource, result2 error) {
fake.getResponseSourceMutex.Lock()
defer fake.getResponseSourceMutex.Unlock()
fake.GetResponseSourceStub = nil
if fake.getResponseSourceReturnsOnCall == nil {
fake.getResponseSourceReturnsOnCall = make(map[int]struct {
result1 routing.MessageSource
result2 error
})
}
fake.getResponseSourceReturnsOnCall[i] = struct {
result1 routing.MessageSource
}{result1}
result2 error
}{result1, result2}
}
func (fake *FakeRouter) OnNewParticipant(arg1 routing.ParticipantCallback) {
@@ -399,18 +417,17 @@ func (fake *FakeRouter) OnNewParticipantArgsForCall(i int) routing.ParticipantCa
return argsForCall.arg1
}
func (fake *FakeRouter) RegisterNode(arg1 *livekit.Node) error {
func (fake *FakeRouter) RegisterNode() error {
fake.registerNodeMutex.Lock()
ret, specificReturn := fake.registerNodeReturnsOnCall[len(fake.registerNodeArgsForCall)]
fake.registerNodeArgsForCall = append(fake.registerNodeArgsForCall, struct {
arg1 *livekit.Node
}{arg1})
}{})
stub := fake.RegisterNodeStub
fakeReturns := fake.registerNodeReturns
fake.recordInvocation("RegisterNode", []interface{}{arg1})
fake.recordInvocation("RegisterNode", []interface{}{})
fake.registerNodeMutex.Unlock()
if stub != nil {
return stub(arg1)
return stub()
}
if specificReturn {
return ret.result1
@@ -424,19 +441,12 @@ func (fake *FakeRouter) RegisterNodeCallCount() int {
return len(fake.registerNodeArgsForCall)
}
func (fake *FakeRouter) RegisterNodeCalls(stub func(*livekit.Node) error) {
func (fake *FakeRouter) RegisterNodeCalls(stub func() error) {
fake.registerNodeMutex.Lock()
defer fake.registerNodeMutex.Unlock()
fake.RegisterNodeStub = stub
}
func (fake *FakeRouter) RegisterNodeArgsForCall(i int) *livekit.Node {
fake.registerNodeMutex.RLock()
defer fake.registerNodeMutex.RUnlock()
argsForCall := fake.registerNodeArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeRouter) RegisterNodeReturns(result1 error) {
fake.registerNodeMutex.Lock()
defer fake.registerNodeMutex.Unlock()
@@ -460,17 +470,17 @@ func (fake *FakeRouter) RegisterNodeReturnsOnCall(i int, result1 error) {
}{result1}
}
func (fake *FakeRouter) SetRTCNode(arg1 string, arg2 string) error {
fake.setRTCNodeMutex.Lock()
ret, specificReturn := fake.setRTCNodeReturnsOnCall[len(fake.setRTCNodeArgsForCall)]
fake.setRTCNodeArgsForCall = append(fake.setRTCNodeArgsForCall, struct {
func (fake *FakeRouter) SetParticipantRTCNode(arg1 string, arg2 string) error {
fake.setParticipantRTCNodeMutex.Lock()
ret, specificReturn := fake.setParticipantRTCNodeReturnsOnCall[len(fake.setParticipantRTCNodeArgsForCall)]
fake.setParticipantRTCNodeArgsForCall = append(fake.setParticipantRTCNodeArgsForCall, struct {
arg1 string
arg2 string
}{arg1, arg2})
stub := fake.SetRTCNodeStub
fakeReturns := fake.setRTCNodeReturns
fake.recordInvocation("SetRTCNode", []interface{}{arg1, arg2})
fake.setRTCNodeMutex.Unlock()
stub := fake.SetParticipantRTCNodeStub
fakeReturns := fake.setParticipantRTCNodeReturns
fake.recordInvocation("SetParticipantRTCNode", []interface{}{arg1, arg2})
fake.setParticipantRTCNodeMutex.Unlock()
if stub != nil {
return stub(arg1, arg2)
}
@@ -480,44 +490,44 @@ func (fake *FakeRouter) SetRTCNode(arg1 string, arg2 string) error {
return fakeReturns.result1
}
func (fake *FakeRouter) SetRTCNodeCallCount() int {
fake.setRTCNodeMutex.RLock()
defer fake.setRTCNodeMutex.RUnlock()
return len(fake.setRTCNodeArgsForCall)
func (fake *FakeRouter) SetParticipantRTCNodeCallCount() int {
fake.setParticipantRTCNodeMutex.RLock()
defer fake.setParticipantRTCNodeMutex.RUnlock()
return len(fake.setParticipantRTCNodeArgsForCall)
}
func (fake *FakeRouter) SetRTCNodeCalls(stub func(string, string) error) {
fake.setRTCNodeMutex.Lock()
defer fake.setRTCNodeMutex.Unlock()
fake.SetRTCNodeStub = stub
func (fake *FakeRouter) SetParticipantRTCNodeCalls(stub func(string, string) error) {
fake.setParticipantRTCNodeMutex.Lock()
defer fake.setParticipantRTCNodeMutex.Unlock()
fake.SetParticipantRTCNodeStub = stub
}
func (fake *FakeRouter) SetRTCNodeArgsForCall(i int) (string, string) {
fake.setRTCNodeMutex.RLock()
defer fake.setRTCNodeMutex.RUnlock()
argsForCall := fake.setRTCNodeArgsForCall[i]
func (fake *FakeRouter) SetParticipantRTCNodeArgsForCall(i int) (string, string) {
fake.setParticipantRTCNodeMutex.RLock()
defer fake.setParticipantRTCNodeMutex.RUnlock()
argsForCall := fake.setParticipantRTCNodeArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2
}
func (fake *FakeRouter) SetRTCNodeReturns(result1 error) {
fake.setRTCNodeMutex.Lock()
defer fake.setRTCNodeMutex.Unlock()
fake.SetRTCNodeStub = nil
fake.setRTCNodeReturns = struct {
func (fake *FakeRouter) SetParticipantRTCNodeReturns(result1 error) {
fake.setParticipantRTCNodeMutex.Lock()
defer fake.setParticipantRTCNodeMutex.Unlock()
fake.SetParticipantRTCNodeStub = nil
fake.setParticipantRTCNodeReturns = struct {
result1 error
}{result1}
}
func (fake *FakeRouter) SetRTCNodeReturnsOnCall(i int, result1 error) {
fake.setRTCNodeMutex.Lock()
defer fake.setRTCNodeMutex.Unlock()
fake.SetRTCNodeStub = nil
if fake.setRTCNodeReturnsOnCall == nil {
fake.setRTCNodeReturnsOnCall = make(map[int]struct {
func (fake *FakeRouter) SetParticipantRTCNodeReturnsOnCall(i int, result1 error) {
fake.setParticipantRTCNodeMutex.Lock()
defer fake.setParticipantRTCNodeMutex.Unlock()
fake.SetParticipantRTCNodeStub = nil
if fake.setParticipantRTCNodeReturnsOnCall == nil {
fake.setParticipantRTCNodeReturnsOnCall = make(map[int]struct {
result1 error
})
}
fake.setRTCNodeReturnsOnCall[i] = struct {
fake.setParticipantRTCNodeReturnsOnCall[i] = struct {
result1 error
}{result1}
}
@@ -575,21 +585,20 @@ func (fake *FakeRouter) StartReturnsOnCall(i int, result1 error) {
}{result1}
}
func (fake *FakeRouter) StartParticipant(arg1 string, arg2 string, arg3 string, arg4 string) error {
func (fake *FakeRouter) StartParticipant(arg1 string, arg2 string, arg3 string) error {
fake.startParticipantMutex.Lock()
ret, specificReturn := fake.startParticipantReturnsOnCall[len(fake.startParticipantArgsForCall)]
fake.startParticipantArgsForCall = append(fake.startParticipantArgsForCall, struct {
arg1 string
arg2 string
arg3 string
arg4 string
}{arg1, arg2, arg3, arg4})
}{arg1, arg2, arg3})
stub := fake.StartParticipantStub
fakeReturns := fake.startParticipantReturns
fake.recordInvocation("StartParticipant", []interface{}{arg1, arg2, arg3, arg4})
fake.recordInvocation("StartParticipant", []interface{}{arg1, arg2, arg3})
fake.startParticipantMutex.Unlock()
if stub != nil {
return stub(arg1, arg2, arg3, arg4)
return stub(arg1, arg2, arg3)
}
if specificReturn {
return ret.result1
@@ -603,17 +612,17 @@ func (fake *FakeRouter) StartParticipantCallCount() int {
return len(fake.startParticipantArgsForCall)
}
func (fake *FakeRouter) StartParticipantCalls(stub func(string, string, string, string) error) {
func (fake *FakeRouter) StartParticipantCalls(stub func(string, string, string) error) {
fake.startParticipantMutex.Lock()
defer fake.startParticipantMutex.Unlock()
fake.StartParticipantStub = stub
}
func (fake *FakeRouter) StartParticipantArgsForCall(i int) (string, string, string, string) {
func (fake *FakeRouter) StartParticipantArgsForCall(i int) (string, string, string) {
fake.startParticipantMutex.RLock()
defer fake.startParticipantMutex.RUnlock()
argsForCall := fake.startParticipantArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3
}
func (fake *FakeRouter) StartParticipantReturns(result1 error) {
@@ -663,6 +672,59 @@ func (fake *FakeRouter) StopCalls(stub func()) {
fake.StopStub = stub
}
func (fake *FakeRouter) UnregisterNode() error {
fake.unregisterNodeMutex.Lock()
ret, specificReturn := fake.unregisterNodeReturnsOnCall[len(fake.unregisterNodeArgsForCall)]
fake.unregisterNodeArgsForCall = append(fake.unregisterNodeArgsForCall, struct {
}{})
stub := fake.UnregisterNodeStub
fakeReturns := fake.unregisterNodeReturns
fake.recordInvocation("UnregisterNode", []interface{}{})
fake.unregisterNodeMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeRouter) UnregisterNodeCallCount() int {
fake.unregisterNodeMutex.RLock()
defer fake.unregisterNodeMutex.RUnlock()
return len(fake.unregisterNodeArgsForCall)
}
func (fake *FakeRouter) UnregisterNodeCalls(stub func() error) {
fake.unregisterNodeMutex.Lock()
defer fake.unregisterNodeMutex.Unlock()
fake.UnregisterNodeStub = stub
}
func (fake *FakeRouter) UnregisterNodeReturns(result1 error) {
fake.unregisterNodeMutex.Lock()
defer fake.unregisterNodeMutex.Unlock()
fake.UnregisterNodeStub = nil
fake.unregisterNodeReturns = struct {
result1 error
}{result1}
}
func (fake *FakeRouter) UnregisterNodeReturnsOnCall(i int, result1 error) {
fake.unregisterNodeMutex.Lock()
defer fake.unregisterNodeMutex.Unlock()
fake.UnregisterNodeStub = nil
if fake.unregisterNodeReturnsOnCall == nil {
fake.unregisterNodeReturnsOnCall = make(map[int]struct {
result1 error
})
}
fake.unregisterNodeReturnsOnCall[i] = struct {
result1 error
}{result1}
}
func (fake *FakeRouter) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
@@ -678,14 +740,16 @@ func (fake *FakeRouter) Invocations() map[string][][]interface{} {
defer fake.onNewParticipantMutex.RUnlock()
fake.registerNodeMutex.RLock()
defer fake.registerNodeMutex.RUnlock()
fake.setRTCNodeMutex.RLock()
defer fake.setRTCNodeMutex.RUnlock()
fake.setParticipantRTCNodeMutex.RLock()
defer fake.setParticipantRTCNodeMutex.RUnlock()
fake.startMutex.RLock()
defer fake.startMutex.RUnlock()
fake.startParticipantMutex.RLock()
defer fake.startParticipantMutex.RUnlock()
fake.stopMutex.RLock()
defer fake.stopMutex.RUnlock()
fake.unregisterNodeMutex.RLock()
defer fake.unregisterNodeMutex.RUnlock()
copiedInvocations := map[string][][]interface{}{}
for key, value := range fake.invocations {
copiedInvocations[key] = value
-3
View File
@@ -15,9 +15,6 @@ type LocalRoomStore struct {
// map of roomName => roomId
roomIds map[string]string
lock sync.RWMutex
//CreateRoom(room *livekit.Room) error
//GetRoom(idOrName string) (*livekit.Room, error)
//DeleteRoom(idOrName string) error
}
func NewLocalRoomStore() *LocalRoomStore {
+100
View File
@@ -1 +1,101 @@
package service
import (
"context"
"github.com/go-redis/redis/v8"
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/proto/livekit"
)
const (
// hash of room_name => Room proto
RoomsKey = "rooms"
// hash of room_id => room name
RoomIdMap = "room_id_map"
)
type RedisRoomStore struct {
rc *redis.Client
ctx context.Context
}
func NewRedisRoomStore(rc *redis.Client) *RedisRoomStore {
return &RedisRoomStore{
rc: rc,
}
}
func (p *RedisRoomStore) CreateRoom(room *livekit.Room) error {
err := p.rc.HSet(p.ctx, RoomIdMap, room.Sid, room.Name).Err()
if err != nil {
return err
}
data, err := proto.Marshal(room)
if err != nil {
return err
}
return p.rc.HSet(p.ctx, RoomsKey, room.Name, data).Err()
}
func (p *RedisRoomStore) GetRoom(idOrName string) (*livekit.Room, error) {
// see if matches any ids
name, err := p.rc.HGet(p.ctx, RoomIdMap, idOrName).Result()
if err != nil {
name = idOrName
}
data, err := p.rc.HGet(p.ctx, RoomsKey, name).Result()
if err != nil {
if err == redis.Nil {
err = ErrRoomNotFound
}
return nil, err
}
room := livekit.Room{}
err = proto.Unmarshal([]byte(data), &room)
if err != nil {
return nil, err
}
return &room, nil
}
func (p *RedisRoomStore) ListRooms() ([]*livekit.Room, error) {
items, err := p.rc.HVals(p.ctx, RoomsKey).Result()
if err != nil && err != redis.Nil {
return nil, err
}
rooms := make([]*livekit.Room, 0, len(items))
for _, item := range items {
room := livekit.Room{}
err := proto.Unmarshal([]byte(item), &room)
if err != nil {
return nil, err
}
rooms = append(rooms, &room)
}
return rooms, nil
}
func (p *RedisRoomStore) DeleteRoom(idOrName string) error {
room, err := p.GetRoom(idOrName)
if err == ErrRoomNotFound {
return nil
} else if err != nil {
return err
}
err = p.rc.HDel(p.ctx, RoomIdMap, room.Sid).Err()
err2 := p.rc.HDel(p.ctx, RoomsKey, room.Name).Err()
if err == nil {
err = err2
}
return err
}
+1 -1
View File
@@ -61,7 +61,7 @@ func (r *RTCRunner) StartSession(roomName, participantId, participantName string
}
// register participant to be on this server
if err = r.router.SetRTCNode(participantId, r.currentNode.Id); err != nil {
if err = r.router.SetParticipantRTCNode(participantId, r.currentNode.Id); err != nil {
logger.Errorw("could not set RTC node", "error", err)
return
}
+12 -3
View File
@@ -79,9 +79,10 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sigConn := NewWSSignalConnection(conn)
participantId := utils.NewGuid(utils.ParticipantPrefix)
err = s.router.StartParticipant(roomName, participantId, pName, s.currentNode.Id)
err = s.router.StartParticipant(roomName, participantId, pName)
if err != nil {
handleError(w, http.StatusInternalServerError, "could not set signal node: "+err.Error())
return
}
logger.Infow("new client connected",
@@ -90,8 +91,16 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
"name", pName,
)
reqSink := s.router.GetRequestSink(participantId)
resSource := s.router.GetResponseSource(participantId)
reqSink, err := s.router.GetRequestSink(participantId)
if err != nil {
handleError(w, http.StatusInternalServerError, "could not get request sink"+err.Error())
return
}
resSource, err := s.router.GetResponseSource(participantId)
if err != nil {
handleError(w, http.StatusInternalServerError, "could not get response source"+err.Error())
return
}
go func() {
for {
+21 -12
View File
@@ -18,13 +18,14 @@ import (
)
type LivekitServer struct {
config *config.Config
roomServer livekit.TwirpServer
rtcService *RTCService
httpServer *http.Server
router routing.Router
running bool
doneChan chan bool
config *config.Config
roomServer livekit.TwirpServer
rtcService *RTCService
httpServer *http.Server
router routing.Router
currentNode routing.LocalNode
running bool
doneChan chan bool
}
func NewLivekitServer(conf *config.Config,
@@ -33,12 +34,14 @@ func NewLivekitServer(conf *config.Config,
keyProvider auth.KeyProvider,
router routing.Router,
runner *RTCRunner,
currentNode routing.LocalNode,
) (s *LivekitServer, err error) {
s = &LivekitServer{
config: conf,
roomServer: livekit.NewRoomServiceServer(roomService),
rtcService: rtcService,
router: router,
config: conf,
roomServer: livekit.NewRoomServiceServer(roomService),
rtcService: rtcService,
router: router,
currentNode: currentNode,
}
middlewares := []negroni.Handler{
@@ -73,6 +76,11 @@ func (s *LivekitServer) Start() error {
return errors.New("already running")
}
if err := s.router.RegisterNode(); err != nil {
return err
}
defer s.router.UnregisterNode()
if err := s.router.Start(); err != nil {
return err
}
@@ -86,7 +94,8 @@ func (s *LivekitServer) Start() error {
}
go func() {
logger.Infow("starting LiveKit server", "address", s.httpServer.Addr)
logger.Infow("starting LiveKit server", "address", s.httpServer.Addr,
"nodeId", s.currentNode.Id)
s.httpServer.Serve(ln)
}()
+1 -1
View File
@@ -27,7 +27,7 @@ func InitializeServer(conf *config.Config, keyProvider auth.KeyProvider, roomSto
return nil, err
}
rtcRunner := NewRTCRunner(roomStore, router, currentNode, webRTCConfig)
livekitServer, err := NewLivekitServer(conf, roomService, rtcService, keyProvider, router, rtcRunner)
livekitServer, err := NewLivekitServer(conf, roomService, rtcService, keyProvider, router, rtcRunner, currentNode)
if err != nil {
return nil, err
}
+63
View File
@@ -0,0 +1,63 @@
package utils
import (
"context"
"time"
"github.com/go-redis/redis/v8"
"github.com/karlseguin/ccache/v2"
)
const (
defaultCacheTTL = time.Minute
)
type CachedRedis struct {
ctx context.Context
rc *redis.Client
cache *ccache.Cache
}
func NewCachedRedis(ctx context.Context, rc *redis.Client) *CachedRedis {
return &CachedRedis{
ctx: ctx,
rc: rc,
cache: ccache.New(ccache.Configure()),
}
}
func (r *CachedRedis) CachedHGet(key, hashKey string) (string, error) {
cacheKey := key + ":" + hashKey
item := r.cache.Get(cacheKey)
if item != nil && !item.Expired() {
return item.Value().(string), nil
}
val, err := r.rc.HGet(r.ctx, key, hashKey).Result()
if err != nil {
return "", err
}
r.cache.Set(key, val, defaultCacheTTL)
return val, nil
}
func (r *CachedRedis) CachedGet(key string) (string, error) {
item := r.cache.Get(key)
if item != nil && !item.Expired() {
return item.Value().(string), nil
}
val, err := r.rc.Get(r.ctx, key).Result()
if err != nil {
return "", err
}
r.cache.Set(key, val, defaultCacheTTL)
return val, nil
}
func (r *CachedRedis) ExpireHash(key, hashKey string) {
r.cache.Delete(key + ":" + hashKey)
}
func (r *CachedRedis) Expire(key string) {
r.cache.Delete(key)
}