Move callbacks out of messageRouter (#269)

* move callbacks out of messageRouter

* OCD

* more OCD

* fix forwarder test

* even more OCD

* maximum OCD

* package name collision, copy lock by value
This commit is contained in:
David Colburn
2021-12-17 13:19:23 -08:00
committed by GitHub
parent a06edd885e
commit faa870de3d
82 changed files with 315 additions and 527 deletions
+2 -1
View File
@@ -5,10 +5,11 @@ import (
"net/http/httptest"
"testing"
"github.com/livekit/livekit-server/pkg/service"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/auth/authfakes"
"github.com/stretchr/testify/require"
"github.com/livekit/livekit-server/pkg/service"
)
func TestAuthMiddleware(t *testing.T) {
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"context"
"time"
livekit "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/livekit"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
+10 -10
View File
@@ -5,7 +5,7 @@ import (
"sync"
"time"
livekit "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/livekit"
)
// encapsulates CRUD operations for room settings
@@ -26,7 +26,7 @@ func NewLocalRoomStore() *LocalRoomStore {
}
}
func (p *LocalRoomStore) StoreRoom(ctx context.Context, room *livekit.Room) error {
func (p *LocalRoomStore) StoreRoom(_ context.Context, room *livekit.Room) error {
if room.CreationTime == 0 {
room.CreationTime = time.Now().Unix()
}
@@ -36,7 +36,7 @@ func (p *LocalRoomStore) StoreRoom(ctx context.Context, room *livekit.Room) erro
return nil
}
func (p *LocalRoomStore) LoadRoom(ctx context.Context, name string) (*livekit.Room, error) {
func (p *LocalRoomStore) LoadRoom(_ context.Context, name string) (*livekit.Room, error) {
p.lock.RLock()
defer p.lock.RUnlock()
@@ -47,7 +47,7 @@ func (p *LocalRoomStore) LoadRoom(ctx context.Context, name string) (*livekit.Ro
return room, nil
}
func (p *LocalRoomStore) ListRooms(ctx context.Context) ([]*livekit.Room, error) {
func (p *LocalRoomStore) ListRooms(_ context.Context) ([]*livekit.Room, error) {
p.lock.RLock()
defer p.lock.RUnlock()
rooms := make([]*livekit.Room, 0, len(p.rooms))
@@ -73,18 +73,18 @@ func (p *LocalRoomStore) DeleteRoom(ctx context.Context, name string) error {
return nil
}
func (p *LocalRoomStore) LockRoom(ctx context.Context, name string, duration time.Duration) (string, error) {
func (p *LocalRoomStore) LockRoom(_ context.Context, _ string, _ time.Duration) (string, error) {
// local rooms lock & unlock globally
p.globalLock.Lock()
return "", nil
}
func (p *LocalRoomStore) UnlockRoom(ctx context.Context, name string, uid string) error {
func (p *LocalRoomStore) UnlockRoom(_ context.Context, _ string, _ string) error {
p.globalLock.Unlock()
return nil
}
func (p *LocalRoomStore) StoreParticipant(ctx context.Context, roomName string, participant *livekit.ParticipantInfo) error {
func (p *LocalRoomStore) StoreParticipant(_ context.Context, roomName string, participant *livekit.ParticipantInfo) error {
p.lock.Lock()
defer p.lock.Unlock()
roomParticipants := p.participants[roomName]
@@ -96,7 +96,7 @@ func (p *LocalRoomStore) StoreParticipant(ctx context.Context, roomName string,
return nil
}
func (p *LocalRoomStore) LoadParticipant(ctx context.Context, roomName, identity string) (*livekit.ParticipantInfo, error) {
func (p *LocalRoomStore) LoadParticipant(_ context.Context, roomName, identity string) (*livekit.ParticipantInfo, error) {
p.lock.RLock()
defer p.lock.RUnlock()
@@ -111,7 +111,7 @@ func (p *LocalRoomStore) LoadParticipant(ctx context.Context, roomName, identity
return participant, nil
}
func (p *LocalRoomStore) ListParticipants(ctx context.Context, roomName string) ([]*livekit.ParticipantInfo, error) {
func (p *LocalRoomStore) ListParticipants(_ context.Context, roomName string) ([]*livekit.ParticipantInfo, error) {
p.lock.RLock()
defer p.lock.RUnlock()
@@ -129,7 +129,7 @@ func (p *LocalRoomStore) ListParticipants(ctx context.Context, roomName string)
return items, nil
}
func (p *LocalRoomStore) DeleteParticipant(ctx context.Context, roomName, identity string) error {
func (p *LocalRoomStore) DeleteParticipant(_ context.Context, roomName, identity string) error {
p.lock.Lock()
defer p.lock.Unlock()
+7 -7
View File
@@ -4,7 +4,7 @@ import (
"context"
"errors"
livekit "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/recording"
"github.com/livekit/protocol/utils"
@@ -46,14 +46,14 @@ func (s *RecordingService) StartRecording(ctx context.Context, req *livekit.Star
return nil, errors.New("recording not configured (redis required)")
}
// reserve a recorde
recordingId, err := recording.ReserveRecorder(s.bus)
// reserve a recorder
recordingID, err := recording.ReserveRecorder(s.bus)
if err != nil {
return nil, err
}
// start the recording
err = recording.RPC(ctx, s.bus, recordingId, &livekit.RecordingRequest{
err = recording.RPC(ctx, s.bus, recordingID, &livekit.RecordingRequest{
Request: &livekit.RecordingRequest_Start{
Start: req,
},
@@ -63,16 +63,16 @@ func (s *RecordingService) StartRecording(ctx context.Context, req *livekit.Star
}
ri := &livekit.RecordingInfo{
Id: recordingId,
Id: recordingID,
Active: true,
}
if template := req.Input.(*livekit.StartRecordingRequest_Template); template != nil {
ri.RoomName = template.Template.RoomName
}
logger.Debugw("recording started", "recordingID", recordingId)
logger.Debugw("recording started", "recordingID", recordingID)
s.telemetry.RecordingStarted(ctx, ri)
return &livekit.StartRecordingResponse{RecordingId: recordingId}, nil
return &livekit.StartRecordingResponse{RecordingId: recordingID}, nil
}
func (s *RecordingService) AddOutput(ctx context.Context, req *livekit.AddOutputRequest) (*emptypb.Empty, error) {
+10 -10
View File
@@ -5,7 +5,7 @@ import (
"time"
"github.com/go-redis/redis/v8"
livekit "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/utils"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
@@ -35,7 +35,7 @@ func NewRedisRoomStore(rc *redis.Client) *RedisRoomStore {
}
}
func (p *RedisRoomStore) StoreRoom(ctx context.Context, room *livekit.Room) error {
func (p *RedisRoomStore) StoreRoom(_ context.Context, room *livekit.Room) error {
if room.CreationTime == 0 {
room.CreationTime = time.Now().Unix()
}
@@ -54,7 +54,7 @@ func (p *RedisRoomStore) StoreRoom(ctx context.Context, room *livekit.Room) erro
return nil
}
func (p *RedisRoomStore) LoadRoom(ctx context.Context, name string) (*livekit.Room, error) {
func (p *RedisRoomStore) LoadRoom(_ context.Context, name string) (*livekit.Room, error) {
data, err := p.rc.HGet(p.ctx, RoomsKey, name).Result()
if err != nil {
if err == redis.Nil {
@@ -72,7 +72,7 @@ func (p *RedisRoomStore) LoadRoom(ctx context.Context, name string) (*livekit.Ro
return &room, nil
}
func (p *RedisRoomStore) ListRooms(ctx context.Context) ([]*livekit.Room, error) {
func (p *RedisRoomStore) ListRooms(_ context.Context) ([]*livekit.Room, error) {
items, err := p.rc.HVals(p.ctx, RoomsKey).Result()
if err != nil && err != redis.Nil {
return nil, errors.Wrap(err, "could not get rooms")
@@ -105,7 +105,7 @@ func (p *RedisRoomStore) DeleteRoom(ctx context.Context, name string) error {
return err
}
func (p *RedisRoomStore) LockRoom(ctx context.Context, name string, duration time.Duration) (string, error) {
func (p *RedisRoomStore) LockRoom(_ context.Context, name string, duration time.Duration) (string, error) {
token := utils.NewGuid("LOCK")
key := RoomLockPrefix + name
@@ -130,7 +130,7 @@ func (p *RedisRoomStore) LockRoom(ctx context.Context, name string, duration tim
return "", ErrRoomLockFailed
}
func (p *RedisRoomStore) UnlockRoom(ctx context.Context, name string, uid string) error {
func (p *RedisRoomStore) UnlockRoom(_ context.Context, name string, uid string) error {
key := RoomLockPrefix + name
val, err := p.rc.Get(p.ctx, key).Result()
@@ -147,7 +147,7 @@ func (p *RedisRoomStore) UnlockRoom(ctx context.Context, name string, uid string
return p.rc.Del(p.ctx, key).Err()
}
func (p *RedisRoomStore) StoreParticipant(ctx context.Context, roomName string, participant *livekit.ParticipantInfo) error {
func (p *RedisRoomStore) StoreParticipant(_ context.Context, roomName string, participant *livekit.ParticipantInfo) error {
key := RoomParticipantsPrefix + roomName
data, err := proto.Marshal(participant)
@@ -158,7 +158,7 @@ func (p *RedisRoomStore) StoreParticipant(ctx context.Context, roomName string,
return p.rc.HSet(p.ctx, key, participant.Identity, data).Err()
}
func (p *RedisRoomStore) LoadParticipant(ctx context.Context, roomName, identity string) (*livekit.ParticipantInfo, error) {
func (p *RedisRoomStore) LoadParticipant(_ context.Context, roomName, identity string) (*livekit.ParticipantInfo, error) {
key := RoomParticipantsPrefix + roomName
data, err := p.rc.HGet(p.ctx, key, identity).Result()
if err == redis.Nil {
@@ -174,7 +174,7 @@ func (p *RedisRoomStore) LoadParticipant(ctx context.Context, roomName, identity
return &pi, nil
}
func (p *RedisRoomStore) ListParticipants(ctx context.Context, roomName string) ([]*livekit.ParticipantInfo, error) {
func (p *RedisRoomStore) ListParticipants(_ context.Context, roomName string) ([]*livekit.ParticipantInfo, error) {
key := RoomParticipantsPrefix + roomName
items, err := p.rc.HVals(p.ctx, key).Result()
if err == redis.Nil {
@@ -194,7 +194,7 @@ func (p *RedisRoomStore) ListParticipants(ctx context.Context, roomName string)
return participants, nil
}
func (p *RedisRoomStore) DeleteParticipant(ctx context.Context, roomName, identity string) error {
func (p *RedisRoomStore) DeleteParticipant(_ context.Context, roomName, identity string) error {
key := RoomParticipantsPrefix + roomName
return p.rc.HDel(p.ctx, key, identity).Err()
+4 -4
View File
@@ -7,7 +7,7 @@ import (
"testing"
"time"
livekit "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/livekit"
"github.com/stretchr/testify/require"
"github.com/livekit/livekit-server/pkg/service"
@@ -18,7 +18,7 @@ func TestParticipantPersistence(t *testing.T) {
rs := service.NewRedisRoomStore(redisClient())
roomName := "room1"
rs.DeleteRoom(ctx, roomName)
_ = rs.DeleteRoom(ctx, roomName)
p := &livekit.ParticipantInfo{
Sid: "PA_test",
@@ -93,7 +93,7 @@ func TestRoomLock(t *testing.T) {
// release after 2 ms
time.Sleep(2 * time.Millisecond)
atomic.StoreUint32(&unlocked, 1)
rs.UnlockRoom(ctx, roomName, token)
_ = rs.UnlockRoom(ctx, roomName, token)
wg.Wait()
})
@@ -106,6 +106,6 @@ func TestRoomLock(t *testing.T) {
time.Sleep(lockInterval + time.Millisecond)
token2, err := rs.LockRoom(ctx, roomName, lockInterval)
require.NoError(t, err)
rs.UnlockRoom(ctx, roomName, token2)
_ = rs.UnlockRoom(ctx, roomName, token2)
})
}
+6 -6
View File
@@ -4,7 +4,7 @@ import (
"context"
"time"
livekit "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils"
@@ -86,8 +86,8 @@ func (r *StandardRoomAllocator) CreateRoom(ctx context.Context, req *livekit.Cre
}
// select a new node
nodeId := req.NodeId
if nodeId == "" {
nodeID := req.NodeId
if nodeID == "" {
nodes, err := r.router.ListNodes()
if err != nil {
return nil, err
@@ -98,11 +98,11 @@ func (r *StandardRoomAllocator) CreateRoom(ctx context.Context, req *livekit.Cre
return nil, err
}
nodeId = node.Id
nodeID = node.Id
}
logger.Debugw("selected node for room", "room", rm.Name, "roomID", rm.Sid, "nodeID", nodeId)
err = r.router.SetNodeForRoom(ctx, rm.Name, nodeId)
logger.Debugw("selected node for room", "room", rm.Name, "roomID", rm.Sid, "nodeID", nodeID)
err = r.router.SetNodeForRoom(ctx, rm.Name, nodeID)
if err != nil {
return nil, err
}
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"context"
"testing"
livekit "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/livekit"
"github.com/stretchr/testify/require"
"github.com/livekit/livekit-server/pkg/config"
+3 -3
View File
@@ -66,7 +66,7 @@ func NewLocalRoomManager(
return r, nil
}
func (r *RoomManager) GetRoom(ctx context.Context, roomName string) *rtc.Room {
func (r *RoomManager) GetRoom(_ context.Context, roomName string) *rtc.Room {
r.lock.RLock()
defer r.lock.RUnlock()
return r.rooms[roomName]
@@ -101,7 +101,7 @@ func (r *RoomManager) DeleteRoom(ctx context.Context, roomName string) error {
return err
}
// CleanupRooms cleans up after old rooms that have been around for awhile
// CleanupRooms cleans up after old rooms that have been around for a while
func (r *RoomManager) CleanupRooms() error {
// cleanup rooms that have been left for over a day
ctx := context.Background()
@@ -381,7 +381,7 @@ func (r *RoomManager) rtcSessionWorker(room *rtc.Room, participant types.Partici
}
// handles RTC messages resulted from Room API calls
func (r *RoomManager) handleRTCMessage(ctx context.Context, roomName, identity string, msg *livekit.RTCNodeMessage) {
func (r *RoomManager) handleRTCMessage(_ context.Context, roomName, identity string, msg *livekit.RTCNodeMessage) {
r.lock.RLock()
room := r.rooms[roomName]
r.lock.RUnlock()
+2 -2
View File
@@ -3,7 +3,7 @@ package service
import (
"context"
livekit "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/livekit"
"github.com/pkg/errors"
"github.com/thoas/go-funk"
"github.com/twitchtv/twirp"
@@ -40,7 +40,7 @@ func (s *RoomService) CreateRoom(ctx context.Context, req *livekit.CreateRoomReq
return
}
func (s *RoomService) ListRooms(ctx context.Context, req *livekit.ListRoomsRequest) (res *livekit.ListRoomsResponse, err error) {
func (s *RoomService) ListRooms(ctx context.Context, _ *livekit.ListRoomsRequest) (res *livekit.ListRoomsResponse, err error) {
err = EnsureListPermission(ctx)
if err != nil {
return nil, twirpAuthError(err)
+4 -3
View File
@@ -4,12 +4,13 @@ import (
"context"
"testing"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
"github.com/stretchr/testify/require"
"github.com/livekit/livekit-server/pkg/routing/routingfakes"
"github.com/livekit/livekit-server/pkg/service"
"github.com/livekit/livekit-server/pkg/service/servicefakes"
"github.com/livekit/protocol/auth"
livekit "github.com/livekit/protocol/livekit"
"github.com/stretchr/testify/require"
)
const grantsKey = "grants"
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"regexp"
"github.com/livekit/protocol/auth"
livekit "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
)
+2 -1
View File
@@ -4,8 +4,9 @@ import (
"testing"
"github.com/go-redis/redis/v8"
"github.com/livekit/livekit-server/pkg/service"
"github.com/stretchr/testify/require"
"github.com/livekit/livekit-server/pkg/service"
)
func redisClient() *redis.Client {
+1 -2
View File
@@ -10,9 +10,8 @@ import (
"github.com/go-redis/redis/v8"
"github.com/google/wire"
livekit "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/livekit"
"github.com/pkg/errors"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils"
+3 -4
View File
@@ -1,8 +1,7 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate go run github.com/google/wire/cmd/wire
//go:build !wireinject
// +build !wireinject
//+build !wireinject
package service
@@ -28,7 +27,7 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
if err != nil {
return nil, err
}
router := routing.CreateRouter(conf, client, currentNode)
router := routing.CreateRouter(client, currentNode)
roomStore := createStore(client)
roomAllocator, err := NewRoomAllocator(conf, router, roomStore)
if err != nil {
@@ -72,7 +71,7 @@ func InitializeRouter(conf *config.Config, currentNode routing.LocalNode) (routi
if err != nil {
return nil, err
}
router := routing.CreateRouter(conf, client, currentNode)
router := routing.CreateRouter(client, currentNode)
return router, nil
}
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"time"
"github.com/gorilla/websocket"
livekit "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"